问题描述
我在 Swing 中构建了一个游戏,我有两个 JPanel 用于菜单和游戏本身。我想使用 JButtons 从菜单面板切换到游戏面板,从游戏切换到菜单。但是每次从菜单切换到游戏时,我都想重新开始游戏并从菜单面板中的某个 textField 发送玩家的姓名。所以我现在需要制作游戏面板的实例。我该怎么做?
这是菜单面板:
public class MenuPanel extends JPanel {
private JPanel contentPane;
private JTextField txtName;
public MenuPanel(JPanel panel) {
setLayout(null);
contentPane = panel;
JButton btnStart = new JButton("Click to start the game");
btnStart.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
cardlayout cardlayout = (cardlayout) contentPane.getLayout();
cardlayout.show(contentPane,"Game Panel");
}
});
btnStart.setFont(new Font("Baskerville Old Face",btnStart.getFont().getStyle(),25));
btnStart.setBounds(290,510,290,70);
add(btnStart);
txtName = new JTextField();
txtName.setBounds(450,460,160,20);
add(txtName);
txtName.setColumns(10);
JLabel lblName = new JLabel("Enter your name:");
lblName.setForeground(SystemColor.text);
lblName.setFont(new Font("Bradley Hand ITC",Font.BOLD,20));
lblName.setBounds(260,180,20);
add(lblName);
JLabel lblGame = new JLabel("RUMMIKUB GAME");
lblGame.setForeground(new Color(0,0));
lblGame.setHorizontalAlignment(SwingConstants.CENTER);
lblGame.setFont(new Font("Goudy Old Style",lblGame.getFont().getStyle(),50));
lblGame.setBounds(180,100,500,140);
add(lblGame);
}
}
这是游戏面板:
public class GamePanel extends JPanel {
private JPanel contentPane;
private JButton btnReturnToMenu;
private TileButton[][] buttons = new TileButton[Constants.ROWS_IN_PLAYER_BOARD][Constants.COLUMNS_IN_PLAYER_BOARD];
private JLabel lblHello;
private TileButton btnThrownByPlayer;
private TileButton btnThrownByOpponent;
private Point clickedButton;
private JButton btnPullFromPool;
private JButton btnFinish;
private Game game;
public GamePanel(String firstPlayerName,String secondplayerName,JPanel panel) {
setLayout(null);
contentPane = panel;
clickedButton = null;
game = new Game(firstPlayerName,secondplayerName);
lblHello = new JLabel("hello " + firstPlayerName + " and " + secondplayerName);
lblHello.setBounds(10,10,250,40);
add(lblHello);
//initiate exit button.
btnReturnToMenu = new JButton("Return To Menu");
btnReturnToMenu.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
cardlayout cardlayout = (cardlayout) contentPane.getLayout();
cardlayout.show(contentPane,"Menu Panel");
}
});
btnReturnToMenu.setFont(new Font("Mongolian Baiti",16));
btnReturnToMenu.setBounds(10,70,200,40);
add(btnReturnToMenu);
// initiate finish button.
btnFinish = new JButton("Finish");
btnFinish.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (btnFinish.isEnabled()) {
if (game.finish()) {
JOptionPane.showMessageDialog(null,"You finish the game.\nCongratulations,you are the winner!!!");
cardlayout cardlayout = (cardlayout) contentPane.getLayout();
cardlayout.show(contentPane,"Finish Panel");
} else {
btnFinish.setEnabled(false);
JOptionPane.showMessageDialog(null,"You didn't finish the game yet");
}
}
}
});
btnFinish.setFont(new Font("Mongolian Baiti",16));
btnFinish.setBounds(750,50,40);
add(btnFinish);
}
}
这是框架:
public class MainFrame extends JFrame {
private JPanel contentPane;
private GamePanel gamePanel;
private MenuPanel menuPanel;
private FinishPanel finishPanel;
private MainFrame() {
//super();
setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
setBounds(233,55,900,627);
setResizable(false);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0,0));
contentPane.setLayout(new cardlayout());
menuPanel = new MenuPanel(contentPane);
gamePanel = new GamePanel("","",contentPane);
finishPanel = new FinishPanel(contentPane);
contentPane.add(menuPanel,"Menu Panel");
contentPane.add(gamePanel,"Game Panel");
contentPane.add(finishPanel,"Finish Panel");
getContentPane().add(contentPane,BorderLayout.CENTER);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokelater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)