My code below does not respond and open the plain message when I click the button after running it, I've checked the code a few times and can't see where I'm going wrong, can anyone see if there's something I missed as I think it should all be implemented correctly but the JButton is not opening my menu items. Many thanks
public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame frame=new JFrame("CoinSorterGUI");
class MyItemListener implements ItemListener{
public void itemStateChanged(ItemEvent ev) {
boolean selected = (ev.getStateChange()== ItemEvent.SELECTED);
AbstractButton button =(AbstractButton) ev.getItemSelectable();
String command = button.getActionCommand();
if (selected) {
int messageType = -1;
String message = "";
if (command.equals("CoinCalculator")) {
messageType = JOptionPane.PLAIN_MESSAGE;
message = "Welcome to the CoinCalculator, click OK to begin program";
}else if (command.equals("Multiple CoinCalculator")) {
messageType = JOptionPane.PLAIN_MESSAGE;
message = "Welcome to the Multiple CoinCalculator, click OK to begin program";
}else if (command.equals("Print Coin List")) {
messageType = JOptionPane.PLAIN_MESSAGE;
message = "Current coin denominations are; £2, £1, 50p, 20p and 10p";
}else if (command.equals("Set details")) {
messageType = JOptionPane.INFORMATION_MESSAGE;
message = " ** Set details Sub Menu ** "
+ " 1 - Set Currency "
+ " 2 - Set minimum input value"
+ " 3 - Set Maximum input value"
+ " 4 - Return to main menu";
}else if (command.equals("Display program configurations")) {
messageType = JOptionPane.PLAIN_MESSAGE;
message = "The current currency is £, the minimum input value is 0 and the maximum input value is 10000";
}
JOptionPane.showMessageDialog(frame,
message,
"CoinSorter",
messageType);
}
}
}
JButton r1 = new JButton("CoinCalculator");
r1.setActionCommand("CoinCalculator");
JButton r2 = new JButton("Multiple CoinCalculator");
r2.setActionCommand("Multiple CoinCalculator");
JButton r3 = new JButton("Print Coin List");
r3.setActionCommand("Print Coin List");
JButton r4 = new JButton("Set details");
r4.setActionCommand("Set details");
JButton r5 = new JButton("Display program configurations");
r5.setActionCommand("Display program configurations");
MyItemListener myItemListener = new MyItemListener();
r1.addItemListener(myItemListener);
r2.addItemListener(myItemListener);
r3.addItemListener(myItemListener);
r4.addItemListener(myItemListener);
r5.addItemListener(myItemListener);
final ButtonGroup group = new ButtonGroup();
group.add(r1);
group.add(r2);
group.add(r3);
group.add(r4);
group.add(r5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
Container cont = frame.getContentPane();
cont.setLayout(new GridLayout(0, 1));
cont.add(new JLabel("Welcome to the CoinSorter, please choose from the options: "));
cont.add(r1);
cont.add(r2);
cont.add(r3);
cont.add(r4);
cont.add(r5);
frame.setVisible(true);
}
}
Aucun commentaire:
Enregistrer un commentaire