www.pudn.com > GuestNum.rar > GuestNum.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
public class GuestNum extends JFrame implements ActionListener
{
double tempt;
JLabel jl1,jl2,jl3;
JTextField jtf;
JButton jb1,jb2;
JDialog jd;
Container con=getContentPane();
public GuestNum(String argu)
{
super(argu);
tempt=Math.floor(1000*Math.random());
jl1=new JLabel("Could you guest the 1 to 1000?");
jl2=new JLabel("Please enter a number:");
jl3=new JLabel();
jtf=new JTextField(8);
jb1=new JButton("Restart!");
jb2=new JButton("Cancel");
jd=new JDialog(this,"Error");
con.add(jl1);
con.add(jl2);
con.add(jtf);
con.add(jl3);
con.add(jb1);
con.add(jb2);
con.setLayout(new FlowLayout());
jtf.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String []args)
{
new GuestNum("Guest numbel");
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e)
{
double tempt;
try
{
tempt=Double.parseDouble(jtf.getText());
if(e.getSource()==jtf)
{
if(tempt>this.tempt)
{
con.setBackground(Color.red);
jl3.setText("Too high!");
jtf.setText("");
}
else if(tempt