java 在 BoxLayout 中调整 JButton 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15394741/
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 19:26:35  来源:igfitidea点击:

Resize JButtons in a BoxLayout

javaswingjbuttonboxlayout

提问by Glenndisimo

I'm trying to make a simple menu for my game. I have 4 buttons in the center and I want to make them a little bit bigger and center them.

我正在尝试为我的游戏制作一个简单的菜单。我在中心有 4 个按钮,我想让它们更大一点并将它们居中。

The last part worked but I can't seem to call any of my JButtonsand do a .setSize/ .setPreferedSize(new Dimension())on it.

最后一部分起作用了,但我似乎无法调用我的任何一个JButtons并对其执行.setSize/.setPreferedSize(new Dimension())操作。

public class mainMenu extends JFrame {
private JButton start, highscore, help, stoppen;


public mainMenu() {
    super("Master Mind");
    maakComponenten();
    maakLayout();
    toonFrame();
}

private void maakComponenten() {
    start = new JButton("Start");
    start.setBackground(Color.gray);
    highscore = new JButton("Higscores");
    help = new JButton("Help");
    stoppen = new JButton("Stoppen");
}

private void maakLayout() {
    JPanel hoofdmenu = new JPanel();
    hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.Y_AXIS ));
    hoofdmenu.add(start);
    start.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(highscore);
    highscore.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(help);
    help.setAlignmentX(CENTER_ALIGNMENT);
    hoofdmenu.add(stoppen);
    stoppen.setAlignmentX(CENTER_ALIGNMENT);
    super.add(hoofdmenu);
}

private void toonFrame() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(500,500);

}

public static void main(String[] args) {
    new mainMenu();
}
}

采纳答案by Java42

As an example, to change the size of the "Start" button,

例如,要更改“开始”按钮的大小,

change :

改变 :

    start1 = new JButton("Start");

to

    start1 = new JButton("Start") {
        {
            setSize(150, 75);
            setMaximumSize(getSize());
        }
    };

回答by user1854369

The problem is that JFrames use BorderLayout by default, which means that your JPanel will naturally fill the space.

问题是 JFrames 默认使用 BorderLayout,这意味着您的 JPanel 会自然地填充空间。

Before adding your JPanel, call the following code to change the JFrame's layout to null and use the JPanel's settings instead.

在添加 JPanel 之前,调用以下代码将 JFrame 的布局更改为 null 并改用 JPanel 的设置。

this.setLayout(null);
JPanel hoofdmenu = new JPanel();
hoofdmenu.setBounds(0,0, 400, 100);

Alternatively, you could set the maximum size of the JButtons

或者,您可以设置 JButtons 的最大尺寸

Dimension maxSize = new Dimension(100, 100);
start.setMaximumSize(maxSize);
highscore.setMaximumSize(maxSize);
help.setMaximumSize(maxSize);
stoppen.setMaximumSize(maxSize);

回答by ob103ninja

Here's another example following behind the previous two - I'm making a soundboard program, and this is actually a sample from it - The JPanel actually is needed, agreeing to the second post.

这是前两个之后的另一个示例 - 我正在制作一个音板程序,这实际上是它的一个示例 - 实际上需要 JPanel,同意第二篇文章。

    JFrame frame = new JFrame();
    JPanel menuPanel = new JPanel();
    JButton Button1 = new JButton("<BUTTON NAME 1>");
    Button1.setSize(80, 30);
    Button1.setLocation(4, 4);
    JButton Button2 = new JButton("<BUTTON NAME 2>");
    Button2.setSize(80, 30);
    Button2.setLocation(90, 4);

Ah, and another thing - You created the buttons in a different block from the second piece of code. Doing that causes the other blocks to not see it. You need to declare them outside the block so all the blocks can see them.

啊,还有一件事 - 您在与第二段代码不同的块中创建了按钮。这样做会导致其他块看不到它。您需要在块外声明它们,以便所有块都可以看到它们。