java - 我不知道为什么我的按钮列表顶部有一个“确定”按钮

标签 java swing jbutton jscrollpane joptionpane

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

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;
}

最佳答案

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

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

如果您将该代码更改为:

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

enter image description here

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

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

No Ok Button

我希望这会有所帮助。

关于java - 我不知道为什么我的按钮列表顶部有一个“确定”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643688/

相关文章:

java - 编译器找不到类 Files 的方法

java - getResource 使用 java 1.7 windows 7 在磁盘名称之前放置一个前导/

java - jsp中的 'Page scope'是什么?

java - 可视化循环内 JPanel 的变化

java - JButtons 标签网格

java - 如何在 JButton ImageIcon 中使颜色不可见(透明)

java - 如何在Java swing中从另一个类禁用一个类中的多个JButton?

java - 如何在android studio中显示来自Firebase实时数据库的登录用户数据

java - 代码有时有效但有时无效(内存或线程问题)

java - JDialog实现Runnable不显示内容