62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class CountInstances extends JFrame implements ActionListener{
private JLabel jla = new JLabel("中奖号码:");
private JLabel jlb = new JLabel();
private JButton jba = new JButton("开始");
private JButton jbb = new JButton("停止");
private static boolean flag = true;
public CountInstances(){
this.setLayout(null);
this.add(jla);this.add(jlb);
jla.setFont(new Font("Courier",Font.PLAIN,22));
jla.setHorizontalAlignment(JLabel.CENTER);
jla.setVerticalAlignment(JLabel.CENTER);
jla.setBounds(100,100,180,30);
jlb.setFont(new Font("Courier",Font.PLAIN,22));
jlb.setHorizontalAlignment(JLabel.CENTER);
jlb.setVerticalAlignment(JLabel.CENTER);
jlb.setBounds(160,100,180,30);
this.add(jba);this.add(jbb);
jba.setBounds(75,150,80,26);
jbb.setBounds(200,150,80,26);
jba.addActionListener(this);
jbb.addActionListener(this);
this.setTitle("抽奖程序");
this.setBounds(400,400,400,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jba){
flag = true;
new Thread(){
public void run(){
while(CountInstances.flag){
Random r = new Random();
int randomInt = r.nextInt(200);
jlb.setText(""+randomInt);
}
}
}.start();
}
else if(e.getSource()==jbb){
flag = false;
}
}
public static void main(String arguments []){
new CountInstances();
}
}
改了下 呵呵import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class CountInstances extends JFrame implements ActionListener{
private JLabel jla = new JLabel("中奖号码:");
private JButton jba = new JButton("开始");
private JButton jbb = new JButton("停止");
private static boolean flag = true;
public CountInstances(){
this.setLayout(null);
this.add(jla);
jla.setFont(new Font("Courier",Font.PLAIN,22));
jla.setHorizontalAlignment(JLabel.CENTER);
jla.setVerticalAlignment(JLabel.CENTER);
jla.setBounds(100,100,180,30);
this.add(jba);this.add(jbb);
jba.setBounds(75,150,80,26);
jbb.setBounds(200,150,80,26);
jba.addActionListener(this);
jbb.addActionListener(this);
this.setTitle("抽奖程序");
this.setBounds(400,400,400,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jba){
new Thread(){
public void run(){
while(CountInstances.flag){
Random r = new Random();
int randomInt = r.nextInt(200);
jla.setText("中奖号码:"+randomInt);
}
}
}.start();
}
else if(e.getSource()==jbb){
flag = false;
}
}
public static void main(String arguments []){
new CountInstances();
}
}
写的不好