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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲2. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能3. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享4. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. UDDI FAQs8. Xml簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
