我试图弄清楚如何在当前代码中实现一些按钮,但经过多次尝试,我似乎无法弄清楚。我需要执行以下操作:
所以我的问题是:当我将 2 个数字加在一起并且它们不为 0(例如 x=10 和 y=10)时,当我单击加号按钮时 y 字段中的内容消失,我该如何解决这个问题吗?
而且我的清除按钮没有清除 x、y 和结果字段中的所有内容,我该如何解决这个问题?
如何右对齐 x 和 y 字段?
package democalc;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
class Calculator implements ActionListener {
private JFrame frame;
private JTextField xfield, yfield;
private JLabel rslt;
private JButton multiButton, clearButton, addButton, subButton, divideButton;
private JPanel xpanel, buttonPanel;
public Calculator() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
xpanel = new JPanel();
xpanel.setBorder(new EtchedBorder());
xpanel.setLayout(new GridLayout(3, 2));
xpanel.add(new JLabel("x ="));
xfield = new JTextField("0", 5);
xpanel.add(xfield);
xpanel.add(new JLabel("y ="));
yfield = new JTextField("0", 5);
xpanel.add(yfield);
xpanel.add(new JLabel());
rslt = new JLabel("0");
rslt.setBackground(Color.green);
rslt.setBorder(new EtchedBorder());
rslt.setOpaque(true);
xpanel.add(rslt);
frame.add(xpanel, BorderLayout.NORTH);
buttonPanel = new JPanel();
frame.add(buttonPanel, BorderLayout.CENTER);
buttonPanel.add(addButton = new JButton("+"));
addButton.addActionListener(this);
buttonPanel.add(subButton = new JButton("-"));
subButton.addActionListener(this);
buttonPanel.add(multiButton = new JButton("*"));
multiButton.addActionListener(this);
buttonPanel.add(divideButton = new JButton("/"));
divideButton.addActionListener(this);
buttonPanel.add(clearButton = new JButton("Clear"));
clearButton.addActionListener(this);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
int x = 0, y = 0;
try {
String xStr = xfield.getText();
x = Integer.parseInt(xStr);
}
catch (NumberFormatException e) {
// The string xStr is not a legal number.
rslt.setText("Illegal data for x.");
xfield.requestFocusInWindow();
return;
}
/* Get a number from yfield in the same way. */
try {
String yStr = yfield.getText();
y = Integer.parseInt(yStr);
}
catch (NumberFormatException e) {
rslt.setText("Illegal data for y.");
yfield.requestFocusInWindow();
return;
}
/* Perform the operation based on the action command
from the button. Note that division by zero produces
an error message. */
String op = event.getActionCommand();
if (op.equals("+"))
rslt.setText( "x + y = " + (x+y) );
else if (op.equals("-"))
rslt.setText( "x - y = " + (x-y) );
else if (op.equals("*"))
rslt.setText( "x * y = " + (x*y) );
else if (op.equals("/")) {
if (y == 0)
rslt.setText("ERROR");
else
rslt.setText( "x / y = " + (x/y) );
}
if (event.getSource() == clearButton)
xfield.setText("0");
yfield.setText("0");
}
}
最佳答案
您的 if 大括号丢失。这就是为什么
yfield.setText("0");
每次都会执行。 将您的代码替换为正确的代码
if (event.getSource() == clearButton){
xfield.setText("0");
yfield.setText("0");
}
关于Java gui swing 计算器运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36255021/