java - GridBagLayout 未填充 JPanel

标签 java swing layout-manager gridbaglayout

我正在使用 GridBagLayout 为程序构建一个工具栏,但即使使用 Weightx 和 fill,组件的大小也没有达到应有的大小,并且没有填充 JPanel。

例如,当jidest.x_size的总大小为1920时,组件的总大小为1776

package jide.parts;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import jide.jidest;

@SuppressWarnings("serial")
public class BottomBar extends JPanel implements MouseListener{
JPanel charCountPanel = new JPanel();
JPanel lineCountPanel = new JPanel();
JPanel cursorPositionPanel = new JPanel();
JPanel spacer1 = new JPanel();
JPanel spacer2 = new JPanel();
JPanel spacer3 = new JPanel();
JPanel morespace = new JPanel();
JLabel charCount = new JLabel("charcount");
JLabel lineCount = new JLabel("linecount");
JLabel cursorPosition = new JLabel("TEST:TEST");
public BottomBar(){
    this.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    charCountPanel.setToolTipText("Click here for more complete stats");
    lineCountPanel.setToolTipText("Click here for more complete stats");
    setPreferredSize(new Dimension((int) jidest.x_size,20));
    setBackground(Color.red);
    cursorPositionPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    cursorPositionPanel.add(cursorPosition);
    //cursorPositionPanel.setSize(50,20);
    lineCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    lineCountPanel.add(lineCount);
    //lineCountPanel.setSize(50,20);
    charCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    charCountPanel.add(charCount);
    //charCountPanel.setSize(50,20);
    morespace.setSize((int) (jidest.x_size-156),20);
    //add(cursorPositionPanel);
    spacer1.setBackground(Color.BLACK);
    //spacer1.setSize(1,20);
    spacer2.setBackground(Color.BLACK);
    //spacer2.setSize(1,20);
    spacer3.setBackground(Color.BLACK);
    //spacer3.setPreferredSize(new Dimension(1,20));
    //size is 156, width is 7
    gbc.fill=GridBagConstraints.BOTH;
    gbc.weighty=1;
    gbc.gridx = 1;
    gbc.weightx=(1.0)/jidest.x_size;
    add(spacer1,gbc);
    gbc.gridx=2;
    gbc.weightx=(50.0)/jidest.x_size;
    add(cursorPositionPanel,gbc);
    gbc.gridx=3;
    gbc.weightx=(1.0)/jidest.x_size;
    add(spacer2,gbc);
    gbc.gridx=4;
    gbc.weightx=(50.0)/jidest.x_size;
    add(lineCountPanel,gbc);
    gbc.gridx=5;
    gbc.weightx=(1.0)/jidest.x_size;
    add(spacer3,gbc);
    gbc.gridx=6;
    gbc.weightx=(50.0)/jidest.x_size;
    add(charCountPanel,gbc);
    gbc.gridy = 0;
    gbc.gridx=0;
    gbc.weightx=(jidest.x_size-153)/jidest.x_size;
    add(morespace,gbc);
    System.out.println((spacer1.getWidth()+cursorPositionPanel.getWidth()+spacer2.getWidth()+lineCountPanel.getWidth()+spacer3.getWidth()+morespace.getWidth())+" should be " + jidest.x_size);
}

@Override
public void mouseClicked(MouseEvent arg0) {

}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}

}

最佳答案

将来,请发布 MCVE。这可以将组件分布在条的宽度上。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class BottomBarGUI {

    private JComponent ui = null;

    BottomBarGUI() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        ui.add(new BottomBar());
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                BottomBarGUI o = new BottomBarGUI();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class BottomBar extends JPanel {

    JPanel charCountPanel = new JPanel();
    JPanel lineCountPanel = new JPanel();
    JPanel cursorPositionPanel = new JPanel();
    JPanel spacer1 = new JPanel();
    JPanel spacer2 = new JPanel();
    JPanel spacer3 = new JPanel();
    JPanel morespace = new JPanel();
    JLabel charCount = new JLabel("charcount");
    JLabel lineCount = new JLabel("linecount");
    JLabel cursorPosition = new JLabel("TEST:TEST");

    public BottomBar() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridy = 0;
        gbc.weightx = .5;
        charCountPanel.setToolTipText("Click here for more complete stats");
        lineCountPanel.setToolTipText("Click here for more complete stats");
        setBackground(Color.red);
        cursorPositionPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        cursorPositionPanel.add(cursorPosition);
        lineCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        lineCountPanel.add(lineCount);
        charCountPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        charCountPanel.add(charCount);
        spacer1.setBackground(Color.BLACK);
        spacer2.setBackground(Color.BLACK);
        spacer3.setBackground(Color.BLACK);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 1;
        gbc.gridx = 0;
        add(spacer1, gbc);
        gbc.gridx = 1;
        add(cursorPositionPanel, gbc);
        gbc.gridx = 2;
        add(spacer2, gbc);
        gbc.gridx = 3;
        add(lineCountPanel, gbc);
        gbc.gridx = 4;
        add(spacer3, gbc);
        gbc.gridx = 5;
        add(charCountPanel, gbc);
        gbc.gridx = 0;
    }
}

关于java - GridBagLayout 未填充 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42766247/

相关文章:

java - 使用Spring的@Configuration类的缺点

java - 将 JPanel 类放入另一个类的 JFrame 中

java - JApplet 行为异常

java - 在另一个类的框架中启用 Jbutton - GUIBuilder

java - 无论我使用哪种布局,JTree 都不会显示

java - 如何在 JFrame 中排列组件

java - TestNG @BeforeClass 和测试类构造函数的区别

java - 如何在 JUnit 5 中使用少量枚举创建参数化测试?

java - Eclipse 热键 : How to switch between indenting levels based with spaces?

java - JFrame 中的多个 JPanel