简体   繁体   English

如何相对于另一个组件布局组件?

[英]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: 我想到了以下几点:

  1. 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
     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); 
    \n\n } }\n} }\n

    But it produces the following output where button isn't centered: 但它产生以下输出,其中按钮不居中:

  2. 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的位置的过程中,将由布局管理器定义,这是不希望的。

  3. Add jbutton to content pane and jlabel to glass pane. 将jbutton添加到内容窗格并将jlabel添加到glass窗格。 But I'll have the same problem with resizing as in previous case. 但是我会像以前的情况一样调整大小。

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.

相关问题 在相对布局中动态添加组件 - Dynamically adding component in relative layout 如何在 vaadin 中布局 Dialog 组件? - How to layout a Dialog component in vaadin? 如何知道某个组件是否相对于屏幕(而不是相对于其父对象)移动? - How to know if a component is moved relative to the screen (not relative to its parent)? 如果只渲染另一个组件,如何渲染组件? - How to render a component only if another component is not rendered? 如何在布局中移动组件? - How do I move a component in a layout? 如何在mig布局中进行组件定位 - How to make component positioning in mig layout 在不将布局从“自由设计”更改为另一个设计的情况下,将摇摆组件放置在另一个组件上? - putting a swing component on another component without changing the layout from Free design to another? (Gridbag布局)图形组件正在剪切到另一个网格中,不确定为什么 - (Gridbag Layout) Graphic component is clipping into another grid, not sure why 如何在位于相对布局管理器中的组件旁边绘制带有图像的弹出窗口? - Howto paint a popup with image beside a component being positioned in a relative layout manager? Vaadin中布局(或组件)的生命周期? - Lifecycle of a Layout (or Component) in Vaadin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM