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);
}
}
}