您的位置首页百科知识

随机选号程序

随机选号程序

import java.awt.*;import java.awt.event.*;public class TestEvent extends Frame implements ActionListener, Runnable {String msg = "ok";Button ok = new Button(msg);boolean stop = true;int min = 0;int max = 0;public TestEvent(int min, int max) { super("随机选号"); this.min = min; this.max = max + 1; this.add(ok, BorderLayout.CENTER); ok.addActionListener(this); Toolkit tool = Toolkit.getDefaultToolkit(); this.setSize(tool.getScreenSize()); Font f = new Font(" ", Font.BOLD, 350); ok.setFont(f); this.addWindowListener(new MyWindowLis()); this.show(); new Thread(this).start();}private Color getRandomColor() { float r = (float) (Math.random() * 255); float y = (float) (Math.random() * 255); float b = (float) (Math.random() * 255); return Color.getHSBColor(r, y, b);}public void run() { while (true) { while (!stop) { int num = (int) (Math.random() * (max - min) + min); ok.setLabel("" + num); ok.setForeground(getRandomColor()); this.repaint(); try { Thread.sleep(10); } catch (InterruptedException ex) { } } }}public void actionPerformed(ActionEvent e) { stop = !stop;}class MyWindowLis extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); }}public static void main(String[] args) { TestEvent testevent = new TestEvent(1, 30);}}