本文主要是介绍随机数生成器 [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();
}
});
}
}
这篇关于随机数生成器 [zhai]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!