在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);
}
}
*「是這種類型的行爲可能與'的JOptionPane 「?」*當然。這和你在'JFrame'中做的事情一樣。如果你無法在框架中實現它,請發佈最佳嘗試的[SSCCE](http://sscce.org/)。 –
'JOptionPane'的消息參數需要一個'Object'。如果你傳遞一個'Component','JOptionPane'將把它用作「主」視圖,在它周圍添加圖標和按鈕 – MadProgrammer
如果你要回答你自己的問題,請發表你的答案作爲答案,並接受你的答案。 (這樣人們不會浪費時間來閱讀你的問題,希望能夠回答它) – Gus