简体   繁体   English

使用CardLayout时不会在Card之间转换的JButton

[英]JButton that won't transition between Cards while using CardLayout

So I'm new to programming and I've been making a program that uses multiple JPanels on a JFrame and other JPanels. 所以我是编程新手,我一直在制作一个在JFrame和其他JPanel上使用多个JPanel的程序。 I'm using CardLayout to go between different JPanels and I have it working on two different JButtons, but I can't get the last one to return to the main screen. 我正在使用CardLayout在不同的JPanel之间切换,并且我在两个不同的JButton上使用它,但是我无法获得最后一个返回主屏幕。

I've looked for answers, but it seems like most people just forget to use an ActionListener, something that I know I've done. 我一直在寻找答案,但似乎大多数人都忘记使用ActionListener了,我知道我已经做了。 Here's some of the involved classes in my code (There are a lot so I won't include them all, but I can provide any others that are needed). 这是我的代码中涉及的一些类(有很多类,因此我不会全部包括在内,但是我可以提供其他需要的类)。

Here's the JFrame class: 这是JFrame类:

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.*;

public class ElephantCare extends JFrame {
private static final long serialVersionUID = 1L;

private final String MAIN_STRING = "Main";
public JPanel container, main;
private Staff1Panel staff1 = new Staff1Panel();
private Staff2Panel staff2 = new Staff2Panel();
private StaffConfirmPanel staffConfirm = new StaffConfirmPanel();
private WelcomePanel welcome = new WelcomePanel();
private StaffPanel staff = new StaffPanel();
private GallonsPanel gallons = new GallonsPanel();
private ToysPanel toys= new ToysPanel();
private ActionPanel action = new ActionPanel();
public CardLayout card = new CardLayout();

public ElephantCare() {
    setSize(400,300);
    setTitle("Elephant Care");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(container);
    setVisible(true);
}

private void buildPanel() {
    main = new JPanel();;
    container = new JPanel(card);
    container.add(main, MAIN_STRING);
    container.add(staff1, "Staff 1");
    container.add(staff2, "Staff 2");

    main.setLayout(new BorderLayout());

    main.add(welcome, BorderLayout.NORTH);
    main.add(staff, BorderLayout.WEST);
    main.add(gallons, BorderLayout.CENTER);
    main.add(toys, BorderLayout.EAST);
    main.add(action, BorderLayout.SOUTH);

    staff.getStaff1Button().addActionListener(new Staff1Listener());
    staff.getStaff2Button().addActionListener(new Staff2Listener());
    staffConfirm.getConfirmButton().addActionListener(new ConfirmButtonListener());
}

private class Staff1Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        card.show(container, "Staff 1");
    }
}

private class Staff2Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        card.show(container, "Staff 2");
    }
}
private class ConfirmButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        card.show(container, MAIN_STRING);
    }
}

} }

Here's the JPanel with the Button: 这是带有按钮的JPanel:

import javax.swing.*;

public class StaffConfirmPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    public JButton confirm;

    public StaffConfirmPanel() {

        confirm = new JButton("OK");

        add(confirm);
    }

    public JButton getConfirmButton() {
        return confirm;
    }
}

And here's the JPanels where the button is used: 这是使用该按钮的JPanels:

 import java.awt.BorderLayout;

import javax.swing.*;

public class Staff1Panel extends JPanel{
    private static final long serialVersionUID = 1L;
    private Staff1NamePanel name = new Staff1NamePanel();
    private Staff1JobPanel job = new Staff1JobPanel();
    private StaffConfirmPanel confirm = new StaffConfirmPanel();

    public Staff1Panel() {
        setLayout(new BorderLayout());

        add(name, BorderLayout.WEST);
        add(job, BorderLayout.EAST);
        add(confirm, BorderLayout.SOUTH);
    }
}

And: 和:

import java.awt.BorderLayout;

import javax.swing.*;

public class Staff2Panel extends JPanel{
    private static final long serialVersionUID = 1L;
    private Staff2NamePanel name = new Staff2NamePanel();
    private Staff2JobPanel job = new Staff2JobPanel();
    private StaffConfirmPanel confirm = new StaffConfirmPanel();

    public Staff2Panel() {
        setLayout(new BorderLayout());

        add(name, BorderLayout.WEST);
        add(job, BorderLayout.EAST);
        add(confirm, BorderLayout.SOUTH);
    }
}

Thanks for any help! 谢谢你的帮助!

There are a lot of things happening in this code that are not quite right, and they contribute to your issue. 这段代码中发生的很多事情不太正确,它们助长了您的问题。 So lets address the biggest issue and then you will need to do the rest yourself. 因此,让我们解决最大的问题,然后您需要自己做剩下的事情。

First edit this code to have some debug text: 首先编辑此代码以包含一些调试文本:

private class ConfirmButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //NEW LINE OF CODE
        System.out.println("ConfirmButtonListener was triggered");
        card.show(container, MAIN_STRING);
    }
}

Now when you run your code you will notice that the message "ConfirmButtonListener was triggered" will never be printed to the console, meaning that the code never runs, so you can never return to the main screen. 现在,当您运行代码时,您将注意到消息“ ConfirmButtonListener被触发”将永远不会打印到控制台,这意味着该代码永远不会运行,因此您永远也不会返回主屏幕。

This happens because you create a StaffConfirmPanel named staffConfirm in your ElephantCare class. 这是因为你创建一个StaffConfirmPanel名为staffConfirmElephantCare类。 Then you add an action listener to staffConfirm . 然后,将一个动作侦听器添加到staffConfirm The problem is that you never use that staffconfirm panel anywhere because you create a new StaffConfirmPanel inside Staff1Panel and Staff2Panel so the action listener you have made will do nothing. 问题是,你永远不使用staffconfirm因为你创建一个新的面板任意位置StaffConfirmPanelStaff1PanelStaff2Panel所以你做什么也不做动作监听。

So the solution is to move the whole ConfirmButtonListener method and the staffConfirm.getConfirmButton().addActionListener line into the StaffConfirmPanel class like so: 因此,解决方案是将整个ConfirmButtonListener方法和staffConfirm.getConfirmButton().addActionListener行移动到StaffConfirmPanel类中,如下所示:

import javax.swing.*;

public class StaffConfirmPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    public JButton confirm;

    public StaffConfirmPanel() {

        confirm = new JButton("OK");

        add(confirm);

        //NEW LINE: SET THE ACTION LISTENER
        confirm.addActionListener(new ConfirmButtonListener());
    }

    public JButton getConfirmButton() {
        return confirm;
    }

    //NEW CODE, MOVE THE ACTION LISTENER METHOD HERE
    //ACTION LISTENER MOVED HERE
    private class ConfirmButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //NEW LINE OF CODE
            System.out.println("ConfirmButtonListener was triggered");
            card.show(ElephantCare.container, ElephantCare.MAIN_STRING);
        }
    }
}

Now it should work correctly and return to the main screen. 现在它应该可以正常工作并返回主屏幕。

EDIT: you will need to make MAIN_STRING a public variable in the ElephantCare class. 编辑:您将需要在ElephantCare类中使MAIN_STRING为公共变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM