如何实现Button ActionListener来让其他类运行cardLayout?

发布于 2025-01-08 15:11:46 字数 1757 浏览 1 评论 0原文

我不明白问题是什么?我尝试使用 JButton 切换两个单独的类扩展 JPanel 和 cardLayout,但我不知道我是否使用了正确的代码... 这是我的编码。

CardLayoutMenu

public class CardLayoutMenu extends JFrame implements ActionListener{

    CardLayout cardLayout = new CardLayout();

    private JPanel p1 = new JPanel(cardLayout);

    final String MAIN = "MAIN";
    final String OPTION = "OPTION";

    MainPanel mainPanel = new MainPanel();
    OptionPanel optionPanel = new OptionPanel();

    private Object object;

    public CardLayoutMenu(Object object) {          
        this.object = object;
    }

    public CardLayoutMenu(){

        setLayout(new BorderLayout());
        setTitle("Card Layout Menu");
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);            
        add(p1);            
        p1.add(mainPanel, MAIN);
        p1.add(optionPanel, OPTION);
    }

    public void actionPerformed(ActionEvent e){

        try{
            cardLayout.show(p1, OPTION);
        }catch(Exception ex){
            System.out.println("" + ex);
        }
    }
}

这是我的 MainPanel

public class MainPanel extends JPanel{

    private JButton jbtOption = new JButton("Option");

    public MainPanel() {            
        setLayout(new FlowLayout());
        add(jbtOption);         
        jbtOption.addActionListener(new CardLayoutMenu(this));
    }
}

然后是我的 OptionPanel,使用 JButton jbtBack 返回 MainPanel

public class OptionPanel extends JPanel{

    private JButton jbtBack = new JButton("Back");

    public OptionPanel() {          
        setLayout(new FlowLayout());
        add(jbtBack);           
    }
}

I don't what the problem is? I try to switch the two seperate classes extends JPanel with the cardLayout by using JButton and I don't know am I used the correct code...
Here is my coding.

CardLayoutMenu

public class CardLayoutMenu extends JFrame implements ActionListener{

    CardLayout cardLayout = new CardLayout();

    private JPanel p1 = new JPanel(cardLayout);

    final String MAIN = "MAIN";
    final String OPTION = "OPTION";

    MainPanel mainPanel = new MainPanel();
    OptionPanel optionPanel = new OptionPanel();

    private Object object;

    public CardLayoutMenu(Object object) {          
        this.object = object;
    }

    public CardLayoutMenu(){

        setLayout(new BorderLayout());
        setTitle("Card Layout Menu");
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);            
        add(p1);            
        p1.add(mainPanel, MAIN);
        p1.add(optionPanel, OPTION);
    }

    public void actionPerformed(ActionEvent e){

        try{
            cardLayout.show(p1, OPTION);
        }catch(Exception ex){
            System.out.println("" + ex);
        }
    }
}

Here is my MainPanel

public class MainPanel extends JPanel{

    private JButton jbtOption = new JButton("Option");

    public MainPanel() {            
        setLayout(new FlowLayout());
        add(jbtOption);         
        jbtOption.addActionListener(new CardLayoutMenu(this));
    }
}

Then my OptionPanel, use the JButton jbtBack to go back the MainPanel

public class OptionPanel extends JPanel{

    private JButton jbtBack = new JButton("Back");

    public OptionPanel() {          
        setLayout(new FlowLayout());
        add(jbtBack);           
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2025-01-15 15:11:46

这里的代码将导致无限递归:

public MainPanel() {
  setLayout(new FlowLayout());
  add(jbtOption);
  jbtOption.addActionListener(new CardLayoutMenu(this));
}

由于此构造函数最终是从 CardLayoutMenu 类调用的,因此您将拥有一个 CardLayoutMenu 对象,该对象创建一个 MainPanel 对象,该对象创建一个 CardLayoutMenu 对象,该对象创建一个 MainPanel 对象,该对象创建一个 CardLayoutMenu 对象,该对象创建一个MainPanel 对象创建了一个......好吧,我想你已经明白了。

我强烈建议您遵循的一项基本规则是,不要让您的 GUI 类实现 Listener 接口,因为它要求该类执行太多操作,并且常常会导致像您这样的代码混乱。这在小型示例程序中很好,但我希望它不被使用,因为它鼓励新手继续做这类事情。相反,请考虑创建一个 ActionListener 对象,并将此侦听器传递给任何需要告诉 CardLayout 更改视图的按钮的类。您可以通过构造函数或 setter 方法参数将此侦听器传递到这些类中。

This code here will cause an infinite recursion:

public MainPanel() {
  setLayout(new FlowLayout());
  add(jbtOption);
  jbtOption.addActionListener(new CardLayoutMenu(this));
}

Since this constructor is ultimately called from the CardLayoutMenu class, you'll have a CardLayoutMenu object that creates a MainPanel object which creates a CardLayoutMenu object that creates a MainPanel object which creates a CardLayoutMenu object that creates a MainPanel object which creates a ... well, I think that you get the picture.

One basic rule I strongly urge on you is to not make your GUI classes implement Listener interfaces as it is asking the class to do too much and often leads to confusing code such as yours. This is sort of fine in small example programs, but I wish that it wasn't used as it encourages newbies to continue to do this sort of thing. Instead consider creating an ActionListener object and pass this listener to any class that needs a button that needs to tell the CardLayout to change views. You can pass this listener into these classes via a constructor or setter method parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文