两个 JLabel 垂直居中

发布于 2024-11-27 21:34:01 字数 377 浏览 1 评论 0原文

我必须创建两个 JLabel,并且它们应该在 JFrame 中位于彼此的中心和正下方。我一直在使用 swing 的 gridbaglayout,但我不知道如何做到这一点。

terminalLabel = new JLabel("No reader connected!", SwingConstants.CENTER);  
terminalLabel.setVerticalAlignment(SwingConstants.TOP); 

cardlabel = new JLabel("No card presented", SwingConstants.CENTER); 
cardlabel.setVerticalAlignment(SwingConstants.BOTTOM);

I've got to create two JLabels, and the should been positioned center and right under each other in the JFrame. I've beeing using the gridbaglayout from swing, but I can't figure out how to do this.

terminalLabel = new JLabel("No reader connected!", SwingConstants.CENTER);  
terminalLabel.setVerticalAlignment(SwingConstants.TOP); 

cardlabel = new JLabel("No card presented", SwingConstants.CENTER); 
cardlabel.setVerticalAlignment(SwingConstants.BOTTOM);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆萌少年 2024-12-04 21:34:01

使用 BoxLayout。在下面的代码中,Box 类是一个便利类,它创建一个使用 BoxLayout 的 JPanel:

import java.awt.*;
import javax.swing.*;

public class BoxExample extends JFrame
{
    public BoxExample()
    {
        Box box = Box.createVerticalBox();
        add( box );

        JLabel above = new JLabel("Above");
        above.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        box.add( above );

        JLabel below = new JLabel("Below");
        below.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        box.add( below );
    }

    public static void main(String[] args)
    {
        BoxExample frame = new BoxExample();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

Use a BoxLayout. In the code below the Box class is a convenience class that creates a JPanel that uses a BoxLayout:

import java.awt.*;
import javax.swing.*;

public class BoxExample extends JFrame
{
    public BoxExample()
    {
        Box box = Box.createVerticalBox();
        add( box );

        JLabel above = new JLabel("Above");
        above.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        box.add( above );

        JLabel below = new JLabel("Below");
        below.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        box.add( below );
    }

    public static void main(String[] args)
    {
        BoxExample frame = new BoxExample();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
一向肩并 2024-12-04 21:34:01

使用 FlowLayout 和 GridLayout

enclosingPanel = new JPanel();
enclosingPanel.setLayout( new FlowLayout( FlowLayout.CENTER) );
labelPanel = new JPanel();
labelPanel.setLayout( new GridLayout( 2 , 1 ) );  // 2 rows 1 column
enclosingPanel.add( labelPanel );
frame.add( enclosingPanel );  // frame = new JFrame();
setPreferredSize( new Dimension( 200 , 200) );
// do other things

使用此方法,您可以将 2 个 JLabels 放置在中心并彼此下方。您还可以设置两个标签之间的垂直间隙。 #GridLayout(int, int, int, int)

Using FlowLayout and GridLayout

enclosingPanel = new JPanel();
enclosingPanel.setLayout( new FlowLayout( FlowLayout.CENTER) );
labelPanel = new JPanel();
labelPanel.setLayout( new GridLayout( 2 , 1 ) );  // 2 rows 1 column
enclosingPanel.add( labelPanel );
frame.add( enclosingPanel );  // frame = new JFrame();
setPreferredSize( new Dimension( 200 , 200) );
// do other things

Using this method you can place 2 JLabels in the center and beneath each other. You can also set vertical gap between the 2 labels . #GridLayout(int, int, int, int)

回心转意 2024-12-04 21:34:01

您应该为用于将标签添加到容器的 GridBagCOnstraint 指定正确的锚点 (CENTER)。

You should specify proper anchor (CENTER) for the GridBagCOnstraints you use to add the labels to container.

九歌凝 2024-12-04 21:34:01

您必须使用 GridBagConstraints 。添加第二个标签时更改约束的 gridY 值,它将放置在第一个标签下。

试试这个:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();

    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.CENTER;
    JLabel terminalLabel = new JLabel("No reader connected!");  
    frame.add(terminalLabel,constraints);

    constraints.gridy = 1;
    JLabel cardlabel = new JLabel("No card presented"); 
    frame.add(cardlabel,constraints);

    frame.setVisible(true);

另请阅读以下内容:如何使用 GridBagLayout

You have to use GridBagConstraints. Change gridY value of constraints while adding second label and it will be placed under first one.

Try this:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();

    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.CENTER;
    JLabel terminalLabel = new JLabel("No reader connected!");  
    frame.add(terminalLabel,constraints);

    constraints.gridy = 1;
    JLabel cardlabel = new JLabel("No card presented"); 
    frame.add(cardlabel,constraints);

    frame.setVisible(true);

Also read this: How to Use GridBagLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文