【问题标题】:JOptionPane with custom array JButtons带有自定义数组 JButtons 的 JOptionPane
【发布时间】:2013-12-26 18:59:16
【问题描述】:

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

        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]);

如果我让单个按钮完美运行,但我需要在一个阵列上来控制其中的一些。

【问题讨论】:

  • 您刚刚声明了一个 JButton 数组,但从未实例化它们中的任何一个。你只需要说 option[0] = new JButton();选项[1] = 新的 JButton();在访问选项 [0] 之前,选项 [1]。你应该在 for 循环或其他东西中这样做......
  • 哦,是的......我的错......我忘了它xD

标签: java arrays swing button dialog


【解决方案1】:

在设置值之前,您必须像这样创建 JButton 对象:

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

在创建数组后写下这行代码。

【讨论】:

    【解决方案2】:
    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 {
    .......
    }
    

    ....

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多