编写Java Applet程序,界面如下图所示。实现的功能为,当点击”Start”按钮时随机产生一个两位数,在文本框中不断显示,当点击”Stop”按钮时,停止显示并将当前数显示在JLabel中。要求使用线程实现。
package lx;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class javadome extends JApplet implements ActionListener, Runnable {
JTextField tf=new JTextField(10);
JButton b1 =new JButton("Start"),
b2 =new JButton("Stop");
JLabel l=new JLabel(" ");
Container cp;
Thread t;
String s="";
boolean run= true;
public void init() {
cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(tf);
cp.add(b1);
cp.add(b2);
cp.add(l);
b1.addActionListener(this);
b2.addActionListener(this);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(run) {
try {
int i=(int)(Math.random()*90+10);
tf.setText(""+i);
Thread.sleep(200);
}catch(InterruptedException e) {
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1) {
run=true;
t=new Thread(this);
t.start();
}else if(e.getSource()==b2) {
run=false;
l.setText(s=s+tf.getText()+" ");
}
}
}
---------------------
【转载】
作者:江南233244
原文:https://blog.csdn.net/qq_3913124 ... 635?utm_source=copy
|
|