调整JFrame大小时,消除组件之间的额外空间

时间:2017-08-27 13:49:37

标签: java swing layout-manager gridbaglayout

我正在使用GridBagLayout来排列框架中的一些组件。
首次创建框架时,组件之间有相当大的空间。enter image description here

但是一旦我调整框架大小,组件之间就会有很多不需要的空间 enter image description here

我尝试按照一些用户的建议调整权重和插入内容,但似乎无法解决问题

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JTextArea;

public class Frame1 extends JFrame {

    JLabel one = new JLabel("one");
    JLabel two = new JLabel("two");
    JLabel three = new JLabel("three");

    JTextField oneF = new JTextField(20);
    JTextField twoF = new JTextField(20);
    JTextField threeF = new JTextField(20);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("menu");

    GridBagConstraints c = new GridBagConstraints();

    public Frame1() {
        setTitle("GridBagLayout Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        menuBar.add(menu);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = c.REMAINDER;
        c.fill = c.HORIZONTAL;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(menuBar, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(one, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(oneF, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(two, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(twoF, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(three, c);

        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.gridheight = 1;
        c.weightx = 1;
        c.weighty = 1;
        c.anchor = c.NORTH;
        c.insets = new Insets(5,5,5,5);
        add(threeF, c);

       //setResizable(false);
       pack();
       setVisible(true);
    }

}

ps: - 我是GUI编程的新手,所以请原谅我任何noob错误。

编辑1:这就是我完成后想要拥有的东西。我知道目前它看起来并不像我想的那样......我还在努力enter image description here

由于

4 个答案:

答案 0 :(得分:3)

  • 使用嵌套布局(几个LayoutManager s的组合),你的图片谈到我,

  • 您仍然可以将GridBagLayout用于放置在左侧的组件,

  • 在我的代码中(尽可能最简单的想法)放置在左侧的JComponent无法调整大小,因为受限于LayoutManager的默认值,更多在Oracle tutorial

enter image description here

enter image description here

从SSCCE / MCVE绘制

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame {

    private static final long serialVersionUID = 1L;
    private JFrame myFrame = new JFrame("Whatever");
    private JPanel parentPanel = new JPanel(new BorderLayout(10, 10)) {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        @Override
        public Color getBackground() {
            return new Color(255, 000, 000);
        }
    };
    private JPanel leftPanel = new JPanel(/*default is FlowLayout*/) {

                private static final long serialVersionUID = 1L;

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(300, 400);
                }

                @Override
                public Color getBackground() {
                    return new Color(255, 255, 000);
                }
            };
    private JPanel leftChildPanel = new JPanel() {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 400);
        }

        @Override
        public Color getBackground() {
            return new Color(255, 255, 225);
        }
    };
    private JPanel rightPanel = new JPanel(new BorderLayout(10, 10)) {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 380);
        }

        @Override
        public Color getBackground() {
            return new Color(000, 255, 225);
        }
    };

    public MyFrame() {
        parentPanel.add(leftPanel, BorderLayout.WEST);
        leftPanel.add(leftChildPanel);
        parentPanel.add(rightPanel);
        myFrame.add(parentPanel);
        myFrame.setLocation(150, 150);
        myFrame.pack();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new MyFrame();
        });
    }
}

答案 1 :(得分:1)

我对它的外观并不十分了解,但您可以尝试为文本字段设置标签c.anchor=GridBagConstraints.EASTc.anchor=GridBagConstraints.WEST

尝试同时设置c.fill = GridBadConstraints.BOTH

答案 2 :(得分:1)

我们的想法是添加空行/列,以增加可用空间:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;

public class Frame1 extends JFrame {

    JLabel one = new JLabel("one");
    JLabel two = new JLabel("two");
    JLabel three = new JLabel("three");

    JTextField oneF = new JTextField(20);
    JTextField twoF = new JTextField(20);
    JTextField threeF = new JTextField(20);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("menu");

    public Frame1() {

        setTitle("GridBagLayout Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; //this defines 4 rows
        //make 2 last empty row grow 
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0,1.0};

        //do the same for columns 
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0,1.0};

        getContentPane().setLayout(gridBagLayout);

        menuBar.add(menu);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 5;
        c.fill = c.HORIZONTAL;
        c.anchor = c.NORTH;
        c.insets = new Insets(5, 5, 5, 0);
        getContentPane().add(menuBar, c);

        //better have a new GridBagConstraints for each component added 
        GridBagConstraints c1 = new GridBagConstraints();
        c1.gridx = 0;
        c1.gridy = 1;
        c1.gridwidth = 1;
        c1.fill = c1.NONE;
        c1.anchor = c1.NORTH;
        c1.insets = new Insets(5, 5, 0, 5);
        getContentPane().add(one, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.gridx = 1;
        c2.gridy = 1;
        c2.fill = c2.NONE;
        c2.anchor = GridBagConstraints.NORTHWEST;
        c2.insets = new Insets(5, 5, 0, 5);
        getContentPane().add(oneF, c2);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Frame1();
    }
}

编辑以响应您的修改:使用“封面艺术”的其他“增长”列

答案 3 :(得分:1)

问题在于您c.weightxc.weighty的分配。 weightx和weighty确定当容器大于容纳组件的首选大小所需的容器时,如何为GridBagLayout中的网格单元分配额外空间。

对于所有细胞,weightx和weighty应该为零,除了那些在窗口变大时你想要变大的细胞。