Java 向JOptionPane添加其他自定义按钮

Java 向JOptionPane添加其他自定义按钮,java,user-interface,joptionpane,Java,User Interface,Joptionpane,我试图显示一个框架,该框架向用户显示一个项目列表,用户可以从中选择(从组合框菜单),用户现在可以选择ok来选择所选项目,或者按cancel,返回null。这些是给我的默认选项 以下是我的代码片段: Set<ICarOption> optionSet = spec.getCarOptions(category); // If the category is not mandatory, create a makeshift option object to skip. if(!cate

我试图显示一个框架,该框架向用户显示一个项目列表,用户可以从中选择(从组合框菜单),用户现在可以选择ok来选择所选项目,或者按cancel,返回null。这些是给我的默认选项

以下是我的代码片段:

Set<ICarOption> optionSet = spec.getCarOptions(category);
// If the category is not mandatory, create a makeshift option object to skip.
if(!category.getMandatory())    optionSet.add(new CarOption("skip"));
ICarOption[] optionArray = optionSet.toArray(new ICarOption[optionSet.size()]);

ICarOption selectedOption = (ICarOption)JOptionPane.showInputDialog(
                frame,
                "Choose one of the following options for Category " + category + ".\n" + 
                "If skip is available, you may choose it to skip this category.",
                "Select a feature",
                JOptionPane.QUESTION_MESSAGE, 
                null, 
                optionArray, 
                optionArray[0]);
Set optionstart=spec.getCarOptions(类别);
//如果类别不是必需的,请创建一个临时选项对象以跳过。
如果(!category.getMandatory())optionSet.add(新建CarOption(“跳过”);
iCaloption[]optionArray=optionAsit.toArray(新iCaloption[optionAsit.size());
i配置选择选项=(i配置)作业窗格。显示输入对话框(
框架
为类别“+类别+”\n“+选择下列选项之一
“如果可以跳过,您可以选择跳过此类别。”,
“选择一项功能”,
JOptionPane.QUESTION\u消息,
无效的
选择阵列,
optionArray[0]);
这段代码发生在for循环中,在类别上迭代,其中类别并不总是必需的。这意味着,如果我想允许用户有跳过类别的选项,我将在组合框中实现另一个名为skip的选项,如果选中了该选项,我将相应地跳过它。但这感觉像是一种肮脏的做事方式(跳过在我定义的iCarporation对象的意义上根本不是一个选项),我更希望有一个名为跳过的按钮,如果该类别是强制性的,并且在该类别不是强制性的情况下可用,则该按钮变灰(不可单击)

我在这里看到了一些例子:

这似乎表明我应该用自定义的按钮列表替换我的组合框,这不是我想要的。我需要有3个按钮(确定,跳过,取消),以及项目列表

更新: 要说明我的GUI的外观,请执行以下操作:

  • 框架制作完成
  • 在对话框窗口中,您会看到iCarporation对象的组合框(下拉列表)
  • 窗口上还有3个按钮:确定、跳过和取消
  • 如果类别为必填项,则SKIP将灰显
  • 如果选择“确定”,组合框中当前选定的项目将被赋予selectedOption变量
  • 如果选择了取消,则选择选项==null
  • 如果选择“跳过”,则将跳过此类别(继续;)
  • 这意味着在输入窗口中,我需要看到一个包含项目和3个按钮的组合框

    -删除子问题-

    更新2: 我刚刚意识到我也不能使用JButton,因为我需要在actionListener中执行很多操作,并且它要求变量是final,其中一些变量不能是final

    当前我的代码如下所示:

    JPanel panel = new JPanel();
    
    JComboBox<ICarOption> optionsBox = new JComboBox<ICarOption>();
    panel.add(optionsBox);
    for(ICarOption option : spec.getCarOptions(category)){
        optionsBox.addItem(option);
    }
    
    Object[] options = { "Select option", "Skip", "Cancel" };
    
    int selected = JOptionPane.showOptionDialog(
                    panel,
                    "Choose one of the following options for Category " + category + ".\n" + 
                    "If skip is available, you may choose it to skip this category.",
                    "Select option",
                    JOptionPane.YES_NO_CANCEL_OPTION, 
                    JOptionPane.INFORMATION_MESSAGE, null, 
                    options, 
                    options[0]);
    
    if(selected == JOptionPane.NO_OPTION)   continue;
    if(selected == JOptionPane.CANCEL_OPTION)   throw new UnavailableException();
    if(selected == JOptionPane.YES_OPTION){
        ...
    }
    
    JPanel面板=新的JPanel();
    JComboBox optionsBox=新的JComboBox();
    面板。添加(选项框);
    对于(iCaloption选项:spec.getCarOptions(类别)){
    选项框。附加项(选项);
    }
    对象[]选项={“选择选项”、“跳过”、“取消”};
    int selected=JOptionPane.showOptionDialog(
    面板
    为类别“+类别+”\n“+选择下列选项之一
    “如果可以跳过,您可以选择跳过此类别。”,
    “选择选项”,
    JOptionPane.YES\u NO\u CANCEL\u选项,
    JOptionPane.INFORMATION_消息,null,
    选项,
    期权[0]);
    如果(所选==JOptionPane.NO_选项)继续;
    if(selected==JOptionPane.CANCEL_选项)抛出新的不可用异常();
    如果(所选==JOptionPane.YES\u选项){
    ...
    }
    
    灵感来自:

    问题是我现在没有办法控制跳过按钮,因为它是在创建窗口的那一刻创建的

    更新3: 它现在起作用了,但我对自己的表现并不感到自豪

            JPanel panel = new JPanel();
    
            JComboBox<ICarOption> optionsBox = new JComboBox<ICarOption>();
            panel.add(optionsBox);
            for(ICarOption option : spec.getCarOptions(category)){
                optionsBox.addItem(option);
            }
    
            int selected;
            if(!category.getMandatory()){
                Object[] options = { "Select option", "Cancel", "Skip" };
    
                selected = JOptionPane.showOptionDialog(
                        panel,
                        "Choose one of the following options for Category " + category + ".\n" + 
                        "If skip is available, you may choose it to skip this category.",
                        "Select option",
                        JOptionPane.YES_NO_CANCEL_OPTION, 
                        JOptionPane.INFORMATION_MESSAGE, null, 
                        options, 
                        options[0]);
            }
            else{
                Object[] options = { "Select option", "Cancel" };
    
                selected = JOptionPane.showOptionDialog(
                        panel,
                        "Choose one of the following options for Category " + category + ".\n" + 
                        "If skip is available, you may choose it to skip this category.",
                        "Select option",
                        JOptionPane.YES_NO_OPTION, 
                        JOptionPane.INFORMATION_MESSAGE, null, 
                        options, 
                        options[0]);
            }
    
            // careful! CANCEL_OPTION means skip has been pressed and NO_OPTION means cancel
            if(selected == JOptionPane.CANCEL_OPTION)   continue;
            if(selected == JOptionPane.NO_OPTION)   throw new UnavailableException();
            if(selected == JOptionPane.YES_OPTION){
                ...
            }
    
    JPanel面板=新的JPanel();
    JComboBox optionsBox=新的JComboBox();
    面板。添加(选项框);
    对于(iCaloption选项:spec.getCarOptions(类别)){
    选项框。附加项(选项);
    }
    选择int;
    如果(!category.getMandatory()){
    对象[]选项={“选择选项”、“取消”、“跳过”};
    选定项=JOptionPane.showOptionDialog(
    面板
    为类别“+类别+”\n“+选择下列选项之一
    “如果可以跳过,您可以选择跳过此类别。”,
    “选择选项”,
    JOptionPane.YES\u NO\u CANCEL\u选项,
    JOptionPane.INFORMATION_消息,null,
    选项,
    期权[0]);
    }
    否则{
    对象[]选项={“选择选项”,“取消”};
    选定项=JOptionPane.showOptionDialog(
    面板
    为类别“+类别+”\n“+选择下列选项之一
    “如果可以跳过,您可以选择跳过此类别。”,
    “选择选项”,
    JOptionPane.YES\u NO\u选项,
    JOptionPane.INFORMATION_消息,null,
    选项,
    期权[0]);
    }
    //小心!取消选项表示跳过已按下,无选项表示取消
    如果(所选==JOptionPane.CANCEL_选项)继续;
    if(selected==JOptionPane.NO_选项)抛出新的不可用异常();
    如果(所选==JOptionPane.YES\u选项){
    ...
    }
    
    这里肯定有大量重复的代码,但这是一种比将skip作为对象传递更好的方式

    更新4: 将复制零件更改为以下内容:

            ArrayList<Object> tempList = new ArrayList<Object>();
            int optionType;
    
            tempList.add("Select option");
            tempList.add("Cancel");
            if(!category.getMandatory()){
                tempList.add("Skip");
                optionType = JOptionPane.YES_NO_CANCEL_OPTION;
            }
            else    optionType = JOptionPane.YES_NO_OPTION;
    
            Object[] options = tempList.toArray(new Object[tempList.size()]);
    
            int selected = JOptionPane.showOptionDialog(
                    panel,
                    "Choose one of the following options for Category " + category + ".\n" + 
                            "If skip is available, you may choose it to skip this category.",
                            "Select option",
                            optionType, 
                            JOptionPane.INFORMATION_MESSAGE, null, 
                            options, 
                            options[0]);
    
    ArrayList tempList=new ArrayList();
    int optionType;
    添加(“选择选项”);
    圣殿骑士。加上(“巨蟹座”)
    
    Object[] options = { "OK", "CANCEL" };
    Object answer = JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
    JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
    null, options, options[0]);
    
    switch ((String) answer){
    case "OK":
        //do stuff
    
        JButton jbt_ok = new JButton("OK");
        JButton jbt_skip = new JButton("Skip");
        JButton jbt_cancel = new JButton("Cancel");
    
        boolean greyOutSkipButton = true;
    
        jbt_ok.addMouseListener(new MouseAdapter() {
    
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("OK was clicked");
    
            }
        });
    
        jbt_cancel.addMouseListener(new MouseAdapter() {
    
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Cancel was clicked");
    
            }
        });
    
        if(greyOutSkipButton)
            jbt_skip.setEnabled(false);
        else
    
            jbt_skip.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Skip was clicked");
    
                }
            });
    
        Object[] options = {jbt_ok, jbt_skip, jbt_cancel};
    
        JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
                JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                null, options, options[0]);