首页 文章

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

提问于
浏览
0

我想要以下布局:

(即jbutton和jlabel放在jpanel,jbutton居中,jlabel就在它之后 . 布局应该能够调整大小. Edit: jbutton和jlabel的大小在调整大小时是不变的 . )

我想到了以下几点:

  • 使用适当的布局管理器 . 我不知道 . 我可以将jbutton和jlabel添加到另一个jpanel并将这个新的jpanel添加到大型jpanel的中心,但在这种情况下,jbutton将不会居中 .

Edit: 我试图使用this proposal:(提案已编辑,代码现在正确)

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);
 

   }
}

但它产生以下输出,其中按钮不居中:

  • 将jbutton和jlabel添加到jpanel . 将 ComponentListener 添加到jpanel并覆盖 componentResized . 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'这个方法的位置 . 但只有在调整大小完成后才会调用 componentResized . 所以在调整jlabel 's position will be defined by layout manager which isn' t的过程中需要 .

  • 将jbutton添加到内容窗格并将jlabel添加到玻璃窗格 . 但是我会像以前的情况一样调整大小 .

我该如何实现这种布局?

2 回答

  • 1

    使用支持您需要的LayoutManager是可行的方法 . 高级管理器允许定义大小等于宽度/高度的组(列/行) . 如果核心经理不允许这样的语义规范,那就去找第三方经理f.i.任何BigThree:MigLayout,FormLayout或DesignGridLayout . 投入学习的时间将得到充分利用:-)

    在MigLayout(我个人最喜欢的)中,约束是“sizegroup groupName”或“sg groupName”(如果只有ongroup,groupName可以为空),f.i .

    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");
    
  • 1

    看起来你可以使用3列的网格包布局 . 第一列和第三列必须具有相同的重量,因此宽度相同 . 中间的列没有重量所以会坐在中间 . 我会为你尝试,但我在iPhone上打字! HTH

    请参阅注释,这是我对示例代码的编辑:

    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);
        }
    }
    

相关问题