如何将 JButton 添加到 GridLayout?
如果我有这样的代码:
class X extends JFrame
{
X()
{
setLayout(new GridLayout(3,3));
JButton b = new JButton("A-ha");
/*I would like to add this button in the center of this grid (2,2)*/
//How can I do it?
}
};
If I have code like so:
class X extends JFrame
{
X()
{
setLayout(new GridLayout(3,3));
JButton b = new JButton("A-ha");
/*I would like to add this button in the center of this grid (2,2)*/
//How can I do it?
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,您必须先填写所有以前的单元格。所以你需要先添加4个组件,然后才能添加中心组件。嗯,它可能是一个更好的布局管理器。
你想做什么?也许 BorderLayout 就是您正在寻找的?
As what I know, you have to fill all the previous cells before. So you need to add 4 components before you can add the center component. Hmm, it could be a better layoutmanager.
What are you trying to do? Maybe BorderLayout is what you are looking for?
您可能想看看 GridBag 布局
You might want to take a look at the GridBag Layout
这垂直和水平居中
This centres vertically and horizontally