import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalField extends JPanel {
private JTextField field;
public CalField() {
setLayout(new GridLayout(1, 1, 5, 5));
field = new JTextField(20);
field.setFont(new Font("SansSerif", Font.PLAIN, 20));
field.setEditable(true);
add(field);
}
public JTextField getField() {
return field;
}
public void setField(String text) {
field.setText(text);
System.out.print(text);
}
}
CalButton类:
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CalButton extends JPanel {
private JButton[] buttons;
private static final String[] buttonNames = { "7", "8", "9", "/", "4", "5",
"6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private CalField calField;
public CalButton() {
buttons = new JButton[buttonNames.length];
setLayout(new GridLayout(4, 4, 3, 3));
calField = new CalField();
ButtonListener handler = new ButtonListener();
for (int i = 0; i < buttonNames.length; i++) {
buttons[i] = new JButton(buttonNames[i]);
buttons[i].setFont(new Font("SansSerif", Font.BOLD, 20));
add(buttons[i]);
buttons[i].addActionListener(handler);
}
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
if (buttons[0].getActionCommand() == event.getActionCommand()) {
calField.setField(event.getActionCommand());
// System.out.print(buttons[0].getActionCommand());
} else if (buttons[1].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[1].getActionCommand());
} else if (buttons[2].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[2].getActionCommand());
} else if (buttons[3].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[3].getActionCommand());
} else if (buttons[4].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[4].getActionCommand());
} else if (buttons[5].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[5].getActionCommand());
} else if (buttons[6].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[6].getActionCommand());
} else if (buttons[7].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[7].getActionCommand());
} else if (buttons[8].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[8].getActionCommand());
} else if (buttons[9].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[9].getActionCommand());
} else if (buttons[10].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[10].getActionCommand());
} else if (buttons[11].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[11].getActionCommand());
} else if (buttons[12].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[12].getActionCommand());
} else if (buttons[13].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[13].getActionCommand());
} else if (buttons[14].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[14].getActionCommand());
} else if (buttons[15].getActionCommand() == event
.getActionCommand()) {
System.out.print(buttons[15].getActionCommand());
}
}
}
}
计算器类:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Calculator {
public static void main(String[] args) {
JFrame application = new JFrame("Calculator");
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(250, 200);
CalField calField = new CalField();
CalButton calButton = new CalButton();
application.add(calField, BorderLayout.PAGE_START);
application.add(calButton, BorderLayout.CENTER);
application.setVisible(true);
}
}
在此程序中,在
JButton
的actionListener中,我调用了CalField类的setText()
方法以在JTextField
中设置相应按钮的文本。但是文本显示在控制台中,但没有显示在
TextField
中。我现在能做什么?请帮助我。 最佳答案
因此,在您的Calculator
类中,创建一个CalField
实例并将其添加到JFrame
...
public class Calculator {
public static void main(String[] args) {
JFrame application = new JFrame("Calculator");
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(250, 200);
CalField calField = new CalField();
CalButton calButton = new CalButton();
application.add(calField, BorderLayout.PAGE_START);
application.add(calButton, BorderLayout.CENTER);
application.setVisible(true);
}
}
然后,在您的
CalButton
类中,创建CalField
的另一个实例...public class CalButton extends JPanel {
//...
private CalField calField;
public CalButton() {
//...
calField = new CalField();
//...
}
它永远不会添加到任何东西,而是您尝试更新的实例。
CalField
的此实例与您添加到框架的CalField
的实例无关。相反,您需要将
CalField
的实例传递给CalButton
public class CalButton extends JPanel {
//...
private CalField calField;
public CalButton(CalField calField) {
//...
this.calField = calField;
//...
}
然后在您的
Calculator
类中,将您创建的CalField
实例传递给CalButton
实例...CalField calField = new CalField();
CalButton calButton = new CalButton(calField);
application.add(calField, BorderLayout.PAGE_START);
application.add(calButton, BorderLayout.CENTER);
这会在实际显示在屏幕上的类之间创建一种关系
现在,当我单击一个按钮时,它将显示文本,但是当我单击另一个按钮时,它将覆盖前一个的文本。如何解决此问题,以便不会发生覆盖
这是因为
setText
就是这样做的,它设置了字段的文本,替换了之前的内容。相反,当您调用
setField
时,应在字段中的当前文本后面添加新的下一个文本,例如...public void setField(String text) {
field.setText(field.getText() + text);
System.out.print(text);
}
但我可能会将方法更改为
appendField
,所以很清楚它的作用另外,由于
if-else
中的大多数actionPerformed
语句实际上并没有做任何事情,而它们所做的几乎是同一回事,if (buttons[0].getActionCommand() == event.getActionCommand()) {
calField.setField(event.getActionCommand());
} else if (buttons[1].getActionCommand() == event.getActionCommand()) {
System.out.print(buttons[1].getActionCommand());
} else if (buttons[2].getActionCommand() == ...
你可以做更多像...
calField.setField(event.getActionCommand());
现在,您可能需要为特殊键添加一些
if
语句,例如=
并清除,但是所有其他数字按钮都只需要执行上述操作,这将大大减少重复的代码并使代码得以维护更轻松