ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何修复GridBagLayout中的间隙

2019-11-23 01:14:12  阅读:236  来源: 互联网

标签:gridbaglayout swing jpanel java user-interface


我正在使用GridBagLayout创建一个名为’Preset’的JPanel,该JPanel在JFrame中被复制了几次.每个预设将有多行(JPanels).我的目标是只显示一行(第一行),但是当单击编辑按钮时,它们都会全部显示.现在,“编辑”按钮可以使用,但是行之间有很大的空间.我希望当多余的行合拢时,每个“预设”将彼此直接位于上方(无空格).您可以在下图中看到我在说什么.

它是这样的:

这就是我想要的样子:

我相当确定我需要对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;
    }
}
}

解决方法:

删除在Y AXIS中为预设JPanel的GridBagConstraints指定填充的所有语句,即

cons.ipady = 100;

标签:gridbaglayout,swing,jpanel,java,user-interface
来源: https://codeday.me/bug/20191123/2063891.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有