【发布时间】:2018-10-08 17:21:21
【问题描述】:
我正在使用 Java 中的 GridBagLayout,我编写了以下代码,但它没有按预期工作
我预计第一个按钮将放置在框架左上角的点 (0,0)。
其次,我预计第二个按钮的高度和宽度将是第一个按钮高度和宽度的两倍,但事实并非如此。
这是输出:https://ibb.co/hDZPgx
这是代码:
import javax.swing.*;
import java.awt.*;
public class GUIJframe extends JFrame {
private JButton jb1 = new JButton("first");
private JButton jb2 = new JButton("second");
private JButton jb3 = new JButton("third");
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbl = new GridBagLayout();
public GUIJframe () {
jcb.addItem(names);
setLocation(150,150);
setSize(500,500);
setLayout(gbl);
addComponent(jb1,0,0,1,1);
addComponent(jb2,1,0,2,2);
addComponent(jb3,2,2,5,5);
setVisible(true);
}
public void addComponent (Component component, int gridx,int gridy, int width, int height) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = width;
gbc.gridheight = height;
gbl.setConstraints(component,gbc);
add (component);
}
}
【问题讨论】:
标签: java layout gridbaglayout