将按钮面板放在中心Java Swing中

时间:2017-03-22 23:40:09

标签: java swing layout-manager

我是新手,只是出于练习原因而试图制作一个简单的问答游戏。我想做的是这样的:enter image description here

这是我的第一个实现,但在那里我扩展了JFrame并在一帧中完成所有这一切然后我意识到我需要使用cardlayout从主菜单更改为播放场景,所以我将它拆分为1类主框架和其他主菜单,游戏场景,场景游戏等的类。所以我的MainMenu现在扩展了JPanel,当我向按钮面板添加按钮然后将按钮面板添加到主面板时我得到了这个:

enter image description here

它就像包含主菜单标签的面板和按钮面板彼此相邻,但我需要它们就像在第一张图片中一样。这是我的MainMenu课程:

public class MainMenu extends JPanel{

private JLabel menuTitle;
private JPanel menuTitlePanel;
private JPanel buttonPanel;

public MainMenu(){

    this.setBackground(new Color(0, 128, 43));

    //ADDING THE TITLE
    menuTitle = new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>");  
    menuTitle.setForeground(Color.BLACK);

    menuTitlePanel = new JPanel(new GridBagLayout());
    menuTitlePanel.setBackground(new Color(0, 128, 43));    

    GridBagConstraints gbcTitle = new GridBagConstraints();
    gbcTitle.weightx = 0;
    gbcTitle.weighty = 0;
    gbcTitle.gridx = 0;
    gbcTitle.gridy = 0;
    gbcTitle.gridwidth = 3;
    gbcTitle.insets = new Insets(70, 0, 0, 0);

    menuTitlePanel.add(menuTitle, gbcTitle);
    this.add(menuTitlePanel, BorderLayout.NORTH);

    buttonPanel = new JPanel(new GridBagLayout());
    buttonPanel.setBackground(new Color(0, 128, 43));

    JButton startButton = new JButton("Start");

    GridBagConstraints gbcStart = new GridBagConstraints();
    gbcStart.gridx = 1;
    gbcStart.gridy = 1;
    gbcStart.ipadx = 50;
    gbcStart.ipady = 10;
    gbcStart.gridwidth = 3;
    gbcStart.insets = new Insets(10, 0, 0, 0);

    buttonPanel.add(startButton, gbcStart);
    this.add(buttonPanel, BorderLayout.CENTER);



}
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

所以,你基本上有两个基本组,标题和按钮,这两个需要单独管理,因为它们有不同的布局要求(主要是按钮布局在中间)。

Menu

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class TestMenu {

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

    public TestMenu() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.NORTH;

            add(new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>"), gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JPanel buttons = new JPanel(new GridBagLayout());
            buttons.add(new JButton("Start"), gbc);
            buttons.add(new JButton("Show scores"), gbc);
            buttons.add(new JButton("Help"), gbc);
            buttons.add(new JButton("Exit"), gbc);

            gbc.weighty = 1;
            add(buttons, gbc);
        }

    }

}

这应该有助于解决当前的问题,但您可能还需要查看How to use CardLayout,了解有关如何在不同视图之间切换的详细信息

答案 1 :(得分:0)

我看到你没有为MainMenu设置布局,也许尝试使用Boxlayout,看看它是否提供了你想要的结果:

public MainMenu(){
    this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
    ...
    this.add(menuTitlePanel, Component.CENTER_ALIGNMENT);
    ...
    this.add(buttonPanel, Component.CENTER_ALIGNMENT);
    ...
}