gpt4 book ai didi

Java-CardLayout 如何从正在切换的卡片中切换卡片

转载 作者:行者123 更新时间:2023-12-02 07:11:37 26 4
gpt4 key购买 nike

我想熟悉 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);
}
});

}


}

我知道我可以用标签上的按钮做类似的事情,但我只想到了两个按钮,所以在那个阶段它会花费更长的时间。

我的处理方式正确吗?你能纠正我在代码中犯的错误吗?

最佳答案

Whenever I run this i get an error

您的应用程序在此处抛出 NPE

gbc.insets = new Insets(35, 50, 0, 50);

因为您尚未初始化 GridBagConstraints gbc

此外,您看到两个面板并排的原因是,即使您创建了 CardLayout,您也忽略了将其用于您的 卡片面板。因此,您仍然使用 JPanel 的默认 FlowLayout。你可以这样做:

cardPanel = new JPanel(cl); 

关于Java-CardLayout 如何从正在切换的卡片中切换卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15438556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com