简体   繁体   English

在具有卡片布局的JPanel中的两个JPanel之间交换

[英]Swapping between two JPanels in a JPanel with card layout

I'm trying to get two jpanels to swap via buttons on those SPECIFIC panels, i don't want general buttons on both panels like normal card layout uses that repeat on both. 我试图通过这些特殊面板上的按钮来交换两个jpanel,但我不希望这两个面板上的常规按钮都像普通的卡布局那样在两个面板上重复使用。 I've tried to have a button called 'add' on main panel lead to the next page and a button on next page called 'back' lead to main panel. 我试图在主面板上将名为“添加”的按钮引导至下一页,在下一页上将名为“后退”的按钮引导至主面板。 However for some reason it isn't working. 但是由于某种原因,它无法正常工作。 I don't want two JPanels on the JFrame because I will be adding the JPanel to a tabbed pane later. 我不希望JFrame上有两个JPanels,因为稍后我会将JPanel添加到选项卡式窗格中。 Heres what I have already: 这是我已经拥有的:

Controller: 控制器:

import java.awt.CardLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Controller extends JPanel {

    private static Controller instance = new Controller();

    JPanel cards;
    Main mainPanel;
    NextPage nextPage;

    public Controller() {
        setLayout(new BorderLayout());
        setSize(810, 510);
        cards = new JPanel(new CardLayout());

        mainPanel = new Main();
        nextPage = new NextPage();
        cards.add(mainPanel, "Main");
        cards.add(nextPage, "Next");
        add(cards);
        setVisible(true);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MainPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Controller con = new Controller();
        frame.getContentPane().add(con);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


    public void changeCard(String card) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, card);
    }

    public static Controller getInstance() {
        return instance;
    }
}

Main: 主要:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Main extends JPanel implements ActionListener {

    private JButton search, add, delete;
    private JTextField textField;

    public Main() {

        search = new JButton("Search");
        add = new JButton("Add");
        delete = new JButton("Delete");
        textField = new JTextField(20);
        add.addActionListener(this);
        delete.addActionListener(this);
        setLayout(new BorderLayout());
        JPanel top = new JPanel();
        top.add(search);
        add(top, BorderLayout.NORTH);
        JPanel bottom = new JPanel();
        bottom.add(add);
        bottom.add(delete);
        add(bottom, BorderLayout.SOUTH);
        setVisible(true);
        setSize(400, 500);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add) {
            Controller.getInstance().changeCard("Next");
        } else if (e.getSource() == delete) {
           System.out.println("do something");
        }
    }

}

For next page: 对于下一页:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class NextPage extends JPanel implements ActionListener {

    private JButton back;
    private JTextField textField;

    public NextPage() {
        back = new JButton("Back");
        textField = new JTextField(20);
        back.addActionListener(this);
        setLayout(new BorderLayout());
        add(back);
        setVisible(true);
        setSize(400, 500);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == back) {
            Controller.getInstance().changeCard("Next");
        }

    }
}

Change your Controller like this: 像这样更改您的Controller

private static Controller instance;

...

private static void createAndShowGUI() {
    JFrame frame = new JFrame("MainPanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    instance = new Controller();
    frame.getContentPane().add(instance);
    frame.setSize(800, 600);
    frame.setVisible(true);
}

You actually created everything on an instance of Controller and showed it, but used another one to manage your events. 您实际上是在Controller实例上创建了所有内容并将其显示,但使用另一个实例来管理事件。 The multiple calls to the Controller constructor should have ringed a bell, because you only want one Controller . Controller构造函数的多次调用应该已经敲响了,因为您只需要一个Controller

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

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