本帖最后由 施大勇 于 2013-9-28 15:30 编辑
- package day3;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.EventQueue;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Vector;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JScrollPane;
- public class SwingSingleThreadDemo extends JFrame {
- public static void main(String[] args) {
- try {
- EventQueue.invokeAndWait(new Runnable() {// 通过事件队列将窗体显示代码与事件处理代码封装在两个程序中。
- @Override
- public void run() {
- SwingSingleThreadDemo frame = new SwingSingleThreadDemo();
- }
- });
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private JRadioButton radioButton_1;
- private JRadioButton radioButton_2;
- private JRadioButton radioButton_3;
- private JRadioButton radioButton_4;
- private ButtonGroup buttonGroup = new ButtonGroup();
- private JPanel groupPane = new JPanel(new GridLayout(4, 1, 0, 0));
- private JList list;
- private JScrollPane scrollPane = new JScrollPane();
- private JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
- private JButton button;
- private JPanel panel = new JPanel(new GridLayout(1, 2, 0, 0));
- private Vector<String> listData=new Vector<String>();
- public SwingSingleThreadDemo() {
- super();
- setBounds(100, 100, 500, 400);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container container = getContentPane();
- container.setLayout(new GridLayout(2, 1, 10, 10));
- radioButton_1 = new JRadioButton("单选按钮一");
- radioButton_2 = new JRadioButton("单选按钮二");
- radioButton_3 = new JRadioButton("单选按钮三");
- radioButton_4 = new JRadioButton("单选按钮四");
- buttonGroup.add(radioButton_1);
- buttonGroup.add(radioButton_2);
- buttonGroup.add(radioButton_3);
- buttonGroup.add(radioButton_4);
- groupPane.add(radioButton_1);
- groupPane.add(radioButton_2);
- groupPane.add(radioButton_3);
- groupPane.add(radioButton_4);
- list = new JList();
- scrollPane.setViewportView(list);
- button = new JButton("添加列表组件");
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- do_button_actionPerformed_2(e);
- }
- });
- buttonPane.add(button);
- panel.add(scrollPane);
- panel.add(groupPane);
- container.add(panel);
- container.add(buttonPane);
- pack();
- setVisible(true);
- }
- //下面启动新线程的两种方式有什么不同????????
- protected void do_button_actionPerformed_1(ActionEvent e) {// 匿名线程启动方式。
- new Thread() { // 开辟一个新的线程执行费时业务
- public void run() {
- for (int i = 0; i < 10; i++) {
- listData.add("选项"+listData.size());
- list.setListData(listData);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }.start(); // 启动业务线程
- }
- protected void do_button_actionPerformed_2(ActionEvent e) {// 匿名线程启动方式。
- new Thread() {
- public void run() {
- for (int i = 0; i < 10; i++) {
- EventQueue.invokeLater(new Runnable(){
- public void run(){
- listData.add("列表选项" + "i");
- list.setListData(listData);
- }
- });
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
- }
复制代码 |
|