首先,我没有收到任何错误。所以这可能是一个逻辑错误。
我试图切换到playMenu
面板但是当我按下按钮(在mainMenu
类中)时没有任何反应。是的,因为我使用简单的switchCard
println.
这是我的Frame类,它包含内容面板。
我删除了无关的变量。
//Panel Final Names
private final String MENU_ID = "menu";
private final String PLAY_ID = "play";
//Panels(Cards)
private MainMenu mainMenu;
private Play playMenu;
//JFrame
private JFrame frame = new JFrame("Battleships");
//Panel that will hold the card panel
private CardLayout cl = new CardLayout();
private JPanel contentPanel = new JPanel(cl);
//Constructors
public Frame(){}
public Frame(boolean x){
GUI();
}
public void GUI(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
/*
Passing Parameters to MainMenu class so
you can do operations like changing panels and
disposing the main JFrame from a different class
*/
mainMenu = new MainMenu(frame, contentPanel);
playMenu = new Play(frame, contentPanel);
//Adding menu panel as a card in contentPanel
contentPanel.add(mainMenu, MENU_ID);
contentPanel.add(playMenu, PLAY_ID);
//Adding contentPanel to JFrame
frame.add(contentPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Frame(true);
}
});
}
public void switchCard(){
contentPanel.setLayout(new CardLayout());
CardLayout cl = (CardLayout) contentPanel.getLayout();
cl.show(contentPanel, PLAY_ID);
}
这是我要切换到的JPanel。我删除了NetBeans生成的自动生成的代码。如果它以某种方式与此问题相关,我将发布它。
public class Play extends javax.swing.JPanel {
/**
* Creates new form Play
*/
private JFrame parentFrame;
private JPanel contentPanel;
public Play(JFrame frameIn, JPanel panelIn) {
initComponents();
contentPanel = panelIn;
parentFrame = frameIn;
}
}
答案 0 :(得分:4)
这不起作用:
public void switchCard(){
contentPanel.setLayout(new CardLayout());
CardLayout cl = (CardLayout) contentPanel.getLayout();
cl.show(contentPanel, PLAY_ID);
}
因为您正在更改原始布局,即包含所有数据的布局。相反,不要设置contentPane的布局,而是使用已存在的布局。
public void switchCard(){
// contentPanel.setLayout(new CardLayout());
CardLayout cl = (CardLayout) contentPanel.getLayout();
cl.show(contentPanel, PLAY_ID);
}
就此而言,您可以使用已有的cl
字段。例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BattleShipSwap {
// make id's public
public static final String MENU_ID = "menu";
public static final String PLAY_ID = "play";
private MainMenu mainMenu;
private Play playMenu;
private JFrame frame = new JFrame("Battleships");
private CardLayout cl = new CardLayout();
private JPanel contentPanel = new JPanel(cl);
public BattleShipSwap() {
initGui();
}
public void initGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
mainMenu = new MainMenu(this);
playMenu = new Play(this);
contentPanel.add(mainMenu, MENU_ID);
contentPanel.add(playMenu, PLAY_ID);
frame.add(contentPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BattleShipSwap();
}
});
}
public void switchCard(String id) {
// contentPanel.setLayout(new CardLayout());
// CardLayout cardlayout = (CardLayout) contentPanel.getLayout();
// why not just use the field?
cl.show(contentPanel, id);
}
}
class MainMenu extends JPanel {
private BattleShipSwap battleShipSwap;
public MainMenu(BattleShipSwap battleShipSwap) {
setBorder(BorderFactory.createTitledBorder("Main Menu"));
this.battleShipSwap = battleShipSwap;
add(new JButton(new SwapButtonAction(battleShipSwap, BattleShipSwap.PLAY_ID)));
}
}
class Play extends JPanel {
private BattleShipSwap battleShipSwap;
public Play(BattleShipSwap battleShipSwap) {
setBorder(BorderFactory.createTitledBorder("Play Menu"));
this.battleShipSwap = battleShipSwap;
add(new JButton(new SwapButtonAction(battleShipSwap, BattleShipSwap.MENU_ID)));
}
}
class SwapButtonAction extends AbstractAction {
private BattleShipSwap battleShipSwap;
private String id;
public SwapButtonAction(BattleShipSwap battleShipSwap, String id) {
super("Swap");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
this.battleShipSwap = battleShipSwap;
this.id = id;
}
@Override
public void actionPerformed(ActionEvent arg0) {
battleShipSwap.switchCard(id);
}
}
将来,您需要创建一个最小的可编译和可运行的示例程序,类似于上面的程序,它可以演示您的问题,MCVE