JOptionPane与自定义数组JButtons

时间:2013-12-26 18:59:16

标签: java arrays swing button dialog

我有这个代码,但它不适用于按钮数组

        JButton[] option = new JButton[2];
        option[0].setText("sad");
        option[0].setEnabled(true);
        option[1].setText("sasdd");
        option[1].setEnabled(true);
        Object[] options = {option[0], option[1]};
        int i = JOptionPane.showOptionDialog(null, "SADASD", "dfgdfgg", 0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

如果我使单个按钮工作完美,但我需要在阵列上控制其中一些。

2 个答案:

答案 0 :(得分:2)

在设置值之前,您必须创建如下的JButton对象:

option[0] = new JButton();
option[1] = new JButton();

在创建数组后立即写下这一行。

答案 1 :(得分:0)

String[] options = {"Java", "C", "C++", "C#"};
String msg = "What is your favourite language";
String title = "Language Poll";
int result = JOptionPane.showOptionDialog(parentComponent, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(result == options[0] {
    //do some thing
} else {
.......
}

...