Java:CardLayout在卡之间切换

TOM*_*-12 4 java swing cardlayout

我有类"框架"延伸JFrame和separetad JPanels:MainMenuSinglePanel 我使用的CardLayout,但我一直在使用切换回面板时有问题 buttonSinglepowrot按钮.所以我的问题是如何使用这些按钮更改/交换卡?

我的Frame班级:

    public class Frame extends JFrame{

    CardLayout cl = new CardLayout();
    final MainMenu menuPanel = new MainMenu();
    final SinglePanel singlePanel = new SinglePanel();

    public Frame(){

        setLayout(cl);
        add(menuPanel,"menu");
        add(singlePanel,"single");


        setSize(200, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
        setEnabled(true);
        swapView("menu");

    }

    public void swapView(String view){
        cl.show(getContentPane(),view);
    } 
}
Run Code Online (Sandbox Code Playgroud)

我的MainMenu班级:

public class MainMenu extends JPanel{


    public MainMenu(){


        setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
        add(Box.createVerticalGlue());

        JButton buttonSingle = new JButton("Single");     
        buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonSingle.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        add(buttonSingle);
        add(Box.createVerticalGlue());
        JButton buttonMulti = new JButton("Multiplayer"); 
        buttonMulti.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonMulti.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        });
        add(buttonMulti);
        add(Box.createVerticalGlue());
        JButton buttonExit = new JButton("Wyj?cie");  
        buttonExit.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonExit.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }});
            add(buttonExit);
            add(Box.createVerticalGlue());                        
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的SinglePanel班级

public class SinglePanel extends JPanel{

    SinglePanel(){
        setLayout(new FlowLayout());

        JButton powrot = new JButton("Wró? do menu");        
        powrot.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        add(powrot);
    }
}
Run Code Online (Sandbox Code Playgroud)

主要课程:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here      

        /*MainMenu mM = new MainMenu();*/

        Frame f = new Frame();
    }

}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 6

您需要引用面板类CardLayoutJFrame内部.你可以做的是通过FrameJPanel类作为参考,那么你可以使用CardLayoutFrame那些类shownext等喜欢的东西

public class MainMenu {
    private CardLayout layout;
    private Frame frame;

    public MainMenu(final Frame frame) {
         this.frame = frame;
         this.layout = (CardLayout)frame.getLayout();

         JButton buttonSingle = new JButton("Single");     
         buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
         buttonSingle.addActionListener(new ActionListener(){
             @Override
             public void actionPerformed(ActionEvent e) {
                 layout.show(frame, "single");
             }
         });
    }
}
Run Code Online (Sandbox Code Playgroud)

创建时MainPanel,需要传递Frame.this给它,引用当前的Frame

MainMenu menuPanel = new MainMenu(Frame.this);
Run Code Online (Sandbox Code Playgroud)

编辑

我刚注意到你的swapView方法.因此,而不是使用CardLayout直接在面板类,可以真正地调用swapViewactionPerformed

frame.swapView("single");
Run Code Online (Sandbox Code Playgroud)

或者更好的是,为了不暴露Frame类,你可以让Frame类实现一个有你需要覆盖的方法的interface说法.并传递给面板类.就像是SwapInterfaceswapViewSwapInterface

public interface SwapInterface {
    public void swapView(String view);
}

public Frame extends JFrame implements SwapInterface {
    MainMenu mainPanel = new MainMenu(Frame.this);
    ....
    @Override
    public void swapView(String view) {
        cl.show(getContentPane(), view);
    }
}

public class MainMenu extends JPanel {
    private SwapInterface swap;

    public MainMenu(SwapInterface swap) {
        this.swap = swap;
        ...
        public void actionPerfomed(ActionEvent e) {
            swap.swapView("single");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

边注

正如HovercraftFullOfEels在他的评论中指出的那样,你应该使用字符串包含String卡值,这样就没有错误了.就像是

private static final String SINGLE_CARD = "single";
Run Code Online (Sandbox Code Playgroud)

然后,在你使用的地方"single",使用SINGLE_CARD替代