[英]How to layout component relative to another component?
I want to have the following layout: 我想要以下布局:
(ie 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.) (即jbutton和jlabel放在jpanel,jbutton居中,jlabel就在它之后。布局应该能够调整大小。 编辑: jbutton和jlabel的大小在调整大小时是不变的。)
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. 我可以将jbutton和jlabel添加到另一个jpanel并将这个新的jpanel添加到大型jpanel的中心,但在这种情况下,jbutton将不会居中。
Edit: I tried to use this proposal: (proposal was edited and code is correct now there) 编辑:我尝试使用此提案:(提案已编辑,代码现在正确)
public class MainFrame extends JFrame { 公共类MainFrame扩展JFrame {\n public static void main(String[] args) { public static void main(String [] args){\n new MainFrame(); 新的MainFrame();\n } }\n\n\n\n } }\n} }\npublic 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. 将jbutton和jlabel添加到jpanel。 Add ComponentListener
to jpanel and override componentResized
there. 将ComponentListener
添加到jpanel并覆盖componentResized
。 Overriden method will look like: Overriden方法如下:
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. 当布局管理器自动调整大小时,将选择JButton的位置,并通过此方法调整jlabel的位置。 But componentResized
will be invoked only after resizing will complete. 但只有在调整大小完成后才会调用componentResized
。 So in process of resizing jlabel's position will be defined by layout manager which isn't desired. 因此,在调整jlabel的位置的过程中,将由布局管理器定义,这是不希望的。
How can I achieve this layout? 我怎样才能实现这种布局?
Looks like you could use a grid bag layout with 3 columns. 看起来你可以使用3列的网格包布局。 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! 我会为你尝试,但我在iPhone上打字! HTH HTH
See comments, here's my edit to your sample code: 请参阅注释,这是我对示例代码的编辑:
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;
Component panel = new JPanel();
panel.setBackground(Color.blue);
add(panel, 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;
JLabel label = new JLabel("Label");
add(label, c);
panel.setPreferredSize(label.getPreferredSize());
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Use a LayoutManager which supports your need is the way to go. 使用支持您需要的LayoutManager是可行的方法。 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, fi any of the BigThree: MigLayout, FormLayout or DesignGridLayout. 如果核心管理者不允许这样的语义规范,那么请选择第三方管理员,任何BigThree:MigLayout,FormLayout或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), fi 在MigLayout(我个人最喜欢的)中,约束是“sizegroup groupName”或“sg groupName”(如果只有组,groupName可以为空),fi
JButton button = new JButton("somesize");
JLabel label = new JLabel("--outer--");
MigLayout layout = new MigLayout("debug, wrap 3",
"[sizegroup, grow][center][sizegroup, grow]");
JComponent comp = new JPanel(layout);
// start adding component in second column
comp.add(button, "cell 1 0");
comp.add(label);
// add something spanning all column in second row for comparison
comp.add(new JButton("somesize"), "span 3, center");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.