关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。
四年前关门了。
改进这个问题
我试图找出如何在当前代码中实现几个按钮,但经过多次尝试,我似乎无法找到它。我需要做以下工作:
所以我的问题是:当我把两个数字加在一起,如果它们不是0(例如x=10和y=10),当我点击加号按钮时,y字段中的内容消失了,我该怎么解决这个问题?
另外,我的清除按钮没有清除x,y和result字段中的所有内容,我该如何解决这个问题?
如何右对齐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");
}
}
1条答案
按热度按时间k0pti3hp1#
你的if花括号不见了。这就是为什么
每次都在执行。用正确的代码替换代码