java gui swing计算器运算符

qacovj5a  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(404)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

四年前关门了。
改进这个问题
我试图找出如何在当前代码中实现几个按钮,但经过多次尝试,我似乎无法找到它。我需要做以下工作:
所以我的问题是:当我把两个数字加在一起,如果它们不是0(例如x=10和y=10),当我点击加号按钮时,y字段中的内容消失了,我该怎么解决这个问题?
另外,我的清除按钮没有清除x,y和result字段中的所有内容,我该如何解决这个问题?
如何右对齐x和y字段?

  1. package democalc;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextField;
  12. import javax.swing.border.EtchedBorder;
  13. class Calculator implements ActionListener {
  14. private JFrame frame;
  15. private JTextField xfield, yfield;
  16. private JLabel rslt;
  17. private JButton multiButton, clearButton, addButton, subButton, divideButton;
  18. private JPanel xpanel, buttonPanel;
  19. public Calculator() {
  20. frame = new JFrame();
  21. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. frame.setLayout(new BorderLayout());
  23. xpanel = new JPanel();
  24. xpanel.setBorder(new EtchedBorder());
  25. xpanel.setLayout(new GridLayout(3, 2));
  26. xpanel.add(new JLabel("x ="));
  27. xfield = new JTextField("0", 5);
  28. xpanel.add(xfield);
  29. xpanel.add(new JLabel("y ="));
  30. yfield = new JTextField("0", 5);
  31. xpanel.add(yfield);
  32. xpanel.add(new JLabel());
  33. rslt = new JLabel("0");
  34. rslt.setBackground(Color.green);
  35. rslt.setBorder(new EtchedBorder());
  36. rslt.setOpaque(true);
  37. xpanel.add(rslt);
  38. frame.add(xpanel, BorderLayout.NORTH);
  39. buttonPanel = new JPanel();
  40. frame.add(buttonPanel, BorderLayout.CENTER);
  41. buttonPanel.add(addButton = new JButton("+"));
  42. addButton.addActionListener(this);
  43. buttonPanel.add(subButton = new JButton("-"));
  44. subButton.addActionListener(this);
  45. buttonPanel.add(multiButton = new JButton("*"));
  46. multiButton.addActionListener(this);
  47. buttonPanel.add(divideButton = new JButton("/"));
  48. divideButton.addActionListener(this);
  49. buttonPanel.add(clearButton = new JButton("Clear"));
  50. clearButton.addActionListener(this);
  51. frame.pack();
  52. frame.setVisible(true);
  53. }
  54. public void actionPerformed(ActionEvent event) {
  55. int x = 0, y = 0;
  56. try {
  57. String xStr = xfield.getText();
  58. x = Integer.parseInt(xStr);
  59. }
  60. catch (NumberFormatException e) {
  61. // The string xStr is not a legal number.
  62. rslt.setText("Illegal data for x.");
  63. xfield.requestFocusInWindow();
  64. return;
  65. }
  66. /* Get a number from yfield in the same way. */
  67. try {
  68. String yStr = yfield.getText();
  69. y = Integer.parseInt(yStr);
  70. }
  71. catch (NumberFormatException e) {
  72. rslt.setText("Illegal data for y.");
  73. yfield.requestFocusInWindow();
  74. return;
  75. }
  76. /* Perform the operation based on the action command
  77. from the button. Note that division by zero produces
  78. an error message. */
  79. String op = event.getActionCommand();
  80. if (op.equals("+"))
  81. rslt.setText( "x + y = " + (x+y) );
  82. else if (op.equals("-"))
  83. rslt.setText( "x - y = " + (x-y) );
  84. else if (op.equals("*"))
  85. rslt.setText( "x * y = " + (x*y) );
  86. else if (op.equals("/")) {
  87. if (y == 0)
  88. rslt.setText("ERROR");
  89. else
  90. rslt.setText( "x / y = " + (x/y) );
  91. }
  92. if (event.getSource() == clearButton)
  93. xfield.setText("0");
  94. yfield.setText("0");
  95. }
  96. }
k0pti3hp

k0pti3hp1#

你的if花括号不见了。这就是为什么

  1. yfield.setText("0");

每次都在执行。用正确的代码替换代码

  1. if (event.getSource() == clearButton){
  2. xfield.setText("0");
  3. yfield.setText("0");
  4. }

相关问题