0
我想熟悉CardLayout,因此我正在製作一個模擬遊戲菜單。這個菜單應該有三個按鈕,但是佈局部分很簡單。Java- CardLayout如何從正在切換的卡中切換卡片
所以,我想要做的就是用一個帶有三個按鈕的菜單啓動它。單人遊戲按鈕應該將用戶看到的內容更改爲單個按鈕,這可以將其更改回原始菜單。
我在網上跟着一個例子,然後對此應用了相同的方法。但是,菜單本身就是一張卡片,它是更換卡片的命令來自哪裏,而不是一個單獨的容器。
每當我運行此我得到一個錯誤:
public class GameMenuCards extends JFrame{
private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;
private GridBagConstraints gbc;
public GameMenuCards(){
initUI();
}
public void initUI(){
//set the properties for the window
setTitle("Game Menu With Cards");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
cardPanel = new JPanel();
cl = new CardLayout();
JPanel game = new JPanel();
game.setBackground(Color.BLACK);
//the menu panel
JPanel menu = new JPanel();
menu.setLayout(new GridBagLayout());
menu.setBackground(Color.BLACK);
cardPanel.add(menu, "1");
cardPanel.add(game, "2");
//set up the buttons for the menu
JButton single = new JButton("Single Player");
single.setPreferredSize(new Dimension(300, 30));
single.setBackground(Color.GRAY);
single.setForeground(Color.CYAN);
single.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
JButton multi = new JButton("Multi Player");
multi.setPreferredSize(new Dimension(300, 30));
multi.setBackground(Color.GRAY);
multi.setForeground(Color.CYAN);
multi.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
JButton score = new JButton("High Scores");
score.setPreferredSize(new Dimension(300, 30));
score.setBackground(Color.GRAY);
score.setForeground(Color.CYAN);
score.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
gbc = new GridBagContraints();
//add everything to the menu
gbc.insets = new Insets(35, 50, 0, 50);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx = 1;
gbc.gridy = 1;
menu.add(single, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
label(menu);
gbc.gridx = 1;
gbc.gridy = 3;
menu.add(multi, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
label(menu);
gbc.gridx = 1;
gbc.gridy = 5;
gbc.insets = new Insets(35, 50, 35, 50);
menu.add(score, gbc);
single.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
currentCard = 2;
cl.show(cardPanel, "" + (currentCard));
}
});
JButton returnBut = new JButton("Back To Menu");
returnBut.setPreferredSize(new Dimension(300, 30));
returnBut.setBackground(Color.GRAY);
returnBut.setForeground(Color.CYAN);
returnBut.setBorder(BorderFactory.createLineBorder(Color.CYAN, 3));
returnBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
currentCard = 1;
cl.show(cardPanel, "" + (currentCard));
}
});
game.add(returnBut);
getContentPane().add(cardPanel);
}
public void label(Container c){
JLabel j1 = new JLabel();
j1.setPreferredSize(new Dimension(300, 40));
j1.setBackground(Color.BLACK);
c.add(j1, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
GameMenuCards gm = new GameMenuCards();
gm.setVisible(true);
}
});
}
}
我知道,我可以做類似的事情與按鈕的標籤,但我只用了思想兩個按鈕,所以在那個階段它會花更長的時間。
我對此有何看法?你能糾正我在代碼中犯的任何錯誤嗎?
我不能相信我忘了那個謝謝!我編輯了問題中的代碼以添加它。但現在,當我運行它時,我並排兩個面板,而不是我想要的。你能幫忙嗎? – 2013-03-15 17:39:24
謝謝。在發佈問題之前,我應該徹底檢查一下,對不起。 – 2013-03-15 19:49:38
沒問題,yw :) – Reimeus 2013-03-15 19:50:13