2

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

在此处输入图像描述.

我正在努力用 Java Swing 做到这一点。我已经尝试过 BorderLayout,使用 BorderLayout.WEST、BorderLayout.CENTER、BorderLayout.EAST 但这会使按钮填充空间: 在此处输入图像描述

这是我用于此的代码:

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,我最接近的是将它们分开,但不拥抱两侧:

在此处输入图像描述

代码:

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);

有任何想法吗?

4

3 回答 3

4

尝试GridLayout使用LEFT, CENTER&RIGHT标签。

    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));

图片

SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/14501446/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridLayout(1, 0));
        p.add(new JLabel("Test", JLabel.LEFT));
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("Test"));
        p.add(buttonPanel);
        p.add(new JLabel("Test", JLabel.RIGHT));
        f.add(p, BorderLayout.NORTH);
        f.add(new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 120);
            }
        }, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
于 2013-01-24T12:31:17.293 回答
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();
            }
        });
    }

}
于 2013-01-24T12:33:56.443 回答
0

你可以像这样添加一个“骗子”:

JButton buttonNeeded = new JButton();
   window.add(buttonNeeded);
JButton cheaterButton = new JButton();
   cheaterButton.setEnabled(false);
   window.add(cheaterButton);
   

填满屏幕,cheaterButton但用户无法单击或查看它。对所有事情都不是很好,但它JButton至少适用于添加 s。

于 2021-12-20T20:51:02.417 回答