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

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

Stacked if statements not working in java

问题

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

public class Menu implements ActionListener {
    public static void main(String[] args) {
        new Menu().createMenu();
    }

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton binToHex = new JButton("Bin to Hex");
    JButton binToDec = new JButton("Bin to Dec");
    JButton hexToBin = new JButton("Hex to Bin");
    JButton hexToDec = new JButton("Hex to Dec");
    JButton decToHex = new JButton("Dec to Hex");
    JButton decToBin = new JButton("Dec to Bin");

    private void createMenu() {
        frame.setVisible(true);
        frame.add(panel);
        panel.add(binToHex);
        panel.add(binToDec);
        panel.add(hexToBin);
        panel.add(hexToDec);
        panel.add(decToHex);
        panel.add(decToBin);
        frame.pack();
        binToHex.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String input = new String();
        if (e.getSource() == binToHex) {
            input = JOptionPane.showInputDialog(null, "Enter a binary number");
            Binary.convertToHexadecimal();
        } 
        
        if (e.getSource() == binToDec) {
            input = JOptionPane.showInputDialog(null, "Enter a binary number");
            Binary.convertToDecimal();
        } 
        
        if (e.getSource() == hexToBin) {
            input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
            Hexadecimal.convertToBinary();
        }
        
        if (e.getSource() == hexToDec) {
            input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
            Hexadecimal.convertToDecimal();
        }
        
        if (e.getSource() == decToHex) {
            input = JOptionPane.showInputDialog(null, "Enter a decimal number");
            Decimal.convertToHexadecimal(input);
        }
        
        if (e.getSource() == decToBin) {
            input = JOptionPane.showInputDialog(null, "Enter a decimal number");
            Decimal.convertToBinary(input);
        }
        
    }
}

如果你遇到了只有第一个 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.

public class Menu implements ActionListener {
public static void main(String[] args) {
new Menu().createMenu();
}
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton binToHex = new JButton("Bin to Hex");
JButton binToDec = new JButton("Bin to Dec");
JButton hexToBin = new JButton("Hex to Bin");
JButton hexToDec = new JButton("Hex to Dec");
JButton decToHex = new JButton("Dec to Hex");
JButton decToBin = new JButton("Dec to Bin");
private void createMenu() {
frame.setVisible(true);
frame.add(panel);
panel.add(binToHex);
panel.add(binToDec);
panel.add(hexToBin);
panel.add(hexToDec);
panel.add(decToHex);
panel.add(decToBin);
frame.pack();
binToHex.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String input = new String();
if (e.getSource() == binToHex) {
input = JOptionPane.showInputDialog(null, "Enter a binary number");
Binary.convertToHexadecimal();
} 
if (e.getSource() == binToDec) {
input = JOptionPane.showInputDialog(null, "Enter a binary number");
Binary.convertToDecimal();
} 
if (e.getSource() == hexToBin) {
input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
Hexadecimal.convertToBinary();
}
if (e.getSource() == hexToDec) {
input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
Hexadecimal.convertToDecimal();
}
if (e.getSource() == decToHex) {
input = JOptionPane.showInputDialog(null, "Enter a decimal number");
Decimal.convertToHexadecimal(input);
}
if (e.getSource() == decToBin) {
input = JOptionPane.showInputDialog(null, "Enter a decimal number");
Decimal.convertToBinary(input);
}
}

}

答案1

得分: 3

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

binToHex.addActionListener(this); // 你已经有这个
binToDec.addActionListener(this);
hexToBin.addActionListener(this);
hexToDec.addActionListener(this);
decToHex.addActionListener(this);
decToBin.addActionListener(this);
英文:

You forgot to call addActionListener on all your JButton instances:

binToHex.addActionListener(this); // you have this one
binToDec.addActionListener(this);
hexToBin.addActionListener(this);
hexToDec.addActionListener(this);
decToHex.addActionListener(this);
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:

确定