java - 如何修复 GridBagLayout 中的间隙

标签 java swing user-interface jpanel gridbaglayout

我正在使用 GridBagLayout 创建一个名为“Preset”的 JPanel,它在 JFrame 中被复制了多次。每个预设将有多行 (JPanels)。我的目标是只显示一行(第一行),但是当单击编辑按钮时,它们都会显示。现在,编辑按钮可以使用,但行与行之间有很大的空间。我希望它是这样的,当额外的线被折叠时,每个预设将直接在彼此之上(没有空间)。您可以在下图中看到我在说什么。

它是这样的: enter image description here

这就是我想要的样子: enter image description here

我相当确定我需要对 GridBag 做些什么,但我不知道做什么。我已经阅读了几个教程并按照我认为应该的方式编写了它,但没有运气。


package SSCCE;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class UI extends JFrame{
    private final static int HEIGHT = 600;
    private final static int WIDTH = 730;
    private JPanel pane; //Pane that stores accounts
    private JScrollPane scroller;
    private Preset[] branches;    

    public static void main(String[] args) {
        JFrame frame = new UI();
    }    

    public UI(){
        //Make the UI close when the exit button is clicked
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

        //Sets the size of the UI
        this.setSize(WIDTH, HEIGHT);

        //Set the layout and add components to it
        this.setLayout(new BorderLayout());

        //Reads in the settings and sets up the branches
        populateBranches();

        pane = new JPanel();
        pane.setLayout(new GridLayout(branches.length,1));
        for (int i = 0; i < branches.length; i++){
            pane.add(branches[i]);
        }

        //Create the center pane of the BorderLayout
        scroller = new JScrollPane(pane);
        scroller.createVerticalScrollBar();
        this.add(scroller,BorderLayout.CENTER);

        //Makes the UI visible
        this.setVisible(true);
    }

    private void populateBranches(){
        //Populates the branches array based on what is read in, or not read in from the file
        branches = new Preset[15];

    for (int i = 0; i < branches.length; i++){
        branches[i] = new Preset();
        branches[i].setEnabled(false);
    }
}

public class Preset extends JPanel{
    private JTextField eName;
    private JButton edit;
    private JButton activate;
    private JComboBox printer;
    private JPanel line1;
    private JPanel line2;
    private String branchName;
    private String ipAddress;
    private boolean enableAll;

    public Preset(){
        eName = new JTextField(20);
        edit = new JButton("Edit");
        activate = new JButton("Activate");

        JPanel nameContainer = new JPanel();
        nameContainer.setLayout(new FlowLayout());
        nameContainer.add(eName);

        printer = new JComboBox();

        //
        line1 = new JPanel();
        line1.setLayout(new FlowLayout());
        line1.add(nameContainer);
        line1.add(edit);
        line1.add(activate);

        //
        line2 = new JPanel();
        line2.setLayout(new BorderLayout());
        line2.add(printer, BorderLayout.WEST);

        GridBagLayout grid = new GridBagLayout();
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        this.setLayout(grid);

        cons.ipady = 100;
        cons.ipadx = 100;
        cons.weighty = 0D;
        cons.gridx = 0;
        cons.gridy = 0;
        cons.gridwidth = 2;
        cons.gridheight = 1;
        grid.setConstraints(line1, cons);
        this.add(line1);

        cons.ipady = 100;
        cons.ipadx = 100;
        cons.weighty = 0D;
        cons.gridx = 0;
        cons.gridy = 1;
        cons.gridwidth = 2;
        cons.gridheight = 1;
        grid.setConstraints(line2, cons);
        this.add(line2);

        //Enable all components
        enableAll = true;
    }
}
}

最佳答案

删除为 Preset JPanelGridBagConstraints 在 Y 轴上分配填充的每个语句,即

cons.ipady = 100;

关于java - 如何修复 GridBagLayout 中的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17972904/

相关文章:

java - FirebaseDatabase仅保存id

java - 为什么 JPasswordField.getPassword() 会创建一个包含密码的字符串?

java - 创建不同数据类型(日期和 double )的二维数组

java - 从构造函数构建 GUI。 [设计决策]

android - 如何开发这个菜单(在图片上)

java - 类加载器之间的反射参数类型不匹配

java - 程序中读取不同硬币数量并打印总数的文件时出现异常

java - 尝试创建随机的 "solar system"

java - 从组合框中选择国家/地区、州和城市

java - 在 JList 中添加并显示对象