随机数生成器 [zhai]

开发工具:JBuilder2005

package test;
import javax.swing.*;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author 我为J狂
 * @version 1.0
 */
public class Move extends Thread{
    JTextField jf;
    public Move(JTextField jf) {
        this.jf=jf;
    }
    boolean start=true;
   public void stopmove(){
        start=false;
    }
    public void startmove(){
       start=true;
   }
    synchronized void show(){
        int random=(int)(Math.random()*10);
         jf.setText(String.valueOf(random));
    }
    public void run(){
        while(start){
            show();
        }
    }
}
package test;

import java.awt.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RandomFrame extends JFrame {
    JPanel contentPane;
    JTextField jTextField1 = new JTextField();
    JButton jButton1 = new JButton();
    Move move;
    boolean sign=true;
    public RandomFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(400, 300));
        setTitle("产生随机数");
        jTextField1.setFont(new java.awt.Font("Dialog", Font.BOLD, 55));
        jTextField1.setHorizontalAlignment(SwingConstants.CENTER);
        jTextField1.setBounds(new Rectangle(113, 57, 153, 89));
        jButton1.setBounds(new Rectangle(142, 184, 99, 25));
        jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
        jButton1.setText("开始");
        jButton1.addActionListener(new RandomFrame_jButton1_actionAdapter(this));
        contentPane.add(jTextField1);
        contentPane.add(jButton1);
        move=new Move(jTextField1);
        move.start();
    }

    public void jButton1_actionPerformed(ActionEvent e) {
    if(sign)
     {   jButton1.setText("开始");
         move.stopmove();
         sign=false;
     }
     else {
          jButton1.setText("停止");
         sign=true;
         move=new Move(jTextField1);
         move.start();
     }
    }
}


class RandomFrame_jButton1_actionAdapter implements ActionListener {
    private RandomFrame adaptee;
    RandomFrame_jButton1_actionAdapter(RandomFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
package test;

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;

public class RandomApp {
    boolean packFrame = false;

    /**
     * Construct and show the application.
     */
    public RandomApp() {
        RandomFrame frame = new RandomFrame();
        // Validate frames that have preset sizes
        // Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
            frame.pack();
        } else {
            frame.validate();
        }

        // Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2,
                          (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }

    /**
     * Application entry point.
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.
                                             getSystemLookAndFeelClassName());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

                new RandomApp();
            }
        });
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值