BoxLayout中如何对齐JLabel?

3

我已经花了几天时间搜索答案,但始终找不到错误所在。我的目的是让顶部JLabel(称为 display)右对齐,底部JLabel(称为 notice)左对齐。然而,两者都无法实现。根据我的阅读,我的代码应该可以实现,但事实并非如此。请帮忙解决一下?

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class Calculator {

    private static JButton clear, add, subtract, multiply, divide, equals, point, zero, one, two, three, four, five, six, seven, eight, nine;
    private static JLabel display, notice, blank1, blank2, blank3;
    private static JPanel mainPanel, buttonPanel, topLabel, bottomLabel;

    public static void goGUI() {

        JFrame frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(600,300));
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        frame.setContentPane(mainPanel);
        Border empty = BorderFactory.createEmptyBorder(10,10,10,10);
        mainPanel.setBorder(empty);

        buttonPanel = new JPanel(new GridLayout(5,4, 5,5));
        Border buttonBorder = BorderFactory.createEmptyBorder(10,0,10,0);
        buttonPanel.setBorder(buttonBorder);

        topLabel = new JPanel();
        bottomLabel = new JPanel();

        clear = new JButton("C");
        add = new JButton("+");
        subtract = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("/");
        equals = new JButton("=");
        point = new JButton(".");
        zero = new JButton("0");
        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");

        // Here I added ActionListeners to all the buttons...

        display = new JLabel("0");
        display.setAlignmentX(Component.RIGHT_ALIGNMENT);
        notice = new JLabel("*Maximum 19 digits - Order of operations not taken into account*");
        notice.setAlignmentX(Component.LEFT_ALIGNMENT);
        blank1 = new JLabel();
        blank2 = new JLabel();
        blank3 = new JLabel();

        buttonPanel.add(clear);
        buttonPanel.add(blank1);
        buttonPanel.add(blank2);
        buttonPanel.add(blank3);
        buttonPanel.add(seven);
        buttonPanel.add(eight);
        buttonPanel.add(nine);
        buttonPanel.add(divide);
        buttonPanel.add(four);
        buttonPanel.add(five);
        buttonPanel.add(six);
        buttonPanel.add(multiply);
        buttonPanel.add(one);
        buttonPanel.add(two);
        buttonPanel.add(three);
        buttonPanel.add(subtract);
        buttonPanel.add(zero);
        buttonPanel.add(point);
        buttonPanel.add(equals);
        buttonPanel.add(add);

        topLabel.add(display);
        bottomLabel.add(notice);

        mainPanel.add(topLabel);
        mainPanel.add(buttonPanel);
        mainPanel.add(bottomLabel);

        frame.pack();
        frame.setVisible(true);

    } //end goGUI

    //ActionListener classes went here...

    public static void main(String[] args) {

        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } 
        catch (Exception e) {}

        goGUI();

    } //end main
} //end Calculator

为了更清晰地表达,我删除了所有ActionListener相关的内容。但是这是我无法解决的布局问题。

3个回答

4

我添加了一些代码,因为我喜欢BoxLayout。几个小时前,我也遇到了几乎相同的问题,即如何对齐组件。

如果我们想要实现这样的效果:

//  +----------------------------------+
//  |  Label1  |            |  Label2  |
//  +----------------------------------+

你只需要在两者之间添加一个"Box.createHorizontalGlue()"即可。
如果我们想要像这样的效果:
//  +----------------------------------+
//  |  Label1  |                       |
//  |----------+            +----------|
//  |                       |  Label2  |
//  +----------------------------------+

我们需要将标签放置在两个不同的JPanel中,并为它们设置不同的AlignmentX。这是因为在布局中,所有组件都应具有相同的alignmentX。为了避免此限制,我们可以采取以下措施:
//  +-----------------------------------------+
//  |  Label1  |   label1 is inside a JPanel  |
//  +----------+ - - - - - - - - - - - - - - -+
//  + - - - - - - - - - - - - - - -+----------+
//  |  label2 also                 |  Label2  |
//  +-----------------------------------------+

这可以通过以下代码实现:

mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));

JLabel lbl1 = new JLabel("Label1");
JPanel p1 = new JPanel();
p1.setOpaque(false);
p1.setLayout(new BorderLayout(0, 0));
p1.add(lbl1, BorderLayout.CENTER);

JLabel lbl2 = new JLabel("Label1");
JPanel p2 = new JPanel();
p2.setOpaque(false);
p2.setLayout(new BorderLayout(0, 0));
p2.add(lbl2, BorderLayout.CENTER);

mainPanel.add(p1);
mainPanel.add(p2);

我希望这对您有所帮助。


4
考虑使用BorderLayout而不是默认的FlowLayout,使topLabel的内容填充它。
topLabel.setLayout(new BorderLayout());

接下来,请确保您将显示器的水平对齐设置为右侧,而不是alignmentX:

display.setHorizontalAlignment(SwingConstants.RIGHT);

类似的更改还应该针对您的通知JLabel及其容器进行。

对于这个问题,topLabel.add(display, BorderLayout.EAST);display.setHorizontalAlignment(SwingConstants.RIGHT);之间有什么区别吗?在这种情况下,它们得到的结果是相同的。+1 如果解决方案有效! - alain.janinm
@alain:是的,这是有很大的区别的。如果你将它添加到BorderLayout.EAST中,JLabel不会扩展。请给标签加上边框以查看区别。 - Hovercraft Full Of Eels

1
您可以简单地使用 BorderLayout()

public static void goGUI() {

        ....

        topLabel = new JPanel(new BorderLayout());
        bottomLabel = new JPanel(new BorderLayout());

        ....

        topLabel.add(display, BorderLayout.EAST);
        bottomLabel.add(notice, BorderLayout.WEST);


    }

同时删除这些调用:

 display.setAlignmentX(Component.RIGHT_ALIGNMENT);
 notice.setAlignmentX(Component.LEFT_ALIGNMENT);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接