博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java卡片布局(CardLayout)
阅读量:2055 次
发布时间:2019-04-28

本文共 1546 字,大约阅读时间需要 5 分钟。

_(:з」∠)_在书本例子的基础上做了点改动

package t4;import java.awt.*;import javax.swing.*;import java.awt.event.*;//让包含事件源的容器对象来担任监听者public class App14_4 extends JFrame implements ActionListener {
static App14_4 frm = new App14_4(); static JPanel cardPan = new JPanel(); //使用卡片布局的容器 static JPanel btPan = new JPanel(); //使用网格布局容器 static CardLayout crd = new CardLayout(5,10); //定义卡片布局对象crd static JButton bt1 = new JButton("第一页"); static JButton bt2 = new JButton("上一页"); static JButton bt3 = new JButton("下一页"); static JButton bt4 = new JButton("最后页"); public static void main(String[] args) {
// TODO Auto-generated method stub bt1.addActionListener(frm); //向按钮们注册监听者frm bt2.addActionListener(frm); bt3.addActionListener(frm); bt4.addActionListener(frm); //设置窗口 frm.setLayout(null); frm.setTitle("卡片式布局策略CardLayout"); frm.setSize(350,300); frm.setResizable(false); //设置使用卡片布局的容器 cardPan.setLayout(crd); cardPan.setBounds(10, 10, 320, 200); for(int i=1;i<=4;i++) {
cardPan.add(new JTextArea("第"+i+"页")); } //设置使用网格布局的容器 btPan.setLayout(new GridLayout(1,4)); btPan.setBounds(10, 220, 320, 25); btPan.add(bt1); btPan.add(bt2); btPan.add(bt3); btPan.add(bt4); frm.add(cardPan); frm.add(btPan); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } public void actionPerformed(ActionEvent e) {
JButton bt = (JButton)e.getSource(); if(bt == bt1) crd.first(cardPan); else if(bt == bt2) crd.previous(cardPan); else if(bt == bt3) crd.next(cardPan); else crd.last(cardPan); }}

转载地址:http://snnlf.baihongyu.com/

你可能感兴趣的文章
[译] Kubernetes 儿童插图指南
查看>>
云原生周报第 2 期 | 2019-07-01~2019-07-05
查看>>
kubectl 创建 Pod 背后到底发生了什么?
查看>>
Kube-scheduler 源码分析(二):调度程序启动前逻辑
查看>>
kubernetes 1.15 有哪些让人眼前一亮的新特性?
查看>>
云原生周报:第 3 期
查看>>
深入理解 Linux Cgroup 系列(三):内存
查看>>
7月最新Java微服务资料
查看>>
Linux 指令
查看>>
wi10优化
查看>>
windows console 颜色设置
查看>>
VC unicode下Cstring转char*
查看>>
MFC ListBox使用
查看>>
Linux 使用grep筛选多个条件
查看>>
H264 NALU分析(sps,pps,关键帧,非关键帧)
查看>>
Windows文本加载wscite的使用
查看>>
浏览器主页被篡改修复
查看>>
FFmpeg - 新老接口对比问题
查看>>
Windows下MinGW编译ffmpeg库
查看>>
SDL在windows下使用 - 显示YUV
查看>>