提问者:小点点

未能正确调整Java中GridBagLayout中具有网格宽度的JButton的大小


我是Java新手,我很难理解GridBag-Layout。我只想做的是将按钮 1 的大小调整为三个按钮的宽度。这意味着按钮 1 从坐标 x=0,y=0 开始,在按钮 3 的末尾上方结束 (x=0,y=3)。

代码如下:

package footballQuestioner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class asd  {

    public static void main(String[] args) {
        JFrame frame = new Houdini();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      frame.setSize(250, 250);
        System.out.println(frame.getWidth());
        frame.setResizable(true); 
        frame.pack();
        frame.setVisible(true);
    }
}


class Houdini extends JFrame {

    private JButton one=new JButton("one");
    private JButton two=new JButton("two");
    private JButton three=new JButton("three");
    private JButton four=new JButton("four");
    private JButton five=new JButton("five");

    private GridBagConstraints gbc=new GridBagConstraints();
    private JPanel panel=new JPanel(new GridBagLayout());

    public Houdini() {



        //      Insets insets=new Insets(30, 0, 30, 0);
//      gbc.insets=insets;

        gbc.gridx=0;
        gbc.gridy=0;


        gbc.gridwidth=1; 
        gbc.gridheight=1;


        gbc.fill=0;

        panel.add(one,gbc);


        gbc.gridx=1;
        gbc.gridy=1;

        gbc.gridwidth=1;
        gbc.gridheight=1;


        gbc.fill=0;

        panel.add(two,gbc);


        gbc.gridx=2;
        gbc.gridy=2;

        gbc.gridwidth=1;
        gbc.gridheight=2;

        gbc.fill=0;

        panel.add(three,gbc);


//      gbc.gridx=3;
//      gbc.gridy=3;
//      
//      gbc.gridwidth=-4;
//      gbc.gridheight=1;
//      
//
//      gbc.fill=GridBagConstraints.HORIZONTAL;
//      
//      panel.add(four,gbc);

        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        add(panel);

    }
}

它应该是这样的:


共1个答案

匿名用户

你的意思更像。。。

我用过的。。。

gbc.gridx = 0;
gbc.gridy = 0;

gbc.gridwidth = 3;
gbc.gridheight = 1;

gbc.fill = 0;

panel.add(one, gbc);

或者

我用过的。。。

gbc.gridx = 0;
gbc.gridy = 0;

gbc.gridwidth = 3;
gbc.gridheight = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

panel.add(one, gbc);

实现?

已更新

问题是,从GridBagLayout的角度来看,列0没有宽度。。。这就是它被压缩的原因。。。

您需要做的是在列中添加某种填充组件,这将导致它在其中添加空间。。。

例如

更新了作弊

根据你想要达到的目标,你也可以作弊;)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestLayout {

    public static void main(String[] args) {
        JFrame frame = new Houdini();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      frame.setSize(250, 250);
        System.out.println(frame.getWidth());
        frame.setResizable(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static class Houdini extends JFrame {

        private JButton one = new JButton("one");
        private JButton two = new JButton("two");
        private JButton three = new JButton("three");
        private JButton four = new JButton("four");
        private JButton five = new JButton("five");

        private GridBagConstraints gbc = new GridBagConstraints();
        private JPanel panel = new JPanel(new BorderLayout());

        public Houdini() {

            JPanel top = new JPanel(new BorderLayout());
            top.add(one);
            JPanel center = new JPanel(new GridLayout(0, 3));

            center.add(empty());
            center.add(two);
            center.add(empty());
            center.add(empty());
            center.add(empty());
            center.add(three);

            panel.add(top, BorderLayout.NORTH);
            panel.add(center);

            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            add(panel);

        }

        private JComponent empty() {
            JPanel panel = new JPanel();
            return panel;
        }
    }
}