Java:CardLayout在卡之间切换

时间:2014-03-23 15:51:38

标签: java swing cardlayout

我上过课#' Frame'它扩展了JFrame和separetad JPanelsMainMenuSinglePanel 我使用的是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);
    } 
}

我的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());                        
        }
    }

我的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);
    }
}

主要课程:

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();
    }

}

1 个答案:

答案 0 :(得分:6)

您需要引用面板类中CardLayout的{​​{1}}。您可以做的是将JFrame作为参考传递给Frame类,然后您可以将这些类中的JPanel的{​​{1}}用于CardLayoutFrame等等。

show

创建next时,您需要将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"); } }); } } 传递给它,引用当前的MainPanel

Frame.this

修改

我刚注意到你的Frame方法。因此,您可以直接在MainMenu menuPanel = new MainMenu(Frame.this);

中调用swapView,而不是直接在小组类中使用CardLayout
swapView

或者更好的是,为了不公开actionPerformed类,您可以让frame.swapView("single"); 类实现FrameFrame,其方法为interface你需要覆盖。并将SwapInterface传递给面板类。像

这样的东西
swapView

旁注

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

这样的东西
SwapInterface

然后,在您使用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"); } } } 的地方,请使用private static final String SINGLE_CARD = "single"; 代替