使用子菜单创建 GUI
Creating GUI with sub menus
我正在尝试构建一个具有子菜单的 GUI,并且可能 return 到主菜单和选项,到目前为止我已经创建了一个主菜单但是当我尝试单击框时也没有打开任何东西当我编码 5 个项目时,显示 4 个项目。有谁知道解决这个问题的方法吗?另外,如果有人知道如何创建不同的框而不是复选框。非常感谢
public class CoinSorterGUI {
public static void main(String[] args) {
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.INFORMATION_MESSAGE;
message = "Welcome to the CoinCalculator";
}else if (command.equals("Multiple CoinCalculator")) {
messageType = JOptionPane.INFORMATION_MESSAGE;
message = "Welcome to the Multiple CoinCalculator";
}else if (command.equals("Print Coin List")) {
messageType = JOptionPane.INFORMATION_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 configuarations")) {
messageType = JOptionPane.INFORMATION_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);
}
}
}
JRadioButton r1 = new JRadioButton("CoinCalculator");
r1.setActionCommand("Welcome to the CoinCalculator");
JRadioButton r2 = new JRadioButton("Multiple CoinCalculator");
r2.setActionCommand("Welcome to the Multiple CoinCalculator");
JRadioButton r3 = new JRadioButton("Print Coin List");
r3.setActionCommand("Current coin denominations are; £2, £1, 50p. 20p and 10p");
JRadioButton r4 = new JRadioButton("Set details");
r4.setActionCommand(" ** Set details Sub Menu ** "
+ " 1 - Set Currency "
+ " 2 - Set minimum input value"
+ " 3 - Set Maximum input value"
+ " 4 - Return to main menu");
JRadioButton r5 = new JRadioButton("Display program configuarations");
r5.setActionCommand("The current currency is £, the minimum input value is 0 and the maximum input value is 10000");
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(300, 200);
Container cont = frame.getContentPane();
cont.setLayout(new GridLayout(0, 1));
cont.add(new JLabel("Please choose from the options: "));
cont.add(r1);
cont.add(r2);
cont.add(r3);
cont.add(r4);
frame.setVisible(true);
}
}
only 4 items are showing when I have coded 5 items
cont.add(r4);
frame.setVisible(true);
应该是:
cont.add(r4);
cont.add(r5); // add ALL the buttons!
frame.setVisible(true);
我正在尝试构建一个具有子菜单的 GUI,并且可能 return 到主菜单和选项,到目前为止我已经创建了一个主菜单但是当我尝试单击框时也没有打开任何东西当我编码 5 个项目时,显示 4 个项目。有谁知道解决这个问题的方法吗?另外,如果有人知道如何创建不同的框而不是复选框。非常感谢
public class CoinSorterGUI {
public static void main(String[] args) {
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.INFORMATION_MESSAGE;
message = "Welcome to the CoinCalculator";
}else if (command.equals("Multiple CoinCalculator")) {
messageType = JOptionPane.INFORMATION_MESSAGE;
message = "Welcome to the Multiple CoinCalculator";
}else if (command.equals("Print Coin List")) {
messageType = JOptionPane.INFORMATION_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 configuarations")) {
messageType = JOptionPane.INFORMATION_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);
}
}
}
JRadioButton r1 = new JRadioButton("CoinCalculator");
r1.setActionCommand("Welcome to the CoinCalculator");
JRadioButton r2 = new JRadioButton("Multiple CoinCalculator");
r2.setActionCommand("Welcome to the Multiple CoinCalculator");
JRadioButton r3 = new JRadioButton("Print Coin List");
r3.setActionCommand("Current coin denominations are; £2, £1, 50p. 20p and 10p");
JRadioButton r4 = new JRadioButton("Set details");
r4.setActionCommand(" ** Set details Sub Menu ** "
+ " 1 - Set Currency "
+ " 2 - Set minimum input value"
+ " 3 - Set Maximum input value"
+ " 4 - Return to main menu");
JRadioButton r5 = new JRadioButton("Display program configuarations");
r5.setActionCommand("The current currency is £, the minimum input value is 0 and the maximum input value is 10000");
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(300, 200);
Container cont = frame.getContentPane();
cont.setLayout(new GridLayout(0, 1));
cont.add(new JLabel("Please choose from the options: "));
cont.add(r1);
cont.add(r2);
cont.add(r3);
cont.add(r4);
frame.setVisible(true);
}
}
only 4 items are showing when I have coded 5 items
cont.add(r4);
frame.setVisible(true);
应该是:
cont.add(r4);
cont.add(r5); // add ALL the buttons!
frame.setVisible(true);