Java GridBagLayout:设置组件的位置

Java GridBagLayout:设置组件的位置,java,swing,user-interface,layout-manager,gridbaglayout,Java,Swing,User Interface,Layout Manager,Gridbaglayout,我阅读了一些问题,其中指出GBL在定位组件方面优于普通的GridLayout 链接: , 所以我尝试向GBL添加一些组件(按钮),但它总是以某种方式将我的组件居中。分开这两个按钮的唯一方法是在我的项目中放置假人,这看起来很糟糕 public class GridGUI extends JPanel { private JButton button; public GridGUI() { setLayout(new GridBagLayout()); GridBagConstr

我阅读了一些问题,其中指出GBL在定位组件方面优于普通的GridLayout

链接: ,

所以我尝试向GBL添加一些组件(按钮),但它总是以某种方式将我的组件居中。分开这两个按钮的唯一方法是在我的项目中放置假人,这看起来很糟糕

public class GridGUI extends JPanel {

private JButton button;

public GridGUI() {
    setLayout(new GridBagLayout());
    GridBagConstraints comps = makeGbc(0,1);
    JPanel panel = new JPanel();
    button = new JButton("Datei speichern");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }

    });
    panel.add(button);
    add(panel,comps);

    comps = makeGbc(1,1);
    button = new JButton("Datei laden");
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(new JPanel());
    panel.add(button);
    add(panel,comps);
}

private GridBagConstraints makeGbc(int x, int y) {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.weightx = x;
    gbc.weighty = 1.0;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    return gbc;
}

public static void showGUI() {
    JFrame frame = new JFrame("Pop-Art-Collage");
    frame.getContentPane().add(new GridGUI());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(800, 800);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            showGUI();
        }

    });
}

}

但是我希望它们出现在GUI的顶部,而不是中央

我还尝试使用
button.setSize(x,y)
调整按钮的大小,但它们始终保持不变

提前谢谢

编辑:

好吧,我现在尝试了我得到的解决方案,但我遇到了一些其他问题

    GridBagConstraints comps = makeGbc(0,1,20,50,0,0);
    button = new JButton("Datei speichern");
    button.setPreferredSize(new Dimension(200,25));
    add(button,comps);
    JButton button2 = new JButton("Datei laden");
    button2.setPreferredSize(new Dimension(100,25));
    comps = makeGbc(1,1,20,150,0,100);
    add(button2,comps);

我的问题是,我将两个按钮的首选大小设置为相同,但第二个按钮的首选大小仍然无法正确显示。
(MaxGBC X、Y之后的4个数字是插入值的顶部、左侧、底部、右侧)

< P>您的按钮位于中间,而不是在顶部,因为您将其放置在:

gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
gridbagstraints.LINE\u END
gridbagstraints.LINE\u START
将组件放在中心。尝试:

gbc.anchor = (x == 0) ? GridBagConstraints.FIRST_LINE_START : GridBagConstraints.FIRST_LINE_END;
此外:

  • 尝试使用
    setPreferredSize()
    设置按钮的大小,使用
    frame.pack()设置GBL的按钮应符合所选大小

  • 您将按钮添加到新的
    JPanels
    ,然后添加到带有GBL的容器中,这样它就会将按钮放在中间。将按钮直接添加到
    GridGUI
    ,它将根据
    GridBagConstraints()
    放置,例如:

    button = new JButton("Datei laden");
    add(button,makeGbc(1,1));
    
  • 对于两个不同的按钮,您使用一个类字段
    JButton button
    ,这样您就无法在更改按钮引用后更改第一个按钮的状态-如果您尝试更改按钮中的文本,例如,从应用程序的不同部分,通过使用
    button
    类字段,您只能访问第二个按钮

编辑

我不确定您到底想要什么效果,但我会尝试先将您的
GridGUI
添加到new
JPanel
,然后添加到
JFrame
(在main方法中):

现在,FlowLayout应该将所有组件保持在左侧,您可以通过更改插图来控制按钮之间的空间。对于第一个按钮,a将所有输入设置为0,而在第二个按钮中,
新插入(0,x,0,0)
,其中x是所需距离。像这样:

add(button,makeGbc(0,1,0,0,0,0));
add(button2,makeGbc(0,1,0,150,0,0));
但我不确定,当您添加更多组件时,这组布局将如何运行,因此更简单的方法是只使用
frame.pack()
,而不使用
frame.setSize()
,然后通过添加新组件来扩展它。那么,预测变化将如何发挥作用就应该更容易了


还要注意一个事实,在上面的示例代码中,您为两个按钮设置了不同的
setPreferredSize()

(1+),
分隔这两个按钮的唯一方法是在我的项目中放置假人,这看起来很糟糕。
您使用的是控制两个组件之间空间的“插入”,所以请更改插图。@AndrewThompson谢谢您的评论,我已经编辑了我的答案!我显然不知道。但是我必须用哪种方式来处理插图呢?他们该怎么办?如果我将插图更改为例如(100,0,0,0),它将更改nothing@m.cekiera好吧,我在谷歌上搜索了一下,现在知道插图是怎么工作的了。他们非常直截了当。但是我更新了我的问题,也许你可以看看这个?@m.cekiera现在JPanel的问题是我的插图格式很奇怪。组件的位置与以前不同。我需要的是“Datei Loaded”按钮与另一个按钮的大小相同。我将首选大小设置为与“Datei Speichen”按钮相同的大小,但插图将其填充起来,使其看起来更大。
add(button,makeGbc(0,1,0,0,0,0));
add(button2,makeGbc(0,1,0,150,0,0));