我有2个单选按钮,按顺序按a-> b-> a我希望输出像a(不打印任何东西) - > b(打印" N删除M") - > a(打印M删除N),但我得到的是(不打印任何东西) - > b(打印" N删除M") - > a(打印M删除M)。为什么会这样? =((顺便说一句,我是这里的新手,也是Java编程的初学者)
public GUI() {
militarytonorm = new JRadioButton("Military Time to Standard Time", false);
normtomilitary = new JRadioButton("Standard Time to Military Time", false);
add(militarytonorm);
add(normtomilitary);
group = new ButtonGroup();
group.add(militarytonorm);
group.add(normtomilitary);
checkButton ButtonChecker = new checkButton();
militarytonorm.addActionListener(ButtonChecker);
normtomilitary.addActionListener(ButtonChecker);
}
private class checkButton implements ActionListener {
public void actionPerformed(ActionEvent MTNButton) {
if (MTNButton.getSource() == militarytonorm) {
if (x == 1) {
try {
getContentPane().remove(Mtext);
getContentPane().remove(Minputhr);
getContentPane().remove(Minputmin);
getContentPane().remove(Minputsec);
getContentPane().remove(doneButton);
System.out.print("M removes M");
} catch (Exception error) {
getContentPane().remove(Ntext);
getContentPane().remove(Ninputhr);
getContentPane().remove(Ninputmin);
getContentPane().remove(Ninputsec);
getContentPane().remove(AMPM);
getContentPane().remove(NTMButton);
System.out.print("M removes N");
}
}
Mtext = new JLabel("input military hours, minutes and seconds");
add(Mtext);
Minputhr = new JFormattedTextField("00");
Minputhr.setColumns(2);
add(Minputhr);
Minputmin = new JFormattedTextField("00");
Minputmin.setColumns(2);
add(Minputmin);
Minputsec = new JFormattedTextField("00");
Minputsec.setColumns(2);
add(Minputsec);
doneButton = new JButton("Done");
add(doneButton);
MTNthehandler handler = new MTNthehandler();
doneButton.addActionListener(handler);
x = 1;
} else if (MTNButton.getSource() == normtomilitary) {
if (x == 1) {
try {
getContentPane().remove(Ntext);
getContentPane().remove(Ninputhr);
getContentPane().remove(Ninputmin);
getContentPane().remove(Ninputsec);
getContentPane().remove(AMPM);
getContentPane().remove(NTMButton);
System.out.print("N removes N");
} catch (Exception error) {
getContentPane().remove(Mtext);
getContentPane().remove(Minputhr);
getContentPane().remove(Minputmin);
getContentPane().remove(Minputsec);
getContentPane().remove(doneButton);
System.out.print("N removes M");
}
}
Ntext = new JLabel("input standard hours, minutes and seconds");
add(Ntext);
Ninputhr = new JFormattedTextField("00");
Ninputhr.setColumns(2);
add(Ninputhr);
Ninputmin = new JFormattedTextField("00");
Ninputmin.setColumns(2);
add(Ninputmin);
Ninputsec = new JFormattedTextField("00");
Ninputsec.setColumns(2);
add(Ninputsec);
AMPM = new JList(AMorPM);
AMPM.setVisibleRowCount(2);
AMPM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(AMPM));
selectList AMPMSelect = new selectList();
AMPM.addListSelectionListener(AMPMSelect);
NTMButton = new JButton("Done");
add(NTMButton);
NTMthehandler NTMhandler = new NTMthehandler();
NTMButton.addActionListener(NTMhandler);
x = 1;
}
}
}
答案 0 :(得分:0)
开始时的注意事项:这是非常复杂的代码 - 如果您将GUI基于选项卡式窗格(https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html)并使用一个标签“军事时间到标准时间”和第二个选项卡,则会更加简单“标准军事时间”标签。
解释您所看到的行为:
第一次按下“militarytonorm”单选按钮时,x
为0,因此代码不会尝试删除任何内容,也不会打印任何内容。然后创建一个JLabel,在Mtext中存储一个引用并将其添加到根窗格(再加上几个,但对于问题,这些不相关。)
如果您现在按下“normtomilitary”单选按钮,x
为1,那么您尝试从内容窗格中删除Ntext。由于Ntext未初始化,因此会抛出一个NullPointerException,它会被catch (Exception error)
捕获,代码会删除Mtext标签并打印“N removed M”。
到目前为止,一切都如你所愿。
<强> BUT 强>:
如果您现在按下“militarytonorm”单选按钮,x
为1,代码会尝试删除Mtext标签。由于Mtext是对JLabel实例的有效引用,因此代码不会抛出异常!所有getContentPane().remove(Mxxxx)
将不执行任何操作(因为上面的步骤2中已经删除了所有组件),然后打印出“M removed M”。