CardLayout中的按钮不起作用

时间:2016-05-01 20:07:23

标签: java swing cardlayout

所以我在我的一个程序中使用cardLayout而我正在努力使它在单击按钮时加载下一个面板。我有一个panelHolder类,其中持有cardlayout,每次按下面板上的按钮时,它会调用panelHolder类中的一个方法,该方法取决于按钮将某个布尔变量设置为true并调用repaint(其中面板是示出)。由于某种原因我的按钮不起作用,我似乎无法找出原因。有人能帮助我吗?

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Arrays;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;

public class SheetReader101 extends JFrame {
    public SheetReader101(){
        super("SheetReader101");
        setSize(2000,1000);
        setLocation(0,0);
        setResizable(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        PanelHolder pg2 = new PanelHolder();
        setContentPane(pg2);
        setVisible(true);
    }
    public static void main(String[]args){
        SheetReader101 z1 = new SheetReader101();
    }
}
class PanelHolder extends JPanel { // HERE
    CardLayout clayout = new CardLayout();
    PianoGameContent x;
    tutorial y;
    boolean [] paneldecide;
    PanelHolder() {
        super();
        y = new tutorial();
        x = new PianoGameContent();
        setLayout(clayout);
        this.add("Tutorial", y);
        this.add("FreePlay Mode", x);
        paneldecide = new boolean[15];
    }
        public static void main(String[]args){
            PanelHolder z1 = new PanelHolder();
            z1.run();
        }
          public void run(){
            layoutShower(0);
         }
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            }
        public void layoutShower (int decide){
            {
                PianoGameContent y2 = new PianoGameContent();
                PanelHolder.this.add("Piano", y2);
                System.out.println("intro slide run");
                if(decide == 1){
                    PanelHolder.this.add("Piano", y2);
                    System.out.println("testing11");
                    clayout.show(PanelHolder.this,"Piano");
            }
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

我“怀疑”核心问题与您发布的原始代码有关,您在子视图的PanelHolder中创建ActionListener的新实例,然后尝试切换视图,这个新实例与屏幕上的实例没有任何关系。

有几种方法可以管理CardLayout,我首选的方法是使用某种“导航”控制器来定义导航的工作方式,例如,您可以使用“下一个”和“上一个”或“返回”,或者您可以定义可以显示的实际视图,例如showMenuViewshowTutorialView等,具体取决于您希望为子视图提供多少控制。

以下是一个演示基本概念的简单示例,它使用enum来定义可用的视图(因为它比01更具意义......我不需要记住视图的实际名称,IDE可以为其提供自动更正;))

我在创建PanelHolder时创建并添加每个视图,我还为每个视图传递NavigationController的实例,以便他们可以与之交互

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JavaApplication1013 {

    public static void main(String[] args) {
        new JavaApplication1013();
    }

    public JavaApplication1013() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new PanelHolder());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public enum View {
        MENU,
        TUTORIAL,
        FREEPLAY;
    }

    public interface NavigationController {
        public void showView(View view);
    }

    public class PanelHolder extends JPanel implements NavigationController {

        private CardLayout cardLayout;

        public PanelHolder() {
            cardLayout = new CardLayout();
            setLayout(cardLayout);

            add(new MenuView(this), View.MENU.name());
            add(new TutorialView(this), View.TUTORIAL.name());
            add(new FreePlayView(this), View.FREEPLAY.name());
        }

        @Override
        public void showView(View view) {
            cardLayout.show(this, view.name());
        }

    }

    public abstract class ViewPane extends JPanel {
        private NavigationController controller;

        public ViewPane(NavigationController controller) {
            this.controller = controller;
        }

        public NavigationController getController() {
            return controller;
        }

        protected void showView(View view) {
            controller.showView(view);
        }

    }

    public class MenuView extends ViewPane {

        public MenuView(NavigationController controller) {
            super(controller);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JButton tut = new JButton("Tutorial");
            JButton freePlay = new JButton("Free Play");

            add(tut, gbc);
            add(freePlay, gbc);

            tut.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showView(View.TUTORIAL);
                }
            });
            freePlay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showView(View.FREEPLAY);
                }
            });
        }

    }

    public class TutorialView extends ViewPane {

        public TutorialView(NavigationController controller) {
            super(controller);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JButton menu = new JButton("Menu");

            add(new JLabel("Tutorial"), gbc);
            add(menu, gbc);

            menu.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showView(View.MENU);
                }
            });
        }

    }

    public class FreePlayView extends ViewPane {

        public FreePlayView(NavigationController controller) {
            super(controller);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JButton menu = new JButton("Menu");

            add(new JLabel("Free Play"), gbc);
            add(menu, gbc);

            menu.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showView(View.MENU);
                }
            });
        }

    }
}

仔细查看How to Use CardLayout了解更多详情