📜  java swing 交换面板 - Java (1)

📅  最后修改于: 2023-12-03 15:31:32.239000             🧑  作者: Mango

Java Swing 交换面板

在 Java Swing 中,可以通过交换面板来更改 GUI 的外观和行为。交换面板是一种常见的 GUI 设计模式,它允许在同一窗口中切换不同的面板。

实现方法

实现交换面板需要以下步骤:

  1. 创建一个 JFrame 对象,并设置其布局管理器为 CardLayout。

    JFrame frame = new JFrame();
    frame.setLayout(new CardLayout());
    
  2. 创建多个 JPanel 对象,并将它们添加到 JFrame 中。

    JPanel panel1 = new JPanel();
    frame.add(panel1, "Panel 1");
    
    JPanel panel2 = new JPanel();
    frame.add(panel2, "Panel 2");
    
    JPanel panel3 = new JPanel();
    frame.add(panel3, "Panel 3");
    
  3. 调用 CardLayout 的 show() 方法,以显示特定的面板。

    CardLayout cardLayout = (CardLayout)frame.getContentPane().getLayout();
    cardLayout.show(frame.getContentPane(), "Panel 1");
    
示例代码

下面是一个示例代码,它演示了如何实现交换面板的功能。

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

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

public class SwapPanelExample {

    private JFrame frame;

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

    public SwapPanelExample() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.RED);
        frame.add(panel1, "Panel 1");

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.GREEN);
        frame.add(panel2, "Panel 2");

        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.BLUE);
        frame.add(panel3, "Panel 3");

        JButton button1 = new JButton("Panel 1");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout)frame.getContentPane().getLayout();
                cardLayout.show(frame.getContentPane(), "Panel 1");
            }
        });
        frame.add(button1, "Button 1");

        JButton button2 = new JButton("Panel 2");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout)frame.getContentPane().getLayout();
                cardLayout.show(frame.getContentPane(), "Panel 2");
            }
        });
        frame.add(button2, "Button 2");

        JButton button3 = new JButton("Panel 3");
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout)frame.getContentPane().getLayout();
                cardLayout.show(frame.getContentPane(), "Panel 3");
            }
        });
        frame.add(button3, "Button 3");

        frame.pack();
        frame.setLocationRelativeTo(null);
    }

}
结论

通过交换面板,可以轻松地实现在同一窗口中显示不同的面板的功能。交换面板可以增强用户体验,并提高应用程序的可用性。