[英]Java- CardLayout how to switch cards from the card that is switching
我正在努力熟悉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);
}
});
}
}
我知道我可以用標簽上的按鈕做類似的事情,但我只考慮了兩個按鈕,所以在那個階段它需要更長的時間。
我是以正確的方式來做這件事的嗎? 你能糾正我在代碼中犯的任何錯誤嗎?
每當我運行這個我得到一個錯誤
您的申請是在這里投擲NPE
gbc.insets = new Insets(35, 50, 0, 50);
因為你還沒有初始化你的GridBagConstraints
gbc
。
此外,您並排看到兩個面板的原因是,即使您創建了CardLayout
,也忽略了將它用於您的cardPanel
。 因此,您仍在使用JPanel
的默認FlowLayout
。 你可以這樣做:
cardPanel = new JPanel(cl);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.