堆叠if语句在java中不起作用

堆叠if语句在java中不起作用,java,if-statement,Java,If Statement,在下面的代码中,当我执行它时,只有第一个if块会工作(当我按下其他选项的按钮时,不会出现弹出窗口)。我做错了什么?这是一个设计用来制作一个应用程序的程序,可以在二进制、十进制和十六进制之间转换数字 public class Menu implements ActionListener { public static void main(String[] args) { new Menu().createMenu(); } JFrame frame = n

在下面的代码中,当我执行它时,只有第一个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);
    }
    
}

}

您忘记在所有
JButton
实例上调用
addActionListener

binToHex.addActionListener(this); // you have this one
binToDec.addActionListener(this);
hexToBin.addActionListener(this);
hexToDec.addActionListener(this);
decToHex.addActionListener(this);
decToBin.addActionListener(this);

如果您打印出
e.getSource()
以查看其值,这是否有助于解释行为?
getSource()
的返回类型是什么?什么类型是binToHex和其他类型?如果它们是
String
,请参阅:@Andreas
binToHex
etc是
JButton
s-单击事件的源。它们不是字符串。binToHex是一个JButton。对于e.getSource(),该调用适用于第一个调用,所以我不知道为什么第二个调用不适用。比较字符串是使用字符串#equals()而不是==。