java 在 BoxLayout 中对齐 JLabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11602237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:44:36  来源:igfitidea点击:

Aligning JLabels in BoxLayout

javaswingjlabelboxlayout

提问by charlieshades

I've been searching around for days trying to find the answer to this, and I can't find out what's wrong. What I want to do is make it so the top JLabel (called display) align to the right and the bottom JLabel (called notice) to align to the left. Neither seems to want to do either. From what I've read, what I have should work, but it doesn't. Help?

我已经搜索了几天试图找到这个问题的答案,但我找不到问题所在。我想要做的是使顶部 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

I removed all ActionListener stuff for clarity. But this is the layout that I can't fix.

为清楚起见,我删除了所有 ActionListener 内容。但这是我无法修复的布局。

回答by Hovercraft Full Of Eels

Consider having topLabel not use the default FlowLayout but rather something that makes its contents fill it up such as BorderLayout.

考虑让 topLabel 不使用默认 FlowLayout,而是使用使其内容填充它的东西,例如 BorderLayout。

topLabel.setLayout(new BorderLayout());

Next make sure that you set the display's horizontal alignment, not its alignmentX to the right:

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

display.setHorizontalAlignment(SwingConstants.RIGHT);

Similar changes should be made for your notice JLabel and its container.

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

回答by Gianmarco

I add few line of code because I like BoxLayout. I have almost the same problems in align components few hours ago.

我添加了几行代码,因为我喜欢 BoxLayout。几个小时前,我在对齐组件中遇到了几乎相同的问题。

If we want to acheive something like this

如果我们想实现这样的目标

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

You have only to put a "Box.createHorizontalGlue()" between the two.

您只需在两者之间放置一个“Box.createHorizo​​ntalGlue()”。

If instead we want something like this:

相反,如果我们想要这样的东西:

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

We have to put the labels inside two differen JPanel, setting a different AlignmentX to them. This is because in the layout all the components should have the same alignmentX. To avoid this restriction we can do something like this:

我们必须将标签放在两个不同的 JPanel 中,为它们设置不同的 AlignmentX。这是因为在布局中所有组件都应该具有相同的alignmentX。为了避免这种限制,我们可以这样做:

//  +-----------------------------------------+
//  |  Label1  |   label1 is inside a JPanel  |
//  +----------+ - - - - - - - - - - - - - - -+
//  + - - - - - - - - - - - - - - -+----------+
//  |  label2 also                 |  Label2  |
//  +-----------------------------------------+

(don't mind about the space between the two panel, is only for graphic) This can be done with this code:

(不要介意两个面板之间的空间,仅用于图形)这可以通过以下代码完成:

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

I hope this is useful.

我希望这是有用的。

回答by alain.janinm

You can simply use BorderLayout():

您可以简单地使用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);


    }

Also remove these calls :

同时删除这些调用:

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