线程-----银行拨号

 

package com;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TestThread extends JFrame {
 JPanel jPanel1 = new JPanel();
 JButton startButton = new JButton();
 JButton stopButton = new JButton();
 MyThread thread = null;
 public TestThread() {
  try {
   jbInit();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 private void jbInit() throws Exception {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  startButton.setText("start");
  startButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    startButton_actionPerformed(e);
   }
  });
  stopButton.setText("stop");
  stopButton.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(ActionEvent e) {
    stopButton_actionPerformed(e);
   }
  });
  this.getContentPane().add(jPanel1, BorderLayout.CENTER);
  jPanel1.add(startButton);
  startButton.setBounds(100, 200, 100, 100);
  jPanel1.add(stopButton);
  stopButton.setBounds(600,600, 700, 31);
 }
 
 void startButton_actionPerformed(ActionEvent e) {
  if (thread != null)
   thread.stop();
  thread = new MyThread();
  thread.start();
 }
 
 void stopButton_actionPerformed(ActionEvent e) {
  if (thread != null)
   thread.stop();
  thread = null;
 }
 
 public static void main(String[] args) {
  TestThread test = new TestThread();
  test.setBounds(500,500,500, 80);
  test.show();
 }
 
 private class MyThread extends Thread {
  public MyThread() {
  }
  
  public void run() {
   System.out.println("\n\n");
   while (true) {
    try {
     sleep(200);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    System.out.println("请01号来到1号窗口");
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值