手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Java Swing与线程的结合应用

(二)

[日期:2012-07-21] 来源:Linux社区  作者:gaowen_han [字体:大 中 小]

Java Swing与线程的结合应用(二):

  1. package com.han;  
  2.   
  3. import java.awt.Container;  
  4. import java.io.IOException;  
  5.   
  6. import javax.imageio.ImageIO;  
  7. import javax.swing.Icon;  
  8. import javax.swing.ImageIcon;  
  9. import javax.swing.JFrame;  
  10. import javax.swing.JLabel;  
  11.   
  12. public class ThreadAndSwing_2 extends JFrame {  
  13.   
  14.     /** 
  15.      *  
  16.      */  
  17.     private static final long serialVersionUID = -5452173345709313051L;  
  18.   
  19.     public ThreadAndSwing_2() {  
  20.         // TODO Auto-generated constructor stub   
  21.         try {  
  22.             final Icon imageIcon = new ImageIcon(ImageScale.scale(  
  23.                     ImageIO.read(this.getClass().getResource(  
  24.                             "/images/Luxun.jpg")), 100, 100, 1));  
  25.             final JLabel label = new JLabel(imageIcon);  
  26.             Container container = getContentPane();  
  27.             container.setLayout(null);  
  28.             container.add(label);  
  29.             label.setBounds(10, 10, imageIcon.getIconWidth(),  
  30.                     imageIcon.getIconHeight());  
  31.   
  32.             Thread thread = new Thread(new Runnable() {  
  33.   
  34.                 @Override  
  35.                 public void run() {  
  36.                     int count = 10;  
  37.                     // TODO Auto-generated method stub   
  38.                     while (true) {  
  39. //                      label.setLocation(count, 10);   
  40.                         label.setBounds(count, 10, imageIcon.getIconWidth(),  
  41.                                 imageIcon.getIconHeight());  
  42.                         try {  
  43.                             Thread.sleep(100);  
  44.                         } catch (InterruptedException e) {  
  45.                             // TODO Auto-generated catch block   
  46.                             e.printStackTrace();  
  47.                         }  
  48.                         count += 4;  
  49.   
  50.                         if (count > 200) {  
  51.                             count = 10;  
  52.                         }  
  53.                         // break;   
  54.                     }  
  55.                 }  
  56.   
  57.             });  
  58.             thread.start();  
  59.         } catch (IOException e) {  
  60.             // TODO Auto-generated catch block   
  61.             e.printStackTrace();  
  62.         }  
  63.   
  64.     }  
  65.   
  66.     /** 
  67.      * @param args 
  68.      */  
  69.     public static void main(String[] args) {  
  70.         // TODO Auto-generated method stub   
  71.         ThreadAndSwing_2 frame = new ThreadAndSwing_2();  
  72.         frame.setTitle("线程与swing JLabel结合应用");  
  73.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  74.         frame.setBounds(300, 200, 320, 160);  
  75.         frame.setVisible(true);  
  76.     }  
  77.   
  78. }  
linux
【内容导航】
第1页:(一) 第2页:(二)
第3页:(三)
相关资讯       Java  Swing  Java Swing 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款