如何在使用卡片布局时显示不同的JPanel

时间:2018-05-16 03:38:08

标签: java swing layout-manager cardlayout

我正在尝试制作一个基本的Swing项目,并试图找出在使用卡片布局时从面板内更换卡片的最佳方法。

我设法让它工作的唯一方法是将主窗口解析为每个面板。这被认为是一种正确的方法吗?或者有更好的方法吗?

这是我的意思的一个例子:

主窗口类:

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import java.awt.Color;

public class Window {

    private JFrame frame;
    private CardLayout cardLayout;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Window() {
        initialize();

            }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout());

        Orange orange = new Orange(this);
        orange.setBackground(Color.ORANGE);
        frame.getContentPane().add(orange, "orange");

        Green green = new Green(this);
        green.setBackground(Color.GREEN);
        frame.getContentPane().add(green, "green");

        //Shows orange by default
        cardLayout = (CardLayout) frame.getContentPane().getLayout();
        cardLayout.show(frame.getContentPane(), "orange");


    }

    //Changes the currently shown card to the "Green" Panel
    public void goGreen() {
        cardLayout.show(frame.getContentPane(), "green");
    }

    //Changes the currently shown card to the "Orange" Panel
    public void goOrange() {
        cardLayout.show(frame.getContentPane(), "orange");
    }   


}

橙色小组:

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Orange extends JPanel {

    private Window parent;

    public Orange(Window parent) {
        this.parent = parent;
        setBackground(Color.ORANGE);        

        //Adds a button to change to the "Orange" Panel
        JButton btnGoGreen = new JButton("Go Green Panel");
        btnGoGreen.setBounds(188, 132, 89, 23);
        btnGoGreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Function of the parent Window.class to change to the Orange Panel
                parent.goGreen();
            }
        });
        add(btnGoGreen);

    }

}

绿色小组:

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Green extends JPanel {

    private Window parent;

    public Green(Window parent) {
        this.parent = parent;
        setBackground(Color.GREEN);     

        //Adds a button to change to the "Green" Panel
        JButton btnGoOrange = new JButton("Go Orange Panel");
        btnGoOrange.setBounds(188, 132, 89, 23);
        btnGoOrange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Function of the parent Window.class to change to the Orange Panel
                parent.goOrange();
            }
        });
        add(btnGoOrange);

    }

}

0 个答案:

没有答案