CardLayout 中的按钮不起作用 [英] Button in CardLayout not working

查看:26
本文介绍了CardLayout 中的按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的一个程序中使用 cardLayout 并且我正在尝试使它这样当您单击按钮时下一个面板加载.我有一个 panelHolder 类,在该类中,cardlayout 被保留,每次按下面板上的按钮时,它都会调用 panelHolder 类中的一个方法,该方法根据按钮将某个布尔变量设置为 true 并调用 repaint(面板所在的位置)显示).由于某种原因,我的按钮不起作用,我似乎无法找出原因.有人可以帮我吗?

So I'm using cardLayout in one of my programs and I'm trying to make it so that when you click a button the next panel loads. I have a panelHolder class where the cardlayout is held and every time the button on the panel is pressed, it would call a method in the panelHolder class that depending on the button sets a certain boolean variable to true and calls repaint (where the panels are shown). For some reason my button isn't working and I can't seem to find out why. Can someone help me?

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

推荐答案

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

I "suspect" that the core problem has to do with the original code you posted, where you're making a new instance of PanelHolder in your child view's ActionListener and then are attempting to switch views, this new instance has no relationship to the instance that is on the screen.

有几种方法可以管理 CardLayout,我的首选方法是使用某种导航"控制器来定义导航的工作方式,例如,您可以有next"和之前"或后退",或者您可以定义可以显示的实际视图,即 showMenuViewshowTutorialView 等,具体取决于您想要给您的子控件多少控制视图.

There are a few ways you can manage CardLayout, my preferred way is to use some kind of "navigation" controller which defines how navigation works, for example, you could have "next" and "previous" or "back", or you could define the actual views that can be displayed, ie showMenuView, showTutorialView etc, depending on how much control you want to give your sub views.

下面是一个简单的例子,展示了基本思想,它使用一个enum来定义可用的视图(因为它比01... 我不需要记住视图的实际名称,IDE 可以为此提供自动更正;))

The following is a simple example which demonstrates the basic idea, it uses a enum to define the available views (as it has more meaning than 0, 1... and I don't need to remember the actual names of the views, the IDE can provide auto correct for that ;))

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

I create and add each view up front when I create the PanelHolder, I also pass each view an instance of the NavigationController, so they can interact with it

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

    }
}

仔细查看如何使用 CardLayout更多详情

这篇关于CardLayout 中的按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆