如何在BoxLayout中居中JLabel和JButton [英] How to center JLabel and JButton in BoxLayout

查看:59
本文介绍了如何在BoxLayout中居中JLabel和JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建具有难度级别的简单菜单

I want to create simple menu with difficult levels

接下来的几行代码是构造函数.

Next few lines of code is constructor.

super();

setMinimumSize(new Dimension(600, 300));

setMaximumSize(new Dimension(600, 300));

setPreferredSize(new Dimension(600, 300));

setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

addButtons();

方法 addButtons()添加您可以在屏幕快照上看到的按钮:

Method addButtons() add buttons which you can see on screenshoot:

add(Box.createVerticalGlue());

addLabel("<html>Current level <b>" + Game.instance()
                                         .getLevelString() +
         "</b></html>");

add(Box.createVerticalGlue());

addButton("Easy");

add(Box.createVerticalGlue());

addButton("Normal");

add(Box.createVerticalGlue());

addButton("Hard");

add(Box.createVerticalGlue());

addButton("Back");

add(Box.createVerticalGlue());

方法 addButton()

private void addButton(String text)
{
    JButton button = new JButton(text);
    button.setAlignmentX(JButton.CENTER_ALIGNMENT);
    button.setFocusable(false);

    add(button);
}

addLabel()

private void addLabel(String text)
{
    JLabel label = new JLabel(text, JLabel.CENTER);

    add(label);
}

我不知道如何将所有元素对齐到中心.这对我来说是个问题.另一个问题是,当我更改 JLabel 上的困难级别文本时,可以更改为简单的当前级别EASY".然后 JButtons 向右移动很多像素,我不知道为什么.

I don't know how to align all elements to center. It's problem for me. Additional problem is when I change difficult level text on JLabel can change into, for simple 'Current level EASY'. Then JButtons are moving a lot of pixels right and I don't know why.

推荐答案

public JLabel(字符串文本,int horizo​​ntalAlignment)中的第二个参数用于确定标签的文本位置.您需要通过 setAlignmentX 方法设置 JLabel 组件的资格.

Second parameter in the public JLabel(String text, int horizontalAlignment) is for determine the text position of the label. You need to set the JLabel component's aligment by setAlignmentX method.

private void addLabel(String text) {
    JLabel label = new JLabel(text, JLabel.CENTER);
    label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
    add(label);
}

您的第二期很奇怪.我不知道为什么会这样,但是我认为为按钮创建第二个面板将解决您的问题.

Your second issue is weird. I don't know why this is happening but I think creating a second panel for buttons will solve your issue.

在构造函数中使用边框布局:

in constructor use border layout:

super();

//set size

setLayout(new BorderLayout());

addButtons();

addButtons()方法:

//you can use empty border if you want add some insets to the top
//for example: setBorder(new EmptyBorder(5, 0, 0, 0));

addLabel("<html>Current level <b>" + Game.instance()
                                     .getLevelString() +
     "</b></html>");

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

buttonPanel.add(Box.createVerticalGlue());

buttonPanel.add(createButton("Easy"));

buttonPanel.add(Box.createVerticalGlue());

//Add all buttons

add(buttonPanel, BorderLayout.CENTER);

createButton()方法

private JButton createButton(String text)
{
    JButton button = new JButton(text);
    button.setAlignmentX(JButton.CENTER_ALIGNMENT);
    button.setFocusable(false);

    return button;
}

addLabel()方法

private void addLabel(String text)
{
    JLabel label = new JLabel(text, JLabel.CENTER);
    add(label, BorderLayout.NORTH);
}

这篇关于如何在BoxLayout中居中JLabel和JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆