Java JFrame 将三个按钮放在中间的一个下面

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

JFrame position three buttons one below another in the center

javaswinguser-interfacejbuttonlayout-manager

提问by

So I would like to have three JButtonsall on top of each other, but not to large in width or height either. I am not too familiar with Java's layouts, and to be honest I am not too keen on them. Please view the image a code below to explain to me how, thanks.

所以我想把三个JButtons都放在一起,但宽度或高度也不大。我不太熟悉 Java 的布局,老实说我不太热衷于它们。请查看下面的图像代码向我解释如何,谢谢。

enter image description here

在此处输入图片说明

package com.aqagame.harrykitchener;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main 
{

    private JButton playGame, playerNames, exitGame;

    public Main()
    {

        JPanel mainCard = new JPanel(new BorderLayout(8, 8));
        playGame = new JButton("Play Game");
        playerNames = new JButton("Player Names");
        exitGame = new JButton("Exit Game");


        mainCard.add(playGame, BorderLayout.NORTH);
        mainCard.add(playerNames, BorderLayout.CENTER);
        mainCard.add(exitGame, BorderLayout.SOUTH);

        JFrame window = new JFrame("Harry's AQA game");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(mainCard);
        window.setSize(900, 800);
        window.setLocationRelativeTo(null);
        window.setVisible(true);

    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }

}

回答by DerStrom8

Check out the Java Documentationfor the different layout managers. I know you're not familiar with them, which is why you should probably start. Once you get used to them there is no end to their benefits. There is a lot of excellent information in the documentation and I am sure you will learn a lot. Personally, I recommend looking at the Box Layout:

查看不同布局管理器的Java 文档。我知道你不熟悉它们,这就是为什么你应该开始。一旦你习惯了它们,它们的好处就无穷无尽。文档中有很多很好的信息,我相信你会学到很多东西。就个人而言,我建议查看Box Layout

enter image description here

在此处输入图片说明

回答by NESPowerGlove

To not use Box or GridBag, I think a combination such as this may work out:

为了不使用 Box 或 GridBag,我认为这样的组合可能会奏效:

  • Have main panel (let's call it A) have a BorderLayout
  • Create another panel (let's call it B), with a FlowLayout, with constructor aligning components to the center
  • Create another panel (let's call it C), with a GridLayout, 1 column 3 rows
  • Add each button to a new JPanel with a FlowLayout (1 JPanel per button, so buttons are wrapped by a FlowLayout), and then add each of those JPanels to C
  • Add C to B
  • Add B to A (center position)
  • 让主面板(我们称之为 A)有一个 BorderLayout
  • 创建另一个面板(我们称之为 B),使用 FlowLayout,构造函数将组件对齐到中心
  • 创建另一个面板(我们称之为 C),使用 GridLayout,1 列 3 行
  • 将每个按钮添加到具有 FlowLayout 的新 JPanel(每个按钮 1 个 JPanel,因此按钮由 FlowLayout 包装),然后将这些 JPanel 中的每一个添加到 C
  • 将 C 添加到 B
  • 将 B 添加到 A(中心位置)

I think this should cause buttons to be on top of each other with small amount of padding while not being stretched widthwise and while appearing in the center of the screen.

我认为这应该会导致按钮彼此重叠并带有少量填充,同时不会被横向拉伸并且出现在屏幕中央。

回答by camickr

  1. Create JPanel that uses a GridLayoutand add all the buttons to the panel. The GridLayout will automactially size the buttons to be the same size.

  2. Set the layout managerof your main window to use a GridBagLayout.

  3. add the panel to the main window using the default GridBagConststraints. Then the panel will automatically be centered both horizontally and vertically.

  1. 创建使用 a 的 JPanelGridLayout并将所有按钮添加到面板。GridLayout 将自动将按钮的大小调整为相同的大小。

  2. layout manager主窗口的 设置为使用GridBagLayout.

  3. 使用默认的 GridBagConststraints 将面板添加到主窗口。然后面板将自动水平和垂直居中。