简体   繁体   中英

Java Text Field Not Updating

I am trying to make a calculator program where click a button and then it would add to the Java Text Field(Not the Java Text Area), and I use the ActionEvent e to figure out the action. Then using the actionCommand where I get the actionListener I try to add the text to the java text field. And somehow it is not updating into the java text field. Is this a problem with repaint or something.

Here's my code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.Writer;
import java.util.ArrayList;

public class Calculator extends JFrame implements ActionListener {
    String actionCommand = "";
    ArrayList<Integer> numberSet1 = new ArrayList<Integer>();
    ArrayList<Integer> numberSet2 = new ArrayList<Integer>();
    JLabel jl = new JLabel();
    JPanel jp = new JPanel();
    int number1 = 0;
    int number2 = 0;
    JTextField jtf = new JTextField();
    JButton btn1;
    JTextArea jta = new JTextArea();
    public Calculator() {
        super("Calculator");
        try {

            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600,600);
        jp.setLayout(new BorderLayout());
        JTextField jtf = new JTextField("This is the text field");
        jtf.setSize(getWidth(),50);
        add(jtf, BorderLayout.NORTH);
        jtf.setBackground(Color.YELLOW);
        jtf.setEditable(false);
        JPanel jp = new JPanel();
        Font myFont = new Font("Serif", Font.BOLD, 32);
        jp.setLayout(new GridLayout(4,4));
        JButton butt0 = new JButton("0");
        butt0.setFont(myFont);
        JButton butt1 = new JButton("1");
        butt1.setFont(myFont);
        JButton butt2 = new JButton("2");
        butt2.setFont(myFont);
        JButton butt3 = new JButton("3");
        butt3.setFont(myFont);
        JButton butt4 = new JButton("4");
        butt4.setFont(myFont);
        JButton butt5 = new JButton("5");
        butt5.setFont(myFont);
        JButton butt6 = new JButton("6");
        butt6.setFont(myFont);
        JButton butt7 = new JButton("7");
        butt7.setFont(myFont);
        JButton butt8 = new JButton("8");
        butt8.setFont(myFont);
        JButton butt9 = new JButton("9");
        butt9.setFont(myFont);
        JButton butt10 = new JButton("+");
        butt10.setFont(myFont);
        JButton butt11 = new JButton("-");
        butt11.setFont(myFont);
        JButton butt12 = new JButton("*");
        butt12.setFont(myFont);
        JButton butt13 = new JButton("/");
        butt13.setFont(myFont);
        JButton butt14 = new JButton("=");
        butt14.setFont(myFont);
        JButton butt15 = new JButton("clear");
        butt15.setFont(myFont);
        butt1.setBackground(Color.cyan);
        butt2.setBackground(Color.cyan);
        butt3.setBackground(Color.cyan);
        butt4.setBackground(Color.cyan);
        butt5.setBackground(Color.cyan);
        butt6.setBackground(Color.cyan);
        butt7.setBackground(Color.cyan);
        butt8.setBackground(Color.cyan);
        butt9.setBackground(Color.cyan);
        butt10.setBackground(Color.red);
        butt11.setBackground(Color.red);
        butt12.setBackground(Color.red);
        butt13.setBackground(Color.red);
        butt14.setBackground(Color.red);
        butt15.setBackground(Color.red);
        butt0.setBackground(Color.cyan);
        butt10.setForeground(Color.lightGray);
        butt11.setForeground(Color.lightGray);
        butt12.setForeground(Color.lightGray);
        butt13.setForeground(Color.lightGray);
        butt14.setForeground(Color.lightGray);
        butt15.setForeground(Color.lightGray);
        jp.add(butt0);
        jp.add(butt1);
        jp.add(butt2);
        jp.add(butt3);
        jp.add(butt4);
        jp.add(butt5);
        jp.add(butt6);
        jp.add(butt7);
        jp.add(butt8);
        jp.add(butt9);
        jp.add(butt10);
        jp.add(butt11);
        jp.add(butt12);
        jp.add(butt13);
        jp.add(butt14);
        jp.add(butt15);
        butt0.addActionListener(this);
        butt0.setActionCommand("0");
        butt1.addActionListener(this);
        butt1.setActionCommand("1");
        butt2.addActionListener(this);
        butt2.setActionCommand("2");
        butt3.addActionListener(this);
        butt3.setActionCommand("3");
        butt4.addActionListener(this);
        butt4.setActionCommand("4");
        butt5.addActionListener(this);
        butt5.setActionCommand("5");
        butt6.addActionListener(this);
        butt6.setActionCommand("6");
        butt7.addActionListener(this);
        butt7.setActionCommand("7");
        butt8.addActionListener(this);
        butt8.setActionCommand("8");
        butt9.addActionListener(this);
        butt9.setActionCommand("9");
        butt10.addActionListener(this);
        butt10.setActionCommand("+");
        butt11.addActionListener(this);
        butt11.setActionCommand("-");
        butt12.addActionListener(this);
        butt12.setActionCommand("*");
        butt13.addActionListener(this);
        butt13.setActionCommand("/");
        butt14.addActionListener(this);
        butt14.setActionCommand("=");
        butt15.addActionListener(this);
        butt15.setActionCommand("clear");

        add(jp, BorderLayout.CENTER);
        setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        actionCommand  = e.getActionCommand();
        System.out.println("Clicked" + actionCommand);
        jtf.setText(actionCommand);
    }

    public void phase1() {
        while (!(actionCommand.equals("+")) || !(actionCommand.equals("-")) || !(actionCommand.equals("*")) || !(actionCommand.equals("/")) || !(actionCommand.equals("clear")) || !(actionCommand.equals("="))) {
            numberSet1.add(Integer.parseInt(actionCommand));
        }
        for(int i = 0; i < numberSet1.size(); i++) {

        }
    }

    public void phase2() {

    }
    public int calculations() {
        return 0;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
    }


}

You have two different JTextField variables that are both named jtf .

The first is an instance variable, which you have declared with JTextField jtf = new JTextField(); , and is accessible from anywhere in the class.

The second is a local variable, which you have declared with JTextField jtf = new JTextField("This is the text field"); , and is only accessible in the constructor of Calculator .

The problem is that the second jtf (the local variable) is being added to the UI, while the first jtf (the instance variable) is what's being updated by the action event.

To fix this, change this (near the top of the class): JTextField jtf = new JTextField();

to this: JTextField jtf;

And then change this (in the constructor):

JTextField jtf = new JTextField("This is the text field");

to this: jtf = new JTextField("This is the text field");

Then, you'll only have one jtf variable (which will be an instance variable), and your action event should work.

Just remove this line:

JTextField jtf = new JTextField("This is the text field"); 

from constructor. The first is an instance variable, JTextField eg jtf is accessible from anywhere in the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM