Search code examples
javajtextfield

How to add what is inputed in a JTextField to a variable?


package basics;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.Caret;

import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

import java.util.Random;
import java.util.random.*;

public class NumGenGIUI {
    
    int x = 1;
    int y = 100;
    Random randomnum = new Random();
    int number = x + randomnum.nextInt(y);

    private JFrame frame;
    private JTextField TEXT1;
    private JLabel lblNewLabel;
    private JButton btnBetAgain;
    private JTextField TEXT2;
    private JTextField TEXT3;
    private JLabel lblNewLabel_1;
    private JTextField TEXT4;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NumGenGIUI window = new NumGenGIUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     */
    public NumGenGIUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 19));
        frame.setBounds(100, 100, 828, 609);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        TEXT1 = new JTextField();
        TEXT1.setBounds(38, 29, 332, 79);
        frame.getContentPane().add(TEXT1);
        TEXT1.setColumns(10);
        TEXT1.getText();
        
        TEXT4 = new JTextField();
        TEXT4.setColumns(10);
        TEXT4.setBounds(38, 130, 332, 79);
        frame.getContentPane().add(TEXT4);
        
        
                

        
        lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(40, 11, 49, 14);
        frame.getContentPane().add(lblNewLabel);
        
        btnBetAgain = new JButton("BET AGAIN");
        btnBetAgain.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                NumGenGIUI.this.TEXT1.setText("input amount");
                TEXT2.setText("");
                TEXT3.setText("");
                
            }
        });
        btnBetAgain.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 40));
        btnBetAgain.setBounds(563, 286, 210, 79);
        frame.getContentPane().add(btnBetAgain);
        
        TEXT2 = new JTextField();
        TEXT2.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 99));
        TEXT2.setColumns(10);
        TEXT2.setBounds(24, 390, 749, 171);
        frame.getContentPane().add(TEXT2);
        
        TEXT3 = new JTextField();
        TEXT3.setBounds(586, 88, 124, 120);
        frame.getContentPane().add(TEXT3);
        TEXT3.setColumns(10);
        
        lblNewLabel_1 = new JLabel("The winning number is?");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 19));
        lblNewLabel_1.setBounds(532, 40, 249, 49);
        frame.getContentPane().add(lblNewLabel_1);
        
        JLabel lblNewLabel_2 = new JLabel("YOUR TOTAL IS...");
        lblNewLabel_2.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.BOLD | Font.ITALIC, 18));
        lblNewLabel_2.setBounds(24, 349, 138, 34);
        frame.getContentPane().add(lblNewLabel_2);
        
        int TEXT1 = 0;
        int TEXT4 = 0;

        JButton btnBET = new JButton("BET");
        btnBET.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do{
                    int x = 1;
                    int y = 30;
                    Random randomnum = new Random();
                    int number = x + randomnum.nextInt(y);
                    int Total =50;
                    if(TEXT1 == number) {
                        Total = Total+(TEXT1 * 2);
                        String TEXT1;
                        
                        TEXT3.setText(Integer.toString(number));
                        TEXT2.setText(Integer.toString(Total));
                    }
                    else{
                        TEXT3.setText(Integer.toString(number));
                        Total =Total -TEXT1;
                        TEXT2.setText(Integer.toString(Total));
                        }
                }while(TEXT1 > 0);
        }
        
        });
    btnBET.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 40));
    btnBET.setBounds(404, 29, 93, 177);
    frame.getContentPane().add(btnBET);
}
}

I want to whatever they enter in TEXT1 to be subtracted from the total if they do not get the right number and if they do i want whatever amount they betted to multiply by 2 and then add to the total however it keep outputting 50 which is the amount they start with whether the number they guessed is right or not


Solution

  • In order to get the text written in a JTextField you should:

        String textInText1 = TEXT1.getText();
    

    but from what I see the variable names you use may lead to some problems (int TEXT1 and JTextField TEXT1) be careful.