编程语言
首页 > 编程语言> > java – 在调整JFrame大小时消除组件之间的额外空间

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

作者:互联网

我正在使用GridBagLayout来安排框架中的一些组件.

首次创建框架时,组件在它们之间具有相当大的空间

但是一旦我调整框架大小,组件之间就会有很多不需要的空间
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

谢谢

解决方法:

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

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

编辑:响应您的编辑:使用额外的“增长”列为“封面艺术”

标签:gridbaglayout,java,swing,layout-manager
来源: https://codeday.me/bug/20190823/1699241.html