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

时间:2016-05-18 21:38:09

标签: java jbutton cardlayout

所以我是编程新手,而且我一直在制作一个在JFrame和其他JPanel上使用多个JPanel的程序。我使用CardLayout在不同的JPanel之间切换,我让它在两个不同的JButton上工作,但是我无法让最后一个返回到主屏幕。

我已经找到了答案,但似乎大多数人都忘了使用ActionListener,这是我知道我做过的事情。这是我的代码中涉及的一些类(有很多因此我不会将它们全部包含在内,但我可以提供其他所需的类。)

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

}

这是带有按钮的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;
    }
}

这里是使用按钮的JPanel:

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

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

此代码中发生的很多事情都不太正确,它们会导致您的问题。所以,让我们解决最大的问题,然后你需要自己完成剩下的工作。

首先编辑此代码以获得一些调试文本:

df['c'] = df.a.where(df.a==df.a, df.b)

现在,当您运行代码时,您会注意到消息" ConfirmButtonListener已被触发"将永远不会打印到控制台,这意味着代码永远不会运行,因此您永远不会返回主屏幕。

这是因为您在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); } } 课程中创建了名为StaffConfirmPanel的{​​{1}}。然后,您向staffConfirm添加一个动作侦听器。问题是您从未在任何地方使用ElephantCare面板,因为您在staffConfirmstaffconfirm内创建了新的StaffConfirmPanel,因此您制作的动作监听器将不执行任何操作。

因此解决方案是将整个Staff1Panel方法和Staff2Panel行移动到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); } } } 作为公共变量。