[英]How to switch cards from a button on a card?
I've been searching for a solution for a while now and despite all the similar questions and answers, found nothing that seems to work. 我一直在寻找解决方案已有一段时间,尽管有所有类似的问题和答案,但似乎没有发现任何可行的方法。 I want a user to be able to progress through various panels set up in a card layout.
我希望用户能够逐步完成卡片布局中设置的各种面板。 However, I want the buttons used to switch between these cards to be on the cards themselves, not a separate set of buttons which doesn't change throught the program.
但是,我希望用于在这些卡之间进行切换的按钮位于卡本身上,而不是在程序中不会更改的单独按钮组。 Here is the main file where the frame is created:
这是创建框架的主文件:
public class BattleGraphs_V1
{
JPanel cards;
public int cardNumber = 1;
public void addComponentToPane (Container pane)
{
JPanel MainMenuCard = new MainMenu_V1();
JPanel DifficultySelectorCard = new DifficultySelector_V1();
cards = new JPanel(new CardLayout());
cards.add(MainMenuCard);
cards.add(DifficultySelectorCard);
pane.add(cards, BorderLayout.CENTER);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("BattleGraphs");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new java.awt.Dimension(725, 420));
frame.setResizable(false);
BattleGraphs_V1 containerPanel = new BattleGraphs_V1();
containerPanel.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
The two panels (more in the future) are in two seperate files in the project. 这两个面板(以后会有更多)位于项目的两个单独文件中。 I haven't added the code for them as apart from an action listener for the single button on each panel, it was all auto-generated code.
除了为每个面板上的单个按钮提供动作侦听器之外,我没有为他们添加代码,而是全部自动生成的代码。
EDIT: I added an auto-generated action listener to the button to the MainMenu JPanel like so: 编辑:我向MainMenu JPanel的按钮添加了一个自动生成的动作侦听器,如下所示:
private void MainMenuCardSBActionPerformed(java.awt.event.ActionEvent evt) {
BattleGraphs_V1 BG_V1 = new BattleGraphs_V1();
CardLayout cardLayout = (CardLayout) (BG_V1.cards.getLayout());
cardLayout.show(BG_V1.cards, "DifficultyCard");
}
After having added names to the cards added in the main file (main file being BattleGraphs_V1) like so: 在将名称添加到主文件(主文件为BattleGraphs_V1)中添加的卡中之后,如下所示:
cards.add(MainMenuCard, "MainMenuCard");
cards.add(DifficultySelectorCard, "DifficultyCard");
And when I ran the program and clicked the button got a null pointer exception error, as I have done with similar solutions. 当我运行程序并单击按钮时,出现空指针异常错误,就像我对类似解决方案所做的那样。 Would I be wrong in assuming this is more down to an issue of scope?
如果我认为这更多地涉及范围问题,我会错吗?
2nd EDIT: Right, hopefully this should help a little, I've added the enite project to a GitHub repository - https://github.com/charga600/BattleGraphs/tree/master 第二次编辑:对,希望这会有所帮助,我已将enite项目添加到GitHub存储库中-https: //github.com/charga600/BattleGraphs/tree/master
When you add your cards to your panel you should name them so you can select switch one you'd like to show on a certain event. 将卡片添加到面板中时,应命名它们,以便可以选择要在特定事件中显示的开关之一。
cards = new JPanel(new CardLayout());
cards.add(MainMenuCard, "first");
cards.add(DifficultySelectorCard, "second");
pane.add(cards, BorderLayout.CENTER);
GUI: 界面:
JButton button = new JButton("Click here");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "second");
}
});
EDIT 编辑
This is a short example to demonstrate CardLayout, since you didnt share your full code. 这是演示CardLayout的简短示例,因为您没有共享完整的代码。
Main class 主班
public class MainMenu_V1 extends JFrame {
private JMenuBar menuBar;
private JMenu file;
private JMenuItem exit;
private JPanel mainPanel;
public MainMenu_V1() {
setTitle("Main Panel");
setResizable(false);
setSize(new Dimension(400, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMainMenu());
setLocationRelativeTo(null);
mainPanel = new JPanel();
mainPanel.setLayout(new CardLayout());
mainPanel.add(new FirstPanel(mainPanel), "FIRST");
mainPanel.add(new SecondPanel(mainPanel), "SECOND");
setContentPane(mainPanel);
}
public JMenuBar createMainMenu() {
menuBar = new JMenuBar();
file = new JMenu("File");
exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(exit);
menuBar.add(file);
return menuBar;
}
public void switchPanel(Container container, String panelName) {
CardLayout card = (CardLayout) (container.getLayout());
card.show(container, panelName);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainMenu_V1().setVisible(true);
}
});
}
}
First Panel 第一小组
public class FirstPanel extends JPanel {
private JButton button;
private JPanel mainPanel;
public FirstPanel(JPanel mainPanel) {
this.mainPanel = mainPanel;
setPreferredSize(new Dimension(400, 200));
setBackground(Color.GRAY);
add(createButton());
}
private JButton createButton() {
button = new JButton("Switch to the second Panel");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainMenu_V1 main = new MainMenu_V1();
main.switchPanel(mainPanel, "SECOND");
}
});
return button;
}
}
Second Panel 第二小组
public class SecondPanel extends JPanel {
private JButton button;
private JPanel mainPanel;
public SecondPanel(JPanel mainPanel) {
this.mainPanel = mainPanel;
setPreferredSize(new Dimension(400, 200));
setBackground(Color.ORANGE);
add(createButton());
}
private JButton createButton() {
button = new JButton("Switch to the first Panel");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainMenu_V1 main = new MainMenu_V1();
main.switchPanel(mainPanel, "FIRST");
}
});
return button;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.