Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GridBagLayout在JPanel中居中放置组件_Java_Swing_Jpanel_Layout Manager_Gridbaglayout - Fatal编程技术网

Java 使用GridBagLayout在JPanel中居中放置组件

Java 使用GridBagLayout在JPanel中居中放置组件,java,swing,jpanel,layout-manager,gridbaglayout,Java,Swing,Jpanel,Layout Manager,Gridbaglayout,我的JPanel有一个GridBagLayout,在组件对齐方面有问题。JLabel位于顶部,但不居中,下面的JButtons位于右侧。有没有办法让他们都站在中间?下面是我初始化GUI的方法 public void initialize() { JButton[] moveChoices = new JButton[3]; JPanel buttonsPanel = new JPanel(); buttonsPanel.setBackground(

我的JPanel有一个GridBagLayout,在组件对齐方面有问题。JLabel位于顶部,但不居中,下面的JButtons位于右侧。有没有办法让他们都站在中间?下面是我初始化GUI的方法

public void initialize() {
        JButton[] moveChoices = new JButton[3];
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setBackground(Color.green);
        buttonsPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JLabel welcome = new JLabel();
        for(int i = 0; i < moveChoices.length; i++) {
            moveChoices[i] = new JButton();
            c.gridx = i;
            c.gridy = 1;
            if (i == 0) c.anchor = GridBagConstraints.EAST;
            if (i == 2) c.anchor = GridBagConstraints.WEST;
            moveChoices[i].addActionListener(this);
            buttonsPanel.add(moveChoices[i], c);
        }

        moveChoices[0].setText("Rock");
        moveChoices[1].setText("Paper");
        moveChoices[2].setText("Scissors");

        welcome.setText("Welcome to rock paper scissors! Please enter your move.");
        c.gridy = 0; c.gridx = 1; c.insets = new Insets(10, 0, 0, 0);
        buttonsPanel.add(welcome);
        winText = new JLabel();
        buttonsPanel.add(winText, c);
        this.add(buttonsPanel);
    }
public void initialize(){
JButton[]moveChoices=新JButton[3];
JPanel buttonPanel=新的JPanel();
按钮面板。挫折背景(颜色。绿色);
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
JLabel welcome=新JLabel();
for(int i=0;i
这是您的代码,它的格式是有效的。要使其正常工作,您需要注意以下事项:

  • 所有按钮都需要相同的权重X,以使空间均匀分布 他们之间
  • 标题需要一些权重X才能获得其所有行
  • 使用fill=BOTH可使所有组件填满它们获得的空间
  • 使用gridwidth使标题使用三列,而按钮仅使用一列
  • 使用JLabel的水平对齐使标题在其空间内居中

  • public void initialize(){
    JButton[]moveChoices=新JButton[3];
    JPanel buttonPanel=新的JPanel();
    按钮面板。挫折背景(颜色。绿色);
    setLayout(新的GridBagLayout());
    GridBagConstraints c=新的GridBagConstraints();
    JLabel welcome=新JLabel();
    c、 fill=GridBagConstraints.BOTH;
    c、 锚点=GridBagConstraints.CENTER;
    welcome.setText(“欢迎来到石头剪纸!请输入您的动作”);
    欢迎使用.setHorizontalAlignment(SwingConstants.CENTER);
    c、 权重x=1;
    c、 gridy=0;c.gridx=0;c.insets=newinsets(10,0,0,0);
    c、 gridwidth=moveChoices.length;
    c、 网格高度=1;
    按钮面板。添加(欢迎,c);
    c、 插图=新插图(0,0,0,0);
    c、 网格宽度=1;
    for(int i=0;i
    为了更快地获得更好的帮助,请发布一个。任何JC组件都通过删除(不要使用任何)GBC来居中
    public void initialize() {
        JButton[] moveChoices = new JButton[3];
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setBackground(Color.green);
        buttonsPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JLabel welcome = new JLabel();
        c.fill=GridBagConstraints.BOTH;
        c.anchor=GridBagConstraints.CENTER;
        welcome.setText("Welcome to rock paper scissors! Please enter your move.");
        welcome.setHorizontalAlignment(SwingConstants.CENTER);
        c.weightx=1;
        c.gridy = 0; c.gridx = 0; c.insets = new Insets(10, 0, 0, 0);
        c.gridwidth=moveChoices.length;
        c.gridheight=1;
        buttonsPanel.add(welcome,c);
        c.insets=new Insets(0,0,0,0);
        c.gridwidth=1;
        for(int i = 0; i < moveChoices.length; i++) {
            moveChoices[i] = new JButton();
            c.gridx = i;
            c.gridy = 1;
            c.weightx=0.5;
            c.weighty=1;
            moveChoices[i].addActionListener(this);
            buttonsPanel.add(moveChoices[i], c);
        }
    
        moveChoices[0].setText("Rock");
        moveChoices[1].setText("Paper");
        moveChoices[2].setText("Scissors");
        this.add(buttonsPanel);
    }