在elective.addActionListener
声明中我调用了错误的变量,cp12
而不是cp6
,在下面发布了我的缩减代码,一切都很好,谢谢
public void displayAddSomething(ArrayList<String> items)
{
// cut down version only showing the button panels
// initialising the variables
JPanel buttPanel = new JPanel(new GridLayout(0, 1));
JPanel pointPanel = new JPanel(new GridLayout(0, 1));
JRadioButton core = new JRadioButton("Core Item: ", true);
final JRadioButton elective = new JRadioButton("Elective Item: ");
final JRadioButton cp6 = new JRadioButton("6 Points: ");
JRadioButton cp12 = new JRadioButton("12 Points: ", true);
ButtonGroup bg1 = new ButtonGroup();
ButtonGroup bg2 = new ButtonGroup();
// adding the buttons to buttPanel and the button group 1
buttPanel.add(new JLabel("Select Course Type"));
buttPanel.add(core);
buttPanel.add(elective);
buttPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
bg1.add(core);
bg1.add(elective);
// add buttons pointPanel and bg2
pointPanel.add(new JLabel("Select Credit Points"));
pointPanel.add(cp6);
pointPanel.add(cp12);
pointPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
bg2.add(cp6);
bg2.add(cp12);
cp6.setEnabled(false);
// add action listener for each of bg1 buttons so if event
// occurs the cp6 button will toggle between enabled and disabled.
elective.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cp6.setEnabled(true);
}});
core.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cp6.setEnabled(false);
}});
Object[] message = {buttPanel,pointPanel};
int result = JOptionPane
.showOptionDialog(
this,
message,
"...some text...",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, new String[] { "Submit", "Cancel" }, "Default");
if (result == JOptionPane.OK_OPTION)
{
// collecting all the input values in a string array to pass to
// another class for processing by the model...
}
else if (result == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(this,
"You Have Elected Not to do anything At This Time",
"YOU HAVE COME THIS FAR AND YOU QUIT NOW....",
JOptionPane.QUESTION_MESSAGE);
}
}