Java Swing CardLayout卡片布局的實(shí)現(xiàn)示例
1. 概述
官方JavaDocsApi: javax.swing.CardLayout
CardLayout,卡片布局管理器。它將容器中的每個(gè)組件看作一張卡片,一次只能看到一張卡片,容器則充當(dāng)卡片的堆棧,默認(rèn)顯示第一張卡片。
CardLayout 構(gòu)造方法:
// 創(chuàng)建一個(gè)間距大小為 0 的卡片布局 CardLayout() // 創(chuàng)建一個(gè)指定水平/垂直間距大小的卡片布局。 CardLayout(int hgap, int vgap)
CardLayout 常用方法:
// 顯示第一張卡片 void first(Container parent); // 顯示最后一張卡片 void last(Container parent); // 顯示下一張卡片(自動(dòng)循環(huán)顯示) void next(Container parent); // 顯示上一張卡片(自動(dòng)循環(huán)顯示) void previous(Container parent); // 顯示指定名稱的組件(添加組件到容器時(shí),可同時(shí)添加組件的名稱) void show(Container parent, String name);
2. 代碼實(shí)例
package com.xiets.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main { public static void main(String[] args) { JFrame jf = new JFrame("測試窗口"); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setSize(300, 200); // 創(chuàng)建卡片布局,卡片間水平和豎直間隔為 10 final CardLayout layout = new CardLayout(10, 10); // 創(chuàng)建內(nèi)容面板容器,指定布局管理器 final JPanel panel = new JPanel(layout); JButton btn01 = new JButton("Button01"); JButton btn02 = new JButton("Button02"); JButton btn03 = new JButton("Button03"); panel.add(btn01, "btn01"); panel.add(btn02, "btn02"); panel.add(btn03, "btn03"); // 先顯示第二個(gè) layout.show(panel, "btn02"); jf.setContentPane(panel); jf.setLocationRelativeTo(null); jf.setVisible(true); // 每間隔2秒切換顯示下一個(gè) new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { layout.next(panel); } }).start(); } }
結(jié)果展示:
實(shí)例一:卡片的切換
窗體默認(rèn)邊界布局,一個(gè)面板以卡片布局,面板上添加五個(gè)按鈕,該面板添加到CENTER位置,另一個(gè)面板添加兩個(gè)按鈕,兩個(gè)按鈕添加事件來切換顯示CENTER位置中的面板的組件
// cardlayout.Java import java.awt.*; import javax.swing.*; import java.awt.event.*;//引入事件包 //定義類時(shí)實(shí)現(xiàn)監(jiān)聽接口 public class cardlayout extendsJFrame implements ActionListener{ JButton nextbutton; JButton preButton; Panel cardPanel=new Panel(); Panel controlpaPanel=new Panel(); //定義卡片布局對(duì)象 CardLayout card=new CardLayout(); //定義構(gòu)造函數(shù) public cardlayout() { super("卡片布局管理器"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); //設(shè)置cardPanel面板對(duì)象為卡片布局 cardPanel.setLayout(card); //循環(huán),在cardPanel面板對(duì)象中添加五個(gè)按鈕 //因?yàn)閏ardPanel面板對(duì)象為卡片布局,因此只顯示最先添加的組件 for (int i = 0; i < 5; i++) { cardPanel.add(new JButton("按鈕"+i)); } //實(shí)例化按鈕對(duì)象 nextbutton=new JButton("下一張卡片"); preButton=new JButton("上一張卡片"); //為按鈕對(duì)象注冊監(jiān)聽器 nextbutton.addActionListener(this); preButton.addActionListener(this); controlpaPanel.add(preButton); controlpaPanel.add(nextbutton); //定義容器對(duì)象為當(dāng)前窗體容器對(duì)象 Container container=getContentPane(); //將 cardPanel面板放置在窗口邊界布局的中間,窗口默認(rèn)為邊界布局 container.add(cardPanel,BorderLayout.CENTER); // 將controlpaPanel面板放置在窗口邊界布局的南邊, container.add(controlpaPanel,BorderLayout.SOUTH); } //實(shí)現(xiàn)按鈕的監(jiān)聽觸發(fā)時(shí)的處理 public void actionPerformed(ActionEvent e){ //如果用戶單擊nextbutton,執(zhí)行的語句 if (e.getSource()==nextbutton){ //切換cardPanel面板中當(dāng)前組件之后的一個(gè)組件 //若當(dāng)前組件為最后添加的組件,則顯示第一個(gè)組件,即卡片組件顯示是循環(huán)的。 card.next(cardPanel); } if (e.getSource()==preButton){ //切換cardPanel面板中當(dāng)前組件之前的一個(gè)組件 //若當(dāng)前組件為第一個(gè)添加的組件,則顯示最后一個(gè)組件,即卡片組件顯示是循環(huán)的。 card.previous(cardPanel); } } public static void main(String[] args) { cardlayout kapian=new cardlayout(); } }
程序顯示結(jié)果如下圖所示,單擊“上一張”、“下一張”等按鈕可以上面的面板中顯示不同的按鈕來。
實(shí)例二:使用CardLayout類的show方法顯示組件。
窗體默認(rèn)邊界布局,一個(gè)面板以卡片布局,面板上添加4個(gè)按鈕,該面板添加到CENTER位置,另一個(gè)面板添加4個(gè)按鈕,這4個(gè)按鈕添加事件來切換顯示CENTER位置中的面板的組件按鈕。
// cardlayout1.java import java.awt.*; import javax.swing.*; import java.awt.event.*;//引入事件包 //定義類時(shí) 實(shí)現(xiàn)監(jiān)聽接口 public class cardlayout1 extends JFrameimplements ActionListener{ JButton b0,b1,b2,b3; Panel cardPanel=new Panel(); Panel controlpaPanel=newPanel(); //定義卡片布局對(duì)象 CardLayout card=newCardLayout(); //定義字符數(shù)組,為卡片命名 StringcardName[]={"0","1","2","3"}; //定義構(gòu)造函數(shù) public cardlayout1() { super("卡片布局管理器"); setSize(400,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); //設(shè)置cardPanel面板對(duì)象為卡片布局 cardPanel.setLayout(card); //循環(huán),在cardPanel面板對(duì)象中添加4個(gè)按鈕 //因?yàn)閏ardPanel面板對(duì)象為卡片布局,因此初始時(shí)顯示最先添加的組件 for (int i = 0; i< 4; i++) { //面板中添加的每個(gè)按鈕對(duì)應(yīng)設(shè)置一個(gè)卡片名 cardPanel.add(cardName[i],newJButton("按鈕"+i)); } //實(shí)例化按鈕對(duì)象 b0=newJButton("0"); b1=newJButton("1"); b2=newJButton("2"); b3=newJButton("3"); //為按鈕對(duì)象注冊監(jiān)聽器 b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); controlpaPanel.add(b0); controlpaPanel.add(b1); controlpaPanel.add(b2); controlpaPanel.add(b3); //定義容器對(duì)象為當(dāng)前窗體容器對(duì)象 Containercontainer=getContentPane(); //將 cardPanel面板放置在窗口邊界布局的中間,窗口默認(rèn)為邊界布局 container.add(cardPanel,BorderLayout.CENTER); // 將controlpaPanel面板放置在窗口邊界布局的南邊, container.add(controlpaPanel,BorderLayout.SOUTH); } //實(shí)現(xiàn)按鈕的監(jiān)聽觸發(fā)時(shí)的處理 public voidactionPerformed(ActionEvent e){ //用戶單擊b0按鈕時(shí)執(zhí)行的語句 if(e.getSource()==b0){ //通過show()方法中的卡片名稱,顯示容器中的組件。 card.show(cardPanel,cardName[0]); } if(e.getSource()==b1){ card.show(cardPanel,cardName[1]); } if(e.getSource()==b2){ card.show(cardPanel,cardName[2]); } if(e.getSource()==b3){ card.show(cardPanel,cardName[3]); } } public static voidmain(String[] args) { cardlayout1kapian=new cardlayout1(); } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java GUI編程之布局控制器(Layout)實(shí)例分析
- Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼
- JavaSwing BorderLayout 邊界布局的實(shí)現(xiàn)代碼
- Java Swing GroupLayout分組布局的實(shí)現(xiàn)代碼
- Java Swing BoxLayout箱式布局的實(shí)現(xiàn)代碼
- Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn)
- JavaSwing GridLayout 網(wǎng)格布局的實(shí)現(xiàn)代碼
- JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)
- java布局管理之CardLayout簡單實(shí)例
- Java Swing組件BoxLayout布局用法示例
- Java Swing組件布局管理器之FlowLayout(流式布局)入門教程
- java Swing布局管理之BoxLayout布局
- Java 最重要布局管理器GridBagLayout的使用方法
- JavaSwing基礎(chǔ)之Layout布局相關(guān)知識(shí)詳解
相關(guān)文章
Mybatis注解開發(fā)單表、多表操作的實(shí)現(xiàn)代碼
這篇文章主要介紹了Mybatis高級(jí):Mybatis注解開發(fā)單表操作,Mybatis注解開發(fā)多表操作,構(gòu)建sql語句,綜合案例學(xué)生管理系統(tǒng)使用接口注解方式優(yōu)化,需要的朋友可以參考下2021-02-02Java正則驗(yàn)證IP的方法實(shí)例分析【測試可用】
這篇文章主要介紹了Java正則驗(yàn)證IP的方法,結(jié)合實(shí)例形式對(duì)比分析了網(wǎng)上常見的幾種針對(duì)IP的正則驗(yàn)證方法,最終給出了一個(gè)比較靠譜的IP正則驗(yàn)證表達(dá)式,需要的朋友可以參考下2017-08-08詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存
本篇文章主要介紹了詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05SpringBoot整合Minio實(shí)現(xiàn)上傳文件的完整步驟記錄
MinIO是一個(gè)基于Apache License v2.0開源協(xié)議的對(duì)象存儲(chǔ)服務(wù),它兼容亞馬遜S3云存儲(chǔ)服務(wù)接口,非常適合于存儲(chǔ)大容量非結(jié)構(gòu)化的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Minio實(shí)現(xiàn)上傳文件的完整步驟,需要的朋友可以參考下2022-05-05SpringBoot+Redis防止惡意刷新與暴力請求接口的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用springboot和Redis來實(shí)現(xiàn)防止惡意刷新與暴力請求接口,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06SpringBoot使用DevTools實(shí)現(xiàn)后端熱部署的過程詳解
在Spring Boot項(xiàng)目中,Spring Boot官方提供你了Devtools熱部署模塊,通過maven的方式導(dǎo)入就能使用,本文主要SpringBoot通過DevTools實(shí)現(xiàn)熱部署,感興趣的朋友一起看看吧2023-11-11