java gridbag_java – 调整Gridbag布局

我试图在

java中使用GRIDBAG布局实现此布局

public static void addComponentsToPane(Container pane) {

if (RIGHT_TO_LEFT) {

pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

}

JLabel label1,label2,label3,result,title;

JButton calculate_btn;

JTextField side1,side2,side3;

pane.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

if (shouldFill) {

//natural height, maximum width

c.fill = GridBagConstraints.HORIZONTAL;

}

title = new JLabel("Area of Triangle");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 2;

c.gridy = -1;

pane.add(title, c);

label1 = new JLabel("Side 1: ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 1;

c.gridy = 1;

pane.add(label1, c);

label2 = new JLabel("Side 2: ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 1;

c.gridy = 2;

pane.add(label2, c);

label3 = new JLabel("Side 3: ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 1;

c.gridy = 3;

pane.add(label3, c);

side1 = new JTextField(" ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 2;

c.gridy = 1;

pane.add(side1, c);

side2 = new JTextField("Side 3: ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 2;

c.gridy = 2;

pane.add(side2, c);

side3 = new JTextField("Side 3: ");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 2;

c.gridy = 3;

pane.add(side3, c);

calculate_btn = new JButton("Calculate");

//c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 30; //make this component tall

c.weightx = 0.5;

c.gridwidth = 3;

c.gridx = 0;

c.gridy = 5;

pane.add(calculate_btn, c);

result = new JLabel("Result displayed here");

if (shouldWeightX) {

c.weightx = 0.5;

}

c.fill = GridBagConstraints.HORIZONTAL;

c.ipady = 20;

c.gridx = 2;

c.gridy = 7;

pane.add(result, c);

}

所以上面的代码基本上只是将添加到GUI的组件,但我不是很想得到我想要的,这就是我想要实现的

但这是我用上面的代码得到的

因此,当我编译上面是我最终的,也是如果可能我不希望用户调整窗口大小,我猜一些布尔与窗口属性之一..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值