package p1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class randomDemo extends JFrame {
int number ;
Container c;
JButton b;
JLabel la;
String s;
public randomDemo()
{
this.setTitle("random number");
c = this.getContentPane();
c.setLayout(new BorderLayout());
Lis l = new Lis();
b = new JButton("产生一个0~100的随机数");
b.addActionListener(l);
b.addKeyListener(l);
la = new JLabel();
number = new Random().nextInt()%10;
c.add(la,BorderLayout.NORTH);
c.add(b,BorderLayout.SOUTH);
this.setBounds(400,200,250,150);
}
class Lis implements ActionListener,KeyListener {
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
check();
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
check();
}
} public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
// System.out.println(e.getKeyCode());
// System.out.println(e.VK_ENTER);
}
}
public void check(){
number = Math.abs(new Random().nextInt()%100);
s = new String("随机数是:"+number);
la.setText(s);
}
public static void main(String []args)
{
randomDemo r= new randomDemo();
r.setVisible(true);
}
}