Java GUI编程(10)---卡片布局CardLayout

CardLayout选项卡,一个窗口切换显示多页不同的内容。把容器中的每个组件看作一张卡片,默认显示第一张。

构造函数

CardLayout() 

CardLayout(int hgap, int vgap)

常用方法

first(Container parent);  //显示第一张

last(Container parent);   //显示最后一张

next(Container parent);   //显示下一张

previous(Container parent);  //显示上一张

show(Container parent, String name); //显示指定名称的一张

演示代码:通过点击窗口中的按钮切换不同的面板

1,新建一个窗口,加入两个面板(/北布局)

2, 北面板加入三个按钮

3, 南面板加入三个内容面板,每个面板设置一个标签组件,区别

演示code


package com.msh.util;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DemoCardLayout {
    
    public static void main(String[] args) {
        JFrame jf = new JFrame("演示卡片布局");
        jf.setSize(500, 400);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        CardLayout layoutCard = new CardLayout(10, 10);
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel(layoutCard); //面板2为卡片布局
       
        jf.add(jp1, "Center");//面板1的位置
        jf.add(jp2, "South");//设置面板2位置
        
        JButton button1 = new JButton("HR查询");
	JButton button2 = new JButton("环安查询");
	JButton button3 = new JButton("行政查询");
        jp1.add(button1);
	jp1.add(button2);
	jp1.add(button3);
        
        JPanel jp3 = new JPanel();
	JPanel jp4 = new JPanel();
	JPanel jp5 = new JPanel();
        
        //把三个面板加入到面板2中
	jp2.add(jp3, "0"); //加入面板3,第一张
	jp2.add(jp4, "1"); //加入面板4,第二张
	jp2.add(jp5, "2"); //加入面板5.第三张
        
        //建三个文本框
        JTextArea textArea1 = new JTextArea(10, 20); //5行10列的文本区域
        textArea1.setLineWrap(true); //设置自动换行,默认为 false
        textArea1.setForeground(Color.BLUE);
        textArea1.setText("HR查询");
        
        JTextArea textArea2 = new JTextArea(10, 20);
        textArea2.setLineWrap(true);
        textArea2.setForeground(Color.red);
        textArea2.setText("环安查询");
        
        JTextArea textArea3 = new JTextArea(10, 20); 
        textArea3.setLineWrap(true);
        textArea3.setForeground(Color.GREEN);
        textArea3.setText("行政查询");
        
        jp3.add(textArea1);
        jp4.add(textArea2);
        jp5.add(textArea3);
        
        //创建响应动作监听器类,重写ActionListener接口的actionPerformed()方法
        class bt_hander implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {   //show(Container parent, String name); 显示指定名称的一张
                if(e.getSource()==button1){layoutCard.show(jp2, "0");}; //显示第一张
                if(e.getSource()==button2){layoutCard.show(jp2, "1");}; //显示第二张
                if(e.getSource()==button3){layoutCard.show(jp2, "2");}; //显示第三张
            }
        }
        // 注册按钮监听事件
        button1.addActionListener(new bt_hander());
        button2.addActionListener(new bt_hander());
        button3.addActionListener(new bt_hander());
        
        jf.setVisible(true);  // 显示窗口
        
    }
    
}

运行代码

点击 按钮 环安查询

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值