【问题标题】:i do not know why i get an OK button at the top of my list of Buttons我不知道为什么我的按钮列表顶部有一个确定按钮
【发布时间】:2019-08-25 06:18:00
【问题描述】:

我在 JscrollPane 中的 JOptionDialog 中有一些 JButton。 OK 按钮神奇地出现在按钮的顶部。我不知道为什么,或者如何杀死它。如果我将 JScrollPane 插入 JPanel,则不会出现顶部的 OK 按钮。有人有想法吗??

public int search() {
    JFrame searchFrame = new JFrame("Search Frame");
    String[] paneOptions = { "" };
    JOptionPane searchPane = new JOptionPane();
    searchPane.setMessage("");
    JScrollPane scrollPane = new JScrollPane(searchPane);
    scrollPane.setBounds(50, 50, 200, 400);
    scrollPane.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK));
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    JButton Button1 = new JButton("Button 1");
    Button1.setBounds(10, 40, 150, 30);
    JButton Button2 = new JButton("Button 2");
    Button2.setBounds(10, 70, 150, 30);
    JButton Button3 = new JButton("Button 3");
    Button3.setBounds(10, 100, 150, 30);
    Button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonNo = 3;
        }
    });
    JButton Button4 = new JButton("Button 4");
    Button4.setBounds(10, 130, 150, 30);
    JButton Button5 = new JButton("Button 5");
    Button5.setBounds(10, 160, 150, 30);
    JButton Button6 = new JButton("Button 6");
    Button6.setBounds(10, 190, 150, 30);
    JButton Button7 = new JButton("Button 7");
    Button7.setBounds(10, 220, 150, 30);
    JButton Button8 = new JButton("Button 8");
    Button8.setBounds(10, 220, 150, 30);
    JButton Button9 = new JButton("Button 9");
    Button9.setBounds(10, 220, 150, 30);
    JButton Button10 = new JButton("Button 10");
    Button10.setBounds(10, 220, 150, 30);

    // searchPane.add( Button1 );
    searchPane.add(Button1);
    searchPane.add(Button2);
    searchPane.add(Button3);
    searchPane.add(Button4);
    searchPane.add(Button5);
    searchPane.add(Button6);
    searchPane.add(Button7);
    searchPane.add(Button8);
    searchPane.add(Button9);
    searchPane.add(Button10);

    scrollPane.setPreferredSize(new Dimension(300, 125));

    String[] options = { "OK", "Cancel" };
    int selectedOption = JOptionPane.showOptionDialog(null, scrollPane, "The Title", 0, -1, null, options, options[0]);

    return buttonNo;
}

【问题讨论】:

    标签: java swing jbutton jscrollpane joptionpane


    【解决方案1】:

    啊,我花了一些时间才弄清楚为什么你会得到这个确定按钮。复制代码并运行后,我发现顶部有那个OK按钮是正常的。您正在构建一个 JOptionPane 类型的新对象 searchPane

    JOptionPane searchPane = new JOptionPane();
    searchPane.setMessage("");
    

    如果您将该代码更改为:

    JOptionPane searchPane = new JOptionPane();
    searchPane.setMessage("This Text belongs to the searchPane instance of type JOptionPane");
    

    您仍然会在顶部显示“确定”按钮,但在按钮顶部会显示新的文本消息。那只是为了向您展示 JOptionPane 的作用。现在要回答您的问题并完全删除 OK 按钮(它是 JOptionPane 的一部分),您需要删除 JOptionPane。为此,您可以修改代码以包含可以充当按钮容器的不同组件。像这样:

    JPanel searchPane = new JPanel();
    searchPane.setLayout(new BoxLayout(searchPane, BoxLayout.PAGE_AXIS));
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这是因为您的代码创建了两个JOptionPanes:

      第一个 JOptionPane 在第 4 行显式创建:

          JOptionPane searchPane = new JOptionPane();
          searchPane.setMessage("");
      

      第一个JOptionPane 是使用默认选项创建的,这意味着您将获得默认的OK 按钮。

      第二个JOptionPane 在方法结束时为您隐式创建:

          int selectedOption = JOptionPane.showOptionDialog(null, scrollPane, "The Title", 0, -1, null, options, options[0]);
      

      第二个JOptionPane 具有您指定的按钮(确定、取消)


      要解决您的问题,您应该使用JOptionPane 作为按钮的容器。

      改为使用JPanel 作为容器:

          JPanel searchPane = new JPanel();
          searchPane.setLayout(new BoxLayout(searchPane));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-13
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 2021-10-29
        • 2019-08-03
        • 1970-01-01
        相关资源
        最近更新 更多