java中的gridy_Java GridBagConstraints gridx和gridy无法正常工作?

我正在尝试使用gridx和gridy约束来定位我的按钮。但是它们不起作用!如果更改gridx和gridy变量,则什么也不会发生。如果我将填充更改GridBagConstraints为NONE,它仍然不起作用。

我在这里想念什么吗?

import java.awt.*;

import javax.swing.*;

public class Window extends JFrame{

private static final long serialVersionUID = 1L;

JFrame frame = new JFrame("GUI");

JTextField username = new JTextField(20);

public void CreateWindow(){

JPanel pane = new JPanel();

pane.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.NONE;

JButton button = new JButton("Button 1");

c.weightx = 1.0;

c.weighty = 1.0;

c.gridx = 3; //Has no effect

c.gridy = 5; //Has no effect

c.anchor = GridBagConstraints.NORTHWEST;//If I remove this, it still does not work.

pane.add(button, c);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

frame.setSize(400, 600);

frame.setResizable(true);

frame.setLocationRelativeTo(null);

frame.add(pane);

frame.setVisible(true);

}

}

如果很难理解,这里就是问题所在:

JPanel pane = new JPanel();

pane.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.NONE;

JButton button = new JButton("Button 1");

c.weightx = 1.0;

c.weighty = 1.0;

c.gridx = 3; //Has no effect

c.gridy = 5; //Has no effect

c.anchor = GridBagConstraints.NORTHWEST; //If I remove this, it still does not work.

pane.add(button, c);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值