簡體   English   中英

將按鈕面板放在中心Java Swing中

[英]Placing button panel in center Java Swing

我是新手,只是出於練習原因而試圖制作一個簡單的問答游戲。 我想要做的是這樣的: 在此輸入圖像描述

這是我的第一個實現,但在那里我擴展JFrame並在一幀中完成所有這一切然后我意識到我需要使用cardlayout從主菜單更改為播放場景,所以我將它分成1類主框架和其他類為主菜單,播放場景,場景游戲等。所以我的MainMenu現在擴展JPanel,當我向按鈕面板添加按鈕,然后將按鈕面板添加到主面板我得到了這個:

在此輸入圖像描述

它就像包含主菜單標簽和按鈕面板的面板彼此相鄰,但我需要它們就像在第一張圖片中一樣。 這是我的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);



}
}

任何幫助將不勝感激。

所以,你基本上有兩個基本組,標題和按鈕,這兩個需要單獨管理,因為它們有不同的布局要求(主要是按鈕布局在中間)。

菜單

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);
        }

    }

}

這應該有助於解決當前的問題,但您可能還想了解如何使用CardLayout獲取有關如何在不同視圖之間切換的詳細信息

我看到你沒有為MainMenu設置布局,也許嘗試Boxlayout,看看它是否提供你所追求的結果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM