Java基礎(chǔ)之CardLayout的使用
在編碼前需要將本案例中使用到的三張圖片(1.png 、2.png、3.png)保存到src所在的文件夾內(nèi)。看下圖:
1.png:
2.png:
3.png:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ShowCardLayout extends MouseAdapter{private JFrame f;private JPanel p1,p2,p3;private JLabel lb1,lb2,lb3;private CardLayout myCard;private Container c;public ShowCardLayout(){f=new JFrame('CardLayout示例');myCard=new CardLayout(5,10);p1=new JPanel();p2=new JPanel();p3=new JPanel();lb1=new JLabel(new ImageIcon('1.png'));lb2=new JLabel(new ImageIcon('2.png'));lb3=new JLabel(new ImageIcon('3.png'));}public void launchFrame(){c=f.getContentPane();c.setLayout(myCard);p1.add(lb1);p2.add(lb2);p3.add(lb3);p1.addMouseListener(this);p2.addMouseListener(this);p3.addMouseListener(this);c.add(p1,'First');c.add(p2,'Second');c.add(p3,'Third');myCard.show(c,'Third');f.pack();f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void mousePressed(MouseEvent e){myCard.next(c);}public static void main(String args[]) {ShowCardLayout sc=new ShowCardLayout();sc.launchFrame();}}
案例運(yùn)行圖:
依次點(diǎn)擊運(yùn)行窗口就會(huì)出現(xiàn)1.png 、2.png、3.png。
到此這篇關(guān)于Java基礎(chǔ)之CardLayout的使用的文章就介紹到這了,更多相關(guān)Java CardLayout的使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色2. Android中的緩存3. Vue實(shí)現(xiàn)div滾輪放大縮小4. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)5. APP啟動(dòng)慢怎么辦,Android官方這樣說6. android RecycleView實(shí)現(xiàn)多級(jí)樹形列表7. Android基于OpenCV實(shí)現(xiàn)圖像修復(fù)8. 解決Android Studio日志太長或滾動(dòng)太快問題9. VUE實(shí)時(shí)監(jiān)聽元素距離頂部高度的操作10. 如何從外部瀏覽開啟Android App
