我无法将我创建的 JPanel 类添加到我的容器类中。老实说,我不确定这是否是创建菜单的最佳方法。我问过教授和助教,他们都告诉我不同的事情。
编辑:为了澄清,问题是没有显示在 JPanel 类中制作的主菜单。我想我在容器类中正确添加了它,但我不完全确定。
这是我试图添加到容器中的 JPanel 类:
package.model;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MainMenu extends JPanel {
private static MainMenu instance;
private JPanel mainMenu;
private JButton btnSinglePlayer, btnMultiPlayer, btnScoreBoard, btnQuit;
private MainMenu() {
mainMenu();
singlePlayer();
multiPlayer();
scoreBoard();
quit();
}
public static MainMenu getMenuInstance() {
if (instance == null) {
instance = new MainMenu();
}
return instance;
}
public void construct() {
mainMenu();
singlePlayer();
multiPlayer();
scoreBoard();
quit();
}
private JPanel mainMenu() {
mainMenu = new JPanel();
mainMenu.setLayout(null);
return mainMenu;
}
private JButton singlePlayer() {
btnSinglePlayer = new JButton("SinglePlayer");
btnSinglePlayer.setBounds(365, 210, 170, 55);
mainMenu.add(btnSinglePlayer);
return btnSinglePlayer;
}
private JButton multiPlayer() {
btnMultiPlayer = new JButton("MultiPlayer");
btnMultiPlayer.setBounds(365, 300, 170, 55);
mainMenu.add(btnMultiPlayer);
return btnMultiPlayer;
}
private JButton scoreBoard() {
btnScoreBoard = new JButton("ScoreBoards");
btnScoreBoard.setBounds(365, 411, 170, 55);
mainMenu.add(btnScoreBoard);
return btnScoreBoard;
}
private JButton quit() {
btnQuit = new JButton("Quit");
btnQuit.setBounds(365, 500, 170, 55);
mainMenu.add(btnQuit);
return btnQuit;
}
}
这是我的容器类:
package view;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import model.MainMenu;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
private static JFrame mainContainer = new JFrame("Checkers");
private JPanel card = new JPanel(new CardLayout());
private CardLayout cardLayout = (CardLayout) (card.getLayout());
private JPanel homeCard, singlePlayerCard, multiPlayerCard, scoreBoardCard;
private JPanel mainMenu = MainMenu.getMenuInstance();
public MainFrame() {
}
private void addComponentToPane(Container pane) {
//mainMenu.construct();
homeCard = new JPanel();
singlePlayerCard = new JPanel();
multiPlayerCard = new JPanel();
scoreBoardCard = new JPanel();
homeCard.add(mainMenu);
card.add(homeCard, "homeCard");
cardLayout.show(card, "homeCard");
pane.add(card);
}
public static void createAndShowGUI() {
MainFrame frame = new MainFrame();
frame.addComponentToPane(mainContainer.getContentPane());
mainContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainContainer.pack();
mainContainer.setSize(920, 650);
mainContainer.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
mainContainer.setLocation(dim.width / 2 - mainContainer.getSize().width
/ 2, dim.height / 2 - mainContainer.getSize().height / 2);
}
}
我想要一个 JPanel 类的原因,而且我将拥有不止一个 btw,是因为我希望该类启动我的游戏线程。而且我不希望通过视图启动游戏线程。如果这是一个糟糕的设计,请告诉我。
顺便说一句,我有一个启动器类,我认为这与发布无关。我知道我做对了,我和我大学的 3-4 位 TA 的错误似乎找不到。