Java: switching CardLayout between cards

I have a 'Frame' class that extends JFrameseparetad JPanels: MainMenuand SinglePanel I use CardLayout, but I am having trouble switching to the panel using the buttonSingleand buttons powrot. So my question is, how can I change / exchange between cards using these buttons?

My Frameclass:

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

my MainMenuclass:

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

my SinglePanelclass

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

Main class:

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

}
+4
source share
1 answer

CardLayout JFrame . , Frame JPanel , CardLayout Frame 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");

, Frame, Frame interface SwapInterface, swapView, . 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");
        }

    }
}

HovercraftFullOfEels , String- String-, . -

private static final String SINGLE_CARD = "single";

, "single", SINGLE_CARD

+6

Source: https://habr.com/ru/post/1533069/


All Articles