GridBagLayout没有填充JPanel

时间:2017-03-13 14:35:22

标签: 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) {

}

}

1 个答案:

答案 0 :(得分:1)

将来,请发布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;
    }
}