我的jbuttons(“搜索按钮”)怎么不会出现在jpanel上?

发布于 2025-02-06 19:56:21 字数 1954 浏览 2 评论 0原文

即时通讯构建库存GUI应用程序,我希望用描述的第一字母搜索这些项目。因此,我需要在jpanel上显示26个小按钮(将是字母的字母)。然后使用搜索按钮对应于保存的文件。但是我似乎甚至无法显示按钮???它只是jpanel。我在这里只是整个程序的片段。只是强调其中的jpanel和jbutton。

JPanel searchPanel = new JPanel( );
searchPanel.setPreferredSize(new Dimension(240, 160));
searchPanel.setBorder(BorderFactory.createTitledBorder("Item Search")); 
searchPanel.setBounds(149, 295, 205, 94);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 7;
gridConstraints.gridwidth = 3;
gridConstraints.insets = new Insets(10, 0, 10, 0);
gridConstraints.anchor = GridBagConstraints.CENTER;
getContentPane().add(searchPanel, gridConstraints);
layeredPane.setLayer(searchPanel, 2);
layeredPane.add(searchPanel, gridConstraints); 

 
int x = 0, y = 0;
    // Create Button and Position of Search Panel
    for (int i = 0; i < 26; i++)
    {
        JButton[ ] searchBtn = new JButton[26];
        searchBtn[i] = new JButton( );
        searchBtn[i].setText(String.valueOf((char) (65 + i)));
        searchBtn[i].setFont(new Font("Arial", Font.BOLD, 12));
        searchBtn[i].setMargin(new Insets(-10, -10, -10, -10));
        searchBtn[i].setBounds(149, 295, i, i);
        sizeButton(searchBtn[i], new Dimension(37, 27));
        searchBtn[i].setBackground(Color.YELLOW);
        searchBtn[i].setFocusable(false);
        gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = x;
        gridConstraints.gridy = y;
        searchPanel.add(searchBtn[i], gridConstraints);
        //Search Buttons Method
        searchBtn[i].addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e)
            {
                searchBtnActionPerformed(e);
            }
            });
        x++;
        //6 Buttons Per Row
        if (x % 6 == 0)
        {
            x = 0;
            y++;
        }
        }
         
         getContentPane( ).setLayout(getLayout( ));
 }

Im building a inventory Gui application and I want the items to be searched with the 1st letter of the description. So, I need the 26 small buttons (which will be letters of the alphabet) to show on the JPanel. Then use the search button to correspond to the saved files. But I cant seem to get the buttons to even show??? It just the JPanel. What I have here is just a snippet of the whole program. Just highlighting the JPanel and JButtons that are inside it.

JPanel searchPanel = new JPanel( );
searchPanel.setPreferredSize(new Dimension(240, 160));
searchPanel.setBorder(BorderFactory.createTitledBorder("Item Search")); 
searchPanel.setBounds(149, 295, 205, 94);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 7;
gridConstraints.gridwidth = 3;
gridConstraints.insets = new Insets(10, 0, 10, 0);
gridConstraints.anchor = GridBagConstraints.CENTER;
getContentPane().add(searchPanel, gridConstraints);
layeredPane.setLayer(searchPanel, 2);
layeredPane.add(searchPanel, gridConstraints); 

 
int x = 0, y = 0;
    // Create Button and Position of Search Panel
    for (int i = 0; i < 26; i++)
    {
        JButton[ ] searchBtn = new JButton[26];
        searchBtn[i] = new JButton( );
        searchBtn[i].setText(String.valueOf((char) (65 + i)));
        searchBtn[i].setFont(new Font("Arial", Font.BOLD, 12));
        searchBtn[i].setMargin(new Insets(-10, -10, -10, -10));
        searchBtn[i].setBounds(149, 295, i, i);
        sizeButton(searchBtn[i], new Dimension(37, 27));
        searchBtn[i].setBackground(Color.YELLOW);
        searchBtn[i].setFocusable(false);
        gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = x;
        gridConstraints.gridy = y;
        searchPanel.add(searchBtn[i], gridConstraints);
        //Search Buttons Method
        searchBtn[i].addActionListener(new ActionListener( ) {
            public void actionPerformed(ActionEvent e)
            {
                searchBtnActionPerformed(e);
            }
            });
        x++;
        //6 Buttons Per Row
        if (x % 6 == 0)
        {
            x = 0;
            y++;
        }
        }
         
         getContentPane( ).setLayout(getLayout( ));
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2025-02-13 19:56:21

因此,我采用了您的代码,以同时尝试将其同时添加两个不同的容器,并产生...

“在此处输入映像”

现在,我很确定这不是您想要的,基于我对您的代码的有限理解(这是代码处于这样的时刻之一,甚至不值得尝试挽救),但是,似乎您显然不明确地不了解如何使用布局经理。

您应该花时间查看在容器

所以,我扔掉了你的代码,只是做了...

”在此处输入图像说明”

public class SearchPane extends JPanel {

    public SearchPane() {
        setBorder(BorderFactory.createTitledBorder("Item Search"));
        setLayout(new GridLayout(-1, 6));

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                JOptionPane.showMessageDialog(SearchPane.this, "You want to search for [" + cmd + "]", "Search", JOptionPane.PLAIN_MESSAGE);
            }
        };

        for (int index = 0; index < 26; index++) {
            JButton btn = new JButton(Character.toString('A' + index));
            btn.addActionListener(listener);
            add(btn);
        }
    }
}

So, I took your code, corrected for the fact that you're trying add it two different containers simultaneously and it produced...

enter image description here

Now, I'm pretty sure this isn't what you wanted, based on my limited understanding of your code (this is one of those moments where the code is in such a state that it's not even worth trying to salvage), but, it would seem that you clearly don't understand how to use layout managers.

You should make the time to look over Laying Out Components Within a Container

So, I threw out your code and simply did...

enter image description here

public class SearchPane extends JPanel {

    public SearchPane() {
        setBorder(BorderFactory.createTitledBorder("Item Search"));
        setLayout(new GridLayout(-1, 6));

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                JOptionPane.showMessageDialog(SearchPane.this, "You want to search for [" + cmd + "]", "Search", JOptionPane.PLAIN_MESSAGE);
            }
        };

        for (int index = 0; index < 26; index++) {
            JButton btn = new JButton(Character.toString('A' + index));
            btn.addActionListener(listener);
            add(btn);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文