将按钮面板置于中心 Java Swing
Placing button panel in center Java Swing
我是 swing 的新手,出于练习的原因,我正在尝试制作一个简单的问答游戏。我想做的是这样的:
这是我的第一个实现,但我扩展了 JFrame 并将所有这些都放在一个框架中然后我意识到我需要使用 cardlayout 从主菜单更改为播放场景所以我将它拆分为 1 class 主框架和其他 classes 用于主菜单、播放场景、游戏结束场景等。所以我的 MainMenu 现在扩展了 JPanel,当我将按钮添加到按钮面板,然后将按钮面板添加到主面板时,我得到了这个:
它就像包含主菜单标签的面板和按钮面板彼此相邻,但我需要它们,就像第一张图片一样。这是我的主菜单 class:
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);
}
}
我们将不胜感激。
我看到你还没有为 MainMenu 设置布局,也许试试 Boxlayout 看看它是否提供你想要的结果:
public MainMenu(){
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
...
this.add(menuTitlePanel, Component.CENTER_ALIGNMENT);
...
this.add(buttonPanel, Component.CENTER_ALIGNMENT);
...
}
所以,你基本上有两个基本组,标题和按钮,这两个需要单独管理,因为它们有不同的布局要求(主要是按钮放在中间)。
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 以了解有关如何在不同视图之间切换的详细信息
我是 swing 的新手,出于练习的原因,我正在尝试制作一个简单的问答游戏。我想做的是这样的:
这是我的第一个实现,但我扩展了 JFrame 并将所有这些都放在一个框架中然后我意识到我需要使用 cardlayout 从主菜单更改为播放场景所以我将它拆分为 1 class 主框架和其他 classes 用于主菜单、播放场景、游戏结束场景等。所以我的 MainMenu 现在扩展了 JPanel,当我将按钮添加到按钮面板,然后将按钮面板添加到主面板时,我得到了这个:
它就像包含主菜单标签的面板和按钮面板彼此相邻,但我需要它们,就像第一张图片一样。这是我的主菜单 class:
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);
}
}
我们将不胜感激。
我看到你还没有为 MainMenu 设置布局,也许试试 Boxlayout 看看它是否提供你想要的结果:
public MainMenu(){
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
...
this.add(menuTitlePanel, Component.CENTER_ALIGNMENT);
...
this.add(buttonPanel, Component.CENTER_ALIGNMENT);
...
}
所以,你基本上有两个基本组,标题和按钮,这两个需要单独管理,因为它们有不同的布局要求(主要是按钮放在中间)。
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 以了解有关如何在不同视图之间切换的详细信息