我的jbuttons(“搜索按钮”)怎么不会出现在jpanel上?
即时通讯构建库存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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我采用了您的代码,以同时尝试将其同时添加两个不同的容器,并产生...
现在,我很确定这不是您想要的,基于我对您的代码的有限理解(这是代码处于这样的时刻之一,甚至不值得尝试挽救),但是,似乎您显然不明确地不了解如何使用布局经理。
您应该花时间查看在容器
所以,我扔掉了你的代码,只是做了...
So, I took your code, corrected for the fact that you're trying add it two different containers simultaneously and it produced...
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...