【问题标题】:Get parameters from a popup box从弹出框中获取参数
【发布时间】:2012-01-13 10:29:04
【问题描述】:

我正在尝试实现类似的目标:

public void execute(){
 RandomClass item = showPopupBox(randomClassArrayList).selectItem();
 randomClassArrayList.remove(item);//Obv I can make this one line.
}

showPopupBox 将创建一个弹出框(如图),为列表中的每个项目填充单选按钮列表,并在您按下“确定”按钮时从列表中返回选定的项目。在此之前,execute 方法将等待弹出框从通过单选按钮选择的弹出框中返回项目。

我真的不能发布更多信息,因为如果可以,我就不需要问了。我正在尝试通过弹出框填充参数。

我的问题只是让execute()方法等待弹出框的OK按钮被按下,这将填充参数并完成execute()方法

【问题讨论】:

  • 您至少可以知道您在谈论什么 gui 技术! Web / Swing / AWT / SWT / 什么?
  • 您介意在发布之前用谷歌搜索您的问题吗? StackOverflow 已经多次包含这个问题!!!
  • adarshr 忘记加swing标签了,你冷静一下。此外,它没有具体说明,所以任何适合该问题的答案都是可以接受的答案。
  • Adel Boutros 那你为什么不给我链接。如果您不想提供帮助,为什么还要费心发表评论。没有答案,没有链接,没有帮助。是的,谢谢。

标签: java swing user-interface parameters popup


【解决方案1】:

你可以这样做(未经测试):

public static RandomClass showPopupBox(List<RandomClass> list)
{
    JRadioButton[] rdoArray = new JRadioButton[list.size()];
    ButtonGroup group = new ButtonGroup();
    JPanel rdoPanel = new JPanel();
    for(int i = 0; i < list.size(); i++)
    {
        rdoArray[i] = new JRadioButton(list.get(i).toString());
        group.add(rdoArray[i]);
        rdoPanel.add(rdoArray[i]);
    }
    rdoArray[0].setSelected(true);

    JOptionPane pane = new JOptionPane();
    int option = pane.showOptionDialog(null, rdoPanel, "The Title",
                                       JOptionPane.NO_OPTION,
                                       JOptionPane.PLAIN_MESSAGE,
                                       null, new Object[]{"Submit!"}, null);

    if(option == 0)
    {
        for(int i = 0; i < list.size(); i++)
            if(rdoArray[i].isSelected()) return list.get(i);
    }
    return null;
}

然后,你可以这样使用它:

RandomClass item = showPopupBox(randomClassArrayList);

【讨论】:

  • 那么你能告诉我是什么导致一切都在等待吗?我查看了 JOptionPane 类,尤其是 ShowOptionDialog 方法,我没有看到该方法如何延迟它的返回,直到它接收到输入,这几乎正是我想要的。
猜你喜欢
  • 2018-07-28
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多