无法在Java中的GridBagLayout中使用gridwidth正确调整JButton的大小

时间:2014-04-14 23:57:19

标签: java swing jbutton gridbaglayout

我是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);

    }
}

它应该是这样的:

enter image description here

1 个答案:

答案 0 :(得分:2)

你的意思更像......

enter image description here

我用过的......

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

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

gbc.fill = 0;

panel.add(one, gbc);

enter image description here

我用过的......

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

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

gbc.fill = GridBagConstraints.HORIZONTAL;

panel.add(one, gbc);

实现?

<强>更新

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

您需要做的是在色谱柱中加入某种填料成分,这会使其留有空间......

For example

更新了作弊

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

enter image description here

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;
        }
    }
}