码迷,mamicode.com
首页 > 编程语言 > 详细

Java中CardLayout布局方式的应用

时间:2019-01-02 20:45:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:you   run   containe   demo   tar   ram   col   ntp   style   

 1 import java.awt.CardLayout;
 2 import java.awt.Color;
 3 import java.awt.Container;
 4 
 5 import javax.swing.JButton;
 6 import javax.swing.JFrame;
 7 
 8 public class CardLayoutDemo {
 9     public static void main(String[] args) {
10         //新建一个JFrame框架
11         JFrame frame = new JFrame("CardLayout");
12         frame.setSize(600, 400);
13         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
14         
15         final Container cp = frame.getContentPane();//得到一个内容面板;final标记的为常量,只能赋值一次
16         final CardLayout cardlayout = new CardLayout();//因为后期会对layout进行操作,才将其单独定义出来
17         cp.setLayout(cardlayout);//cp.setLayout(new CardLayout())的方式不可行,会出现异常
18         Color[] colors = { Color.white, Color.GRAY, Color.PINK, Color.cyan };
19 
20         for (int i = 0; i < colors.length; i++) {
21             String name = "card" + String.valueOf(i + 1);
22             JButton button = new JButton(name);
23             button.setBackground(colors[i]);
24             cp.add(name, button);
25         }
26 
27         frame.setVisible(true);
28         /**
29          * 要点是创建线程时,要重写Thread类中的run方法
30          */
31         Thread thread = new Thread() {
32             public void run() {
33                 while (true) {
34                     try {
35                         Thread.sleep(400);
36                     } catch (InterruptedException e) {
37                         e.printStackTrace();
38                     }
39                     /**
40                      * cardLayout的next是另外一个内容面板
41                      */
42                     cardlayout.next(cp);
43                 }
44             }
45         };
46         thread.start();//启动线程
47     }
48 }

 

Java中CardLayout布局方式的应用

标签:you   run   containe   demo   tar   ram   col   ntp   style   

原文地址:https://www.cnblogs.com/LJHAHA/p/10211082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!