InvokeExample:用户界面线程和线程 : 界面线程 « 线程 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 线程 » 界面线程屏幕截图 
InvokeExample:用户界面线程和线程
InvokeExample:用户界面线程和线程

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// InvokeExample.java
//This class demonstrates several examples of how to handle long-running
//tasks (such as querying a remote resource). Some of the examples are
//good, some are not!
//

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class InvokeExample {
  private static JButton good = new JButton("Good");

  private static JButton bad = new JButton("Bad");

  private static JButton bad2 = new JButton("Bad2");

  private static JLabel resultLabel = new JLabel("Ready", JLabel.CENTER);

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Layout . . .
    JPanel p = new JPanel();
    p.setOpaque(true);
    p.setLayout(new FlowLayout());
    p.add(good);
    p.add(bad);
    p.add(bad2);

    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(p, BorderLayout.CENTER);
    c.add(resultLabel, BorderLayout.SOUTH);

    // Listeners
    good.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        resultLabel.setText("Working . . .");
        setEnabled(false);

        // We're going to do something that takes a long time, so we
        // spin off a thread and update the display when we're done.
        Thread worker = new Thread() {
          public void run() {
            // Something that takes a long time . . . in real life,
            // this
            // might be a DB query, remote method invocation, etc.
            try {
              Thread.sleep(5000);
            catch (InterruptedException ex) {
            }

            // Report the result using invokeLater().
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                resultLabel.setText("Ready");
                setEnabled(true);
              }
            });
          }
        };

        worker.start()// So we don't hold up the dispatch thread.
      }
    });

    bad.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        resultLabel.setText("Working . . .");
        setEnabled(false);

        // We're going to do the same thing, but not in a separate
        // thread.
        try {
          Thread.sleep(5000)// Dispatch thread is starving!
        catch (InterruptedException ex) {
        }

        // Report the result.
        resultLabel.setText("Ready");
        setEnabled(true);
      }
    });

    bad2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        resultLabel.setText("Working . . . ");
        setEnabled(false);

        // The wrong way to use invokeLater(). The runnable() shouldn't
        // starve the dispatch thread.
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            try {
              Thread.sleep(5000)// Dispatch thread is starving!
            catch (InterruptedException ex) {
            }

            resultLabel.setText("Ready");
            setEnabled(true);
          }
        });
      }
    });

    f.setSize(300100);
    f.setVisible(true);
  }

  // Allows us to turn the buttons on or off while we work.
  static void setEnabled(boolean b) {
    good.setEnabled(b);
    bad.setEnabled(b);
    bad2.setEnabled(b);
  }
}


           
       
Related examples in the same category
1. 事件调度线程事件调度线程
2. 图形界面时钟程序
3. 比赛条件下使用图形界面组件比赛条件下使用图形界面组件
4. 消除比赛条件下使用图形界面组件消除比赛条件下使用图形界面组件
5. 使用Runnable接口使用Runnable接口
6. Write your Beans this way so they can run in a multithreaded environmentWrite your Beans this way so they can run in a multithreaded environment
7. 用户界面响应
8. 计数器:用户界面线程和线程计数器:用户界面线程和线程
9. 螺纹精度:用户界面线程和线程螺纹精度:用户界面线程和线程
10. 用户界面线程和线程:援引等待用户界面线程和线程:援引等待
11. 用户界面线程用户界面线程
12. 用户界面线程用户界面线程
13. 用户界面线程和线程:滚动文字用户界面线程和线程:滚动文字
14. 动画:用户界面线程和线程动画:用户界面线程和线程
15. 用户界面线程和线程:耗时操作用户界面线程和线程:耗时操作
16. 用户界面线程和线程:取消耗时操作用户界面线程和线程:取消耗时操作
17. 用户界面线程和线程:重绘用户界面线程和线程:重绘
18. 用户界面线程线程测试
19. 线程和Swing 1线程和Swing 1
20. 线程和Swing 2线程和Swing 2
21. 线程和Swing 3线程和Swing 3
22. 用户界面线程测试10用户界面线程测试10
23. 用户界面线程和线程
24. SwingUtilities.invokeLater和线程
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.