• java Swing GUI 入门-切换布局


    java Swing GUI 入门-切换布局

    觉得有用的话,欢迎一起讨论相互学习~

    我的微博我的github我的B站

    布局

    1. 通过按钮切换Panel
    2. Panel的叠加
    • 布局,并向底部的ParentPanel中添加三个同等大小的panel相互叠加在一起.并且具有不同的颜色.

    添加事件

    • 向Button1中添加按钮事件以只显示panel1
            card1button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card1Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
                }
            });
        }
    
    • 向Button2中添加按钮事件以只显示panel2
    • 向Button2中添加按钮事件以只显示panel3
            card2Button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card2Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
    
                }
            });
            card3Button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card3Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
                }
            });
    

    效果演示

    完整代码

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class CardLayout {
        private JPanel mainPanel;
        private JPanel buttonPanel;
        private JButton card1button;
        private JButton card2Button;
        private JButton card3Button;
        private JPanel card1Panel;
        private JPanel card2Panel;
        private JPanel parentPanel;
        private JPanel card3Panel;
        private JCheckBox checkBox1;
        private JRadioButton radioButton1;
        private JButton button1;
    
        public CardLayout() {
            card1button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card1Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
                }
            });
            card2Button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card2Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
    
                }
            });
            card3Button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    parentPanel.removeAll();
                    parentPanel.add(card3Panel);
                    parentPanel.repaint();
                    parentPanel.revalidate();
                }
            });
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Card Layout");
            frame.setContentPane(new CardLayout().mainPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    
  • 相关阅读:
    [转] 百万级数据查询优化
    vs生成失败不报错
    C# 编译器错误对应代码详细信息
    使用XSL 样式表无法查看XML 输入。请更正错误然后单击刷新按钮
    vs中release模式调试和错误:CA0503:无法显示额外的代码分析警告或错误
    sp_executesql的用法
    一个for循环根据条件可以递增或递减
    Lc.exe已退出 代码为1
    将所有输出窗口文本重定向到即时窗口
    两步解决《内部服务器错误:500》
  • 原文地址:https://www.cnblogs.com/cloud-ken/p/14169983.html
Copyright © 2020-2023  润新知