import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import javax.swing.JFrame;
public class testGUI {
/**
* @param args
*/
public static void main(String[] args)
{
myGUI m=new myGUI();
}
}
class myGUI extends Frame implements ActionListener
{
Button btn0 = new Button("得到一个随机数");
Button btn1 = new Button("确定");
TextField tf = new TextField();
Panel p = new Panel();
Label l=new Label(" ");
int rand;
int a;
int n=0;
myGUI()
{
p.setLayout(new GridLayout(4,1,2,2));
p.add(btn0);
p.add(tf);
p.add(l);
l.setBackground(Color.CYAN);
p.add(btn1);
try{
a=Integer.parseInt(tf.getText());
}
catch(NumberFormatException e)
{}
add(p);
btn0.addActionListener(this);
btn1.addActionListener(this);
setVisible(true);
setBounds(400,200,300,300);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()=="得到一个随机数")
{
Random r = new Random();
rand=r.nextInt(10);
}
if (e.getActionCommand()=="确定")
{
if(rand!=a)
{
if(a>rand)
{
l.setText("偏大啦");
}
else
{
l.setText("偏小啦");
}
}
else
{
l.setText("猜对啦");
}
}
}
}
|
|