我已经创建了这个简单的程序。我的问题是我想显示其他类的面板而不打开新的JFrame。但我无法创建任何解决方案。有人可以编写简单的代码来显示如何访问其他类的面板并替换当前面板吗?

package cardlayout;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Clayou {

    private JFrame jf = new JFrame("card layout");
    private JPanel jp = new JPanel();
    private JPanel jp1 = new JPanel();
    private JPanel jp2 = new JPanel();
    private JButton jb1 = new JButton("second");
    private JButton jb2 = new JButton("first");
    private CardLayout cl = new CardLayout();

    Clayou() {

        jp.setLayout(cl);
        jp1.add(jb1);
        jp2.add(jb2);

        jp1.setBackground(Color.black);
        jp2.setBackground(Color.white);

        jp.add(jp1, "1");
        jp.add(jp2, "2");

        cl.show(jp, "1");

        jb1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(jp, "2");

            }
        });
        jb2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(jp, "1");

            }
        });
        jf.add(jp);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);

    }

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

最佳答案

我刚刚回答了一个可能与此答案相同的问题。 Changing panels in gui我认为这也对您有帮助。您可以扩展卡类,以允许更多卡类型。在此示例中,我制作了两种卡类型。

进口货

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.Vector;


主班

public class Gui extends JFrame implements ListSelectionListener {

    Vector<String> menuList = new Vector<>();
    JList<String> menu = new JList<>(menuList);

    JPanel content = new JPanel(new CardLayout());

    Gui(){
        //put menu on left, content in middle
        add(menu, BorderLayout.WEST);
        add(content, BorderLayout.CENTER);
        menu.addListSelectionListener(this);

        //add multiple cards
        addCard(new SimpleLabelCard("First Item", "First Item Text"));
        addCard(new SimpleLabelCard("Second Item", "Second Item Text"));
        addCard(new SimpleTextAreaCard("Third Item", "Third Item Text"));


        //set content to first item
        ((CardLayout) content.getLayout()).show(content, "First Item");
    }

    private void addCard(Card c){
        menuList.add(c.name);
        content.add(c, c.name);
    }


    public static void main(String [] args){
        Gui gui = new Gui();
        gui.setSize(600, 500);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(e.getValueIsAdjusting()) return;

        //set card layout from JList menu
        ((CardLayout) content.getLayout()).show(content, menu.getSelectedValue());
    }
}


其他类,则将每个类放在各自的文件中。

//Card.java
//this is the basic card class the extends JPanel
//it contains the name so you can easily switch to it
public class Card extends JPanel{
    public final String name;

    public Card(String name){
        this.name = name;
    }
}

//SimpleLabelCard.java
//extends Card, so it is also a JPanel
public class SimpleLabelCard extends Card{

    private JLabel label = new JLabel();

    public SimpleLabelCard(String name, String text) {
        super(name);
        label.setText(text);
        add(label);
    }
}

//SimpleTextAreaCard.java
public class SimpleTextAreaCard extends Card{

    private JTextArea text = new JTextArea();

    public SimpleTextAreaCard(String name, String text) {
        super(name);
        this.text.setText(text);
        setLayout(new BorderLayout());
        add(this.text, BorderLayout.CENTER);
    }
}


现在,如果您要制作其他类型的卡,只需扩展卡类别。我希望这很简单。对我而言,这实际上是一次学习经历。

10-08 09:42