【问题标题】:Force JOptionPane to Stay Open强制 JOptionPane 保持打开状态
【发布时间】:2013-01-27 02:48:30
【问题描述】:

我的应用程序构造如下:

  • 主窗口允许用户选择要解析的 CSV 文件
  • 选择 CSV 文件后会出现 JOptionPane,并且 JOptionPane 包含一个带有各种选项的下拉菜单;每个都生成一个单独的窗口
  • 目前,从菜单中进行选择并单击“确定”按钮后,JOptionPane 将关闭

我正在寻找一种方法来强制 JOptionPane 保持打开状态,以便用户可以根据需要选择不同的东西。我希望仅通过单击右上角的“X”来关闭 JOptionPane。如果使用 JOptionPane 不是最好的方法,我也愿意接受其他可能性来实现类似的结果。

这是我正在处理的相关代码块:

try 
{
    CSVReader reader = new CSVReader(new FileReader(filePath), ',');

    // Reads the complete file into list of tokens.
    List<String[]> rowsAsTokens = null;

    try 
    {
        rowsAsTokens = reader.readAll();
    } 

    catch (IOException e1) 
    {
        e1.printStackTrace();
    }

    String[] menuChoices = { "option 1", "option 2", "option 3" };

    String graphSelection = (String) JOptionPane.showInputDialog(null, 
            "Choose from the following options...", "Choose From DropDown", 
            JOptionPane.QUESTION_MESSAGE, null, 
            menuChoices, // Array of menuChoices
            menuChoices[0]); // Initial choice

    String menuSelection = graphSelection;

    // Condition if first item in drop-down is selected
    if (menuSelection == menuChoices[0] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option1();          
    }

    if (menuSelection == menuChoices[1] && graphSelection != null)
    {

        log.append("Generating graph: " + graphSelection + newline);

        option2();      
    }

    if (menuSelection == menuChoices[2] && graphSelection != null)
    {
        log.append("Generating graph: " + graphSelection + newline);

        option3();
    }

    else if (graphSelection == null)
    {   
        log.append("Cancelled." + newline);
    }
}

【问题讨论】:

  • 请发布您的代码。
  • 将下拉菜单放在不同的 jframe 中而不是选项窗格中可能会更好,这将为您提供更多行为选项
  • 我还注意到您正在使用 == 比较 Strings。这不是在Java 中执行此操作的方法。您应该改用equals() 方法:menuSelection.equals(menuChoice[0])
  • @Michael - 为什么优选 equals() 方法?
  • @THEDOCTOR 在Java 中,== 表示法用于比较原语,例如int a = 4; int b = 4; return a == b; 将导致 TRUE。如果您尝试使用== 比较两个Objects (如Strings),它将测试Objects 是否相同(或者,准确地说,它将测试reference 是否相同) .例如String c = "Test"; String d = "Test"; return c == d; 将导致 False。但是return c.equals(d); 将导致True

标签: java swing user-interface drop-down-menu joptionpane


【解决方案1】:

我希望带有选项的窗口即使在之后仍保持打开状态 用户选择了一个选项,以便他们可以选择另一个选项 如果他们愿意。如何让 JOptionPane 保持打开状态而不是 它的默认行为,一旦下拉值是它就会关闭 选择了吗?

【讨论】:

    【解决方案2】:

    在这些选项窗格中的任何一个中,我都可以在关闭它之前多次更改我的选择。第三个选项窗格将显示(默认为)在第一个选项中选择的值 - 当前值。

    import java.awt.*;
    import javax.swing.*;
    
    class Options {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    Object[] options = {
                        "Option 1",
                        "Option 2",
                        "Option 3",
                        "None of the above"
                    };
                    JComboBox optionControl = new JComboBox(options);
                    optionControl.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(null, optionControl, "Option",
                            JOptionPane.QUESTION_MESSAGE);
                    System.out.println(optionControl.getSelectedItem());
    
                    String graphSelection = (String) JOptionPane.showInputDialog(
                            null,
                            "Choose from the following options...", 
                            "Choose From DropDown",
                            JOptionPane.QUESTION_MESSAGE, null,
                            options, // Array of menuChoices
                            options[3]); // Initial choice
                    System.out.println(graphSelection);
    
                    // show the combo with current value!
                    JOptionPane.showMessageDialog(null, optionControl, "Option",
                            JOptionPane.QUESTION_MESSAGE);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
            SwingUtilities.invokeLater(r);
        }
    }
    

    我认为迈克尔猜对了JList。这是listcombo 之间的比较。

    请注意,JListJComboBox 都可以使用组合中的渲染器。重要的区别在于列表是一个支持多选的嵌入式组件。

    【讨论】:

    • “我希望带有选项的窗口保持打开状态” 如果“窗口”是指选项窗格 - 它确实保持打开状态。如果您的意思是组合下拉菜单,它是下拉菜单,不是窗口!
    【解决方案3】:

    以下解决方案不会为您提供下拉菜单,但允许您选择多个值。

    您可以使用JList 来存储您的选择并像这样使用JOptionPane.showInputMessage

    JList listOfChoices = new JList(new String[] {"First", "Second", "Third"});
    JOptionPane.showInputDialog(null, listOfChoices, "Select Multiple Values...", JOptionPane.QUESTION_MESSAGE);
    

    JOptionPane.showInputDialog() 之后在listOfChoices 上使用getSelectedIndices() 方法将返回一个整数数组,其中包含从JList 中选择的索引,您可以使用ListModel 来获取它们的值:

    int[] ans = listOfChoices.getSelectedIndices();
    ListModel listOfChoicesModel = listOfChoices.getModel();
    for (int i : ans) {
        System.out.println(listOfChoicesModel.getElementAt(i));
    }
    

    【讨论】:

    • 一看到你的回答,我就听到一分钱的下降。我认为你已经得到了 OP 实际需要的东西。
    • @AndrewThompson 谢谢 :) 他想要一个下拉列表...这不一样,但如果他没有很多值,这可以解决问题。
    • 虽然这允许用户选择多个值,但一旦单击“确定”按钮,JOptionPane 仍会关闭。我希望用户即使在初始选择之后也能继续选择项目。
    • @THEDOCTOR 是的,用户可以使用ctrl 按钮加上left mouse button 选择多个值。
    • 我看到了,但我的观点是在您单击“确定”后关闭,我不希望它关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多