我试图制作一个计算器程序,单击一个按钮,然后将其添加到Java文本字段(而不是Java文本区域)中,然后使用ActionEvent e找出操作。然后,使用在我获得actionListener的地方的actionCommand,尝试将文本添加到java文本字段中。并且它以某种方式没有更新到java文本字段中。这是粉刷问题吗?
这是我的代码
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();
}
}
答案 0 :(得分:0)
您有两个不同的JTextField变量,都命名为jtf
。
第一个是实例变量,您已使用JTextField jtf = new JTextField();
进行了声明,并且可以从类中的任何位置进行访问。
第二个是局部变量,已使用JTextField jtf = new JTextField("This is the text field");
声明,只能在Calculator
的构造函数中访问。
问题在于第二个jtf
(局部变量)被添加到UI,而第一个jtf
(实例变量)被动作事件更新。
要解决此问题,请对此进行更改(在班级顶部附近):JTextField jtf = new JTextField();
对此:JTextField jtf;
然后更改它(在构造函数中):
JTextField jtf = new JTextField("This is the text field");
对此:jtf = new JTextField("This is the text field");
然后,您只有一个jtf
变量(它将是一个实例变量),并且您的操作事件应该可以正常工作。
答案 1 :(得分:0)
只需删除此行:
JTextField jtf = new JTextField("This is the text field");
来自构造函数。 第一个是实例变量JTextField,例如可以从类中的任何地方访问jtf。