JPanel布局管理器 - 如何防止组件填充空间?

时间:2013-01-24 12:27:37

标签: java swing

我正在尝试构建一组两个标签和一个按钮(标签按钮标签),看起来像这一行的顶行:

enter image description here

我正在努力用Java Swing做到这一点。我尝试过BorderLayout,使用BorderLayout.WEST,BorderLayout.CENTER,BorderLayout.EAST,但这会使按钮填满空格: enter image description here

这是我用于此的代码:

panel = new JPanel(new BorderLayout());
l1 = new JLabel("l1");
button = new JButton("B");
l2 = new JLabel("l2");
panel.add(l1, BorderLayout.WEST);
panel.add(button, BorderLayout.CENTER);
panel.add(l2, BorderLayout.EAST);

我也尝试了GridBagLayout,而我最接近的就是让它们间隔开来,但不要抱着两边:

enter image description here

代码:

panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
l1 = new JLabel("L1");
l2 = new JLabel("L2");
button = new JButton("B");
c.weightx = Integer.MAX_VALUE;
c.gridx = 0;
panel.add(l1, c);
c.weightx = 1;
c.gridx = 1;
panel.add(button, c);
c.weightx = Integer.MAX_VALUE;
c.gridx = 2;
panel.add(l2, c);

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

GridLayoutLEFT& CENTER标签。

RIGHT

image

SSCCE:

    JPanel p = new JPanel(new GridLayout(1,0));
    p.add(new JLabel("Test", JLabel.LEFT));
    p.add(new JLabel("Test", JLabel.CENTER));
    p.add(new JLabel("Test", JLabel.RIGHT));

答案 1 :(得分:4)

以下是两种方法。一次使用BorderLayout,一次使用GridBagLayout:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI1() {
        final JFrame frame = new JFrame("Grid bag layout");
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel l1 = new JLabel("L1");
        JLabel l2 = new JLabel("L2");
        JButton b = new JButton("B");
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        panel.add(l1, gbc);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(b, gbc);
        gbc.weightx = 0;
        panel.add(l2, gbc);
        frame.add(panel);
        frame.setSize(800, 100);
        frame.setVisible(true);
    }

    protected void initUI2() {
        final JFrame frame = new JFrame("Border layout");
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        JLabel l1 = new JLabel("L1");
        JLabel l2 = new JLabel("L2");
        JButton b = new JButton("B");
        JPanel wrappingPanel = new JPanel(new FlowLayout());
        wrappingPanel.add(b);
        panel.add(l1, BorderLayout.WEST);
        panel.add(l2, BorderLayout.EAST);
        panel.add(wrappingPanel);
        frame.add(panel);
        frame.setLocation(0, 125);
        frame.setSize(800, 100);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout test = new TestGridBagLayout();
                test.initUI1();
                test.initUI2();
            }
        });
    }

}