?? ch11.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ch11 extends JFrame implements ActionListener,Runnable{
JPanel pnlMain;
JLabel lblThread1,lblThread2,lblThread3;
JButton btnControl1,btnControl2,btnControl3;
Thread thdDisplay1,thdDisplay2,thdDisplay3;
public ch11(){
super(" 接口Runnable類線程演示");
pnlMain=new JPanel(new GridLayout(2,3));
setContentPane(pnlMain);
lblThread1=new JLabel("掛起");
lblThread2=new JLabel("掛起");
lblThread3=new JLabel("掛起");
lblThread1.setForeground(Color.RED);
lblThread2.setForeground(Color.RED);
lblThread3.setForeground(Color.RED);
btnControl1=new JButton("掛起");
btnControl2=new JButton("掛起");
btnControl3=new JButton("掛起");
thdDisplay1=new Thread();
thdDisplay2=new Thread();
thdDisplay3=new Thread();
btnControl1.addActionListener(this);
btnControl2.addActionListener(this);
btnControl3.addActionListener(this);
pnlMain.add(lblThread1);
pnlMain.add(lblThread2);
pnlMain.add(lblThread3);
pnlMain.add(btnControl1);
pnlMain.add(btnControl2);
pnlMain.add(btnControl3);
thdDisplay1.start();
thdDisplay2.start();
thdDisplay3.start();
setSize(300,250);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
try{
if(ae.getSource()==btnControl1){
if(ae.getActionCommand()=="掛起"){
btnControl1.setText("重啟");
lblThread1.setText("重啟");
thdDisplay1.suspend();
}
if(ae.getActionCommand()=="重啟"){
btnControl1.setText("掛起");
lblThread1.setText("掛起");
thdDisplay1.resume();
}
}
if(ae.getSource()==btnControl2){
if(ae.getActionCommand()=="掛起"){
btnControl2.setText("重啟");
lblThread2.setText("重啟");
thdDisplay2.suspend();
}
if(ae.getActionCommand()=="重啟"){
btnControl2.setText("掛起");
lblThread2.setText("掛起");
thdDisplay2.resume();
}
}
if(ae.getSource()==btnControl3){
if(ae.getActionCommand()=="掛起"){
btnControl3.setText("重啟");
lblThread3.setText("重啟");
thdDisplay3.suspend();
}
if(ae.getActionCommand()=="重啟"){
btnControl3.setText("掛起");
lblThread3.setText("掛起");
thdDisplay3.resume();
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"線程錯誤!");
}
}
public static void main(String args[]){
ch11 t=new ch11();
}
public void run(){}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -