首页 文章

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

提问于
浏览
0

我是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 回答

  • 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);
    

    实现?

    Updated

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

    您需要做的是在色谱柱中添加一些填料成分,这将使其进入空间......

    For example

    Updated with a cheat

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

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

相关问题