如何相对于另一个组件布局组件?
我想要以下布局:
(即 jbutton 和 jlabel 放置在 jpanel 处,jbutton 居中,jlabel 紧随其后布局应该能够调整大小。编辑: jbutton 和 jlabel 的大小在调整大小期间保持不变。)
我考虑了以下内容:
使用适当的布局管理器。我一个也不认识。我可以将 jbutton 和 jlabel 添加到另一个 jpanel 并将这个新的 jpanel 添加到大 jpanel 的中心,但在这种情况下 jbutton 将不会居中。
编辑:我尝试使用此提案:(提案已编辑,代码为现在正确)
公共类 MainFrame 扩展 JFrame { 公共静态无效主(字符串[] args){ 新的主框架(); }
} }公共 MainFrame() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = 1; 添加(新的 JPanel(),c); c.gridx = 1; c.weightx = 0; c.anchor = GridBagConstraints.CENTER; 添加(新的 JButton(“按钮”),c); c.gridx = 2; c.weightx = 1; c.anchor = GridBagConstraints.WEST; 添加(新JLabel(“标签”),c); 盒(); 设置可见(真); setDefaultCloseOperation(EXIT_ON_CLOSE);
但它会产生以下输出,其中按钮未居中:
将 jbutton 和 jlabel 添加到 jpanel。将
ComponentListener
添加到 jpanel 并覆盖其中的componentResized
。重写方法将如下所示:public void componentResized(ComponentEvent e) { int x = 按钮.getX() + 按钮.getWidth() + 3; int y = 按钮.getY(); label.setLocation(new Point(x, y)); }
通过布局管理器自动调整大小时将选择 JButton 的位置,并通过此方法选择 jlabel 的位置。但
componentResized
仅在调整大小完成后才会被调用。因此,在调整大小的过程中,jlabel 的位置将由布局管理器定义,这是不需要的。- 将 jbutton 添加到内容窗格,将 jlabel 添加到玻璃窗格。但我在调整大小时会遇到与之前情况相同的问题。
我怎样才能实现这个布局?
I want to have the following layout:
(i.e. jbutton and jlabel are placed at jpanel, jbutton is centered and jlabel goes just after it. Layout should be able to resize. Edit: Size of jbutton and jlabel is constant during resizing.)
I thought about the following:
Use appropriate layout manager. I don't know one. I can add jbutton and jlabel to another jpanel and add this new jpanel to center of large jpanel but in that case jbutton won't be centered.
Edit: I tried to use this proposal: (proposal was edited and code is correct now there)
public class MainFrame extends JFrame { public static void main(String[] args) { new MainFrame(); }
public MainFrame() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = 1; add(new JPanel(), c); c.gridx = 1; c.weightx = 0; c.anchor = GridBagConstraints.CENTER; add(new JButton("Button"), c); c.gridx = 2; c.weightx = 1; c.anchor = GridBagConstraints.WEST; add(new JLabel("Label"), c); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}But it produces the following output where button isn't centered:
Add jbutton and jlabel to jpanel. Add
ComponentListener
to jpanel and overridecomponentResized
there. Overriden method will look like:public void componentResized(ComponentEvent e) { int x = button.getX() + button.getWidth() + 3; int y = button.getY(); label.setLocation(new Point(x, y)); }
JButton's position will be chosen when resizing automatically by layout manager and jlabel's position by this method. But
componentResized
will be invoked only after resizing will complete. So in process of resizing jlabel's position will be defined by layout manager which isn't desired.- Add jbutton to content pane and jlabel to glass pane. But I'll have the same problem with resizing as in previous case.
How can I achieve this layout?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用支持您的需求的 LayoutManager 是正确的方法。高级管理器允许定义大小始终等于宽度/高度的组(列/行)。如果核心管理器不允许这样的语义规范,请寻找第三方管理器,fi 任何 BigThree:MigLayout、FormLayout 或 DesignGridLayout。花在学习上的时间会很值得:-)
在MigLayout(目前我个人最喜欢的)中,约束是“sizegroup groupName”或“sg groupName”(如果只有on group,则groupName可以为空),fi
Use a LayoutManager which supports your need is the way to go. Advanced managers allow to define groups (of columns/rows) which are sized to equal width/height always. If core managers don't allow such semantic specifications, go for a third party manager, f.i. any of the BigThree: MigLayout, FormLayout or DesignGridLayout. The time invested in learning will be well spent :-)
In MigLayout (my personal favourite currently), the contraint is "sizegroup groupName" or "sg groupName" (where groupName can be empty if there's only on group), f.i.
看起来您可以使用具有 3 列的网格袋布局。第一列和第三列必须具有相同的权重,因此宽度也相同。中间的柱子没有重量,所以会坐在中间。我想为你尝试一下,但我是在 iPhone 上打字! HTH
请参阅评论,这是我对示例代码的编辑:
Looks like you could use a grid bag layout with 3 columns. The first and third column must have equal weight and therefore will be the same width. The middle column would have no weight so would sit in the middle. I would try it for you but I'm typing on an iPhone! HTH
See comments, here's my edit to your sample code: