嵌套的 if 语句在 Java 中无法正常工作。

huangapple go评论96阅读模式
英文:

Stacked if statements not working in java

问题

在以下的代码中,当我执行它时,只有第一个 if 块会起作用(当我按下其他选项的按钮时,没有弹出窗口)。我做错了什么?这是一个用于创建一个可以在二进制、十进制和十六进制之间转换数字的应用程序的程序。

  1. public class Menu implements ActionListener {
  2. public static void main(String[] args) {
  3. new Menu().createMenu();
  4. }
  5. JFrame frame = new JFrame();
  6. JPanel panel = new JPanel();
  7. JButton binToHex = new JButton("Bin to Hex");
  8. JButton binToDec = new JButton("Bin to Dec");
  9. JButton hexToBin = new JButton("Hex to Bin");
  10. JButton hexToDec = new JButton("Hex to Dec");
  11. JButton decToHex = new JButton("Dec to Hex");
  12. JButton decToBin = new JButton("Dec to Bin");
  13. private void createMenu() {
  14. frame.setVisible(true);
  15. frame.add(panel);
  16. panel.add(binToHex);
  17. panel.add(binToDec);
  18. panel.add(hexToBin);
  19. panel.add(hexToDec);
  20. panel.add(decToHex);
  21. panel.add(decToBin);
  22. frame.pack();
  23. binToHex.addActionListener(this);
  24. }
  25. public void actionPerformed(ActionEvent e) {
  26. String input = new String();
  27. if (e.getSource() == binToHex) {
  28. input = JOptionPane.showInputDialog(null, "Enter a binary number");
  29. Binary.convertToHexadecimal();
  30. }
  31. if (e.getSource() == binToDec) {
  32. input = JOptionPane.showInputDialog(null, "Enter a binary number");
  33. Binary.convertToDecimal();
  34. }
  35. if (e.getSource() == hexToBin) {
  36. input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
  37. Hexadecimal.convertToBinary();
  38. }
  39. if (e.getSource() == hexToDec) {
  40. input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
  41. Hexadecimal.convertToDecimal();
  42. }
  43. if (e.getSource() == decToHex) {
  44. input = JOptionPane.showInputDialog(null, "Enter a decimal number");
  45. Decimal.convertToHexadecimal(input);
  46. }
  47. if (e.getSource() == decToBin) {
  48. input = JOptionPane.showInputDialog(null, "Enter a decimal number");
  49. Decimal.convertToBinary(input);
  50. }
  51. }
  52. }

如果你遇到了只有第一个 if 块起作用的问题,这可能是因为其他选项的按钮没有绑定正确的动作监听器,或者在按钮被按下时没有执行相应的转换操作。请确保每个按钮都正确地绑定了 ActionListener,并在相应的 if 块中执行相应的转换操作。

英文:

In the following code, when I execute it, only the first if block will work (when I press the button for the other options, no pop-ups happen). What am I doing wrong? This is a program designed to make an app that can convert numbers between binary, decimal, and hexadecimal.

  1. public class Menu implements ActionListener {
  2. public static void main(String[] args) {
  3. new Menu().createMenu();
  4. }
  5. JFrame frame = new JFrame();
  6. JPanel panel = new JPanel();
  7. JButton binToHex = new JButton("Bin to Hex");
  8. JButton binToDec = new JButton("Bin to Dec");
  9. JButton hexToBin = new JButton("Hex to Bin");
  10. JButton hexToDec = new JButton("Hex to Dec");
  11. JButton decToHex = new JButton("Dec to Hex");
  12. JButton decToBin = new JButton("Dec to Bin");
  13. private void createMenu() {
  14. frame.setVisible(true);
  15. frame.add(panel);
  16. panel.add(binToHex);
  17. panel.add(binToDec);
  18. panel.add(hexToBin);
  19. panel.add(hexToDec);
  20. panel.add(decToHex);
  21. panel.add(decToBin);
  22. frame.pack();
  23. binToHex.addActionListener(this);
  24. }
  25. public void actionPerformed(ActionEvent e) {
  26. String input = new String();
  27. if (e.getSource() == binToHex) {
  28. input = JOptionPane.showInputDialog(null, "Enter a binary number");
  29. Binary.convertToHexadecimal();
  30. }
  31. if (e.getSource() == binToDec) {
  32. input = JOptionPane.showInputDialog(null, "Enter a binary number");
  33. Binary.convertToDecimal();
  34. }
  35. if (e.getSource() == hexToBin) {
  36. input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
  37. Hexadecimal.convertToBinary();
  38. }
  39. if (e.getSource() == hexToDec) {
  40. input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
  41. Hexadecimal.convertToDecimal();
  42. }
  43. if (e.getSource() == decToHex) {
  44. input = JOptionPane.showInputDialog(null, "Enter a decimal number");
  45. Decimal.convertToHexadecimal(input);
  46. }
  47. if (e.getSource() == decToBin) {
  48. input = JOptionPane.showInputDialog(null, "Enter a decimal number");
  49. Decimal.convertToBinary(input);
  50. }
  51. }

}

答案1

得分: 3

你忘记在所有的 JButton 实例上调用 addActionListener 方法:

  1. binToHex.addActionListener(this); // 你已经有这个
  2. binToDec.addActionListener(this);
  3. hexToBin.addActionListener(this);
  4. hexToDec.addActionListener(this);
  5. decToHex.addActionListener(this);
  6. decToBin.addActionListener(this);
英文:

You forgot to call addActionListener on all your JButton instances:

  1. binToHex.addActionListener(this); // you have this one
  2. binToDec.addActionListener(this);
  3. hexToBin.addActionListener(this);
  4. hexToDec.addActionListener(this);
  5. decToHex.addActionListener(this);
  6. decToBin.addActionListener(this);

huangapple
  • 本文由 发表于 2020年10月6日 06:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64217162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定