黑马程序员技术交流社区

标题: 在Swing窗体中使用事件处理,多线程的问题 [打印本页]

作者: 施大勇    时间: 2013-9-18 17:29
标题: 在Swing窗体中使用事件处理,多线程的问题
本帖最后由 施大勇 于 2013-9-28 15:30 编辑
  1. package day3;
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.EventQueue;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.util.Vector;
  11. import javax.swing.ButtonGroup;
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JList;
  15. import javax.swing.JPanel;
  16. import javax.swing.JRadioButton;
  17. import javax.swing.JScrollPane;
  18. public class SwingSingleThreadDemo extends JFrame {
  19. public static void main(String[] args) {
  20.   try {
  21.    EventQueue.invokeAndWait(new Runnable() {// 通过事件队列将窗体显示代码与事件处理代码封装在两个程序中。
  22.       @Override
  23.       public void run() {
  24.        SwingSingleThreadDemo frame = new SwingSingleThreadDemo();
  25.       }
  26.      });
  27.   } catch (InterruptedException e) {
  28.    // TODO Auto-generated catch block
  29.    e.printStackTrace();
  30.   } catch (InvocationTargetException e) {
  31.    // TODO Auto-generated catch block
  32.    e.printStackTrace();
  33.   }
  34. }
  35. private JRadioButton radioButton_1;
  36. private JRadioButton radioButton_2;
  37. private JRadioButton radioButton_3;
  38. private JRadioButton radioButton_4;
  39. private ButtonGroup buttonGroup = new ButtonGroup();
  40. private JPanel groupPane = new JPanel(new GridLayout(4, 1, 0, 0));
  41. private JList list;
  42. private JScrollPane scrollPane = new JScrollPane();
  43. private JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
  44. private JButton button;
  45. private JPanel panel = new JPanel(new GridLayout(1, 2, 0, 0));
  46. private Vector<String> listData=new Vector<String>();
  47. public SwingSingleThreadDemo() {
  48.   super();
  49.   setBounds(100, 100, 500, 400);
  50.   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.   Container container = getContentPane();
  52.   container.setLayout(new GridLayout(2, 1, 10, 10));
  53.   radioButton_1 = new JRadioButton("单选按钮一");
  54.   radioButton_2 = new JRadioButton("单选按钮二");
  55.   radioButton_3 = new JRadioButton("单选按钮三");
  56.   radioButton_4 = new JRadioButton("单选按钮四");
  57.   buttonGroup.add(radioButton_1);
  58.   buttonGroup.add(radioButton_2);
  59.   buttonGroup.add(radioButton_3);
  60.   buttonGroup.add(radioButton_4);
  61.   groupPane.add(radioButton_1);
  62.   groupPane.add(radioButton_2);
  63.   groupPane.add(radioButton_3);
  64.   groupPane.add(radioButton_4);
  65.   list = new JList();
  66.   scrollPane.setViewportView(list);
  67.   button = new JButton("添加列表组件");
  68.   button.addActionListener(new ActionListener() {
  69.    @Override
  70.    public void actionPerformed(ActionEvent e) {
  71.     do_button_actionPerformed_2(e);
  72.    }
  73.   });
  74.   buttonPane.add(button);
  75.   panel.add(scrollPane);
  76.   panel.add(groupPane);
  77.   container.add(panel);
  78.   container.add(buttonPane);
  79.   pack();
  80.   setVisible(true);
  81. }
  82. //下面启动新线程的两种方式有什么不同????????
  83. protected void do_button_actionPerformed_1(ActionEvent e) {// 匿名线程启动方式。
  84.   new Thread() {           // 开辟一个新的线程执行费时业务
  85.    public void run() {
  86.     for (int i = 0; i < 10; i++) {
  87.      listData.add("选项"+listData.size());
  88.      list.setListData(listData);
  89.      try {
  90.       Thread.sleep(1000);
  91.      } catch (InterruptedException e) {
  92.       // TODO Auto-generated catch block
  93.       e.printStackTrace();
  94.      }
  95.     }
  96.    }
  97.   }.start();            // 启动业务线程
  98. }
  99. protected void do_button_actionPerformed_2(ActionEvent e) {// 匿名线程启动方式。
  100.   new Thread() {
  101.    public void run() {
  102.     for (int i = 0; i < 10; i++) {
  103.      EventQueue.invokeLater(new Runnable(){
  104.       public void run(){
  105.        listData.add("列表选项" + "i");
  106.        list.setListData(listData);
  107.       }
  108.      });
  109.      try {
  110.       Thread.sleep(500);
  111.      } catch (InterruptedException e) {
  112.       e.printStackTrace();
  113.      }
  114.     }
  115.    }
  116.   }.start();
  117. }
  118. }
复制代码

作者: 黑色海    时间: 2013-9-18 19:05
沙发,感觉很强大的样子




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2