- packagetestswingguilock;
- importjavax.swing.*;
- importjava.awt.*;
- importjava.awt.event.*;
- publicclassTestSwingGUILock{
- TestSwingGUILock(){
- initGUI();
- }
- voidcostMoreTime(){
- doubled=0;
- //耗费大量时间的操作
- for(inti=0;i<Integer.MAX_VALUE;i++)
- d+=i;
- System.out.println("Ihavedosomanyworks!Theresultis"+d);
- }
- voidinitGUI(){
- JButtonjbutton=newJButton("Start");
- jbutton.addActionListener(newActionListener(){
- publicvoidactionPerformed(ActionEvente){
- System.out.println("Youhavepressedme!");
- //将耗时操作放置在新的线程内完成
- newThread(newRunnable(){
- publicvoidrun(){
- costMoreTime();
- }
- }).start();
- }
- });
- JFramejframe=newJFrame();
- Containercontainer=newContainer();
- container.setLayout(newBorderLayout());
- container.add(jbutton,BorderLayout.SOUTH);
- jframe.setContentPane(container);
- jframe.setSize(200,200);
- jframe.setVisible(true);
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- publicstaticvoidmain(String[]args){
- newTestSwingGUILock();
- }
- }
【编辑推荐】