📄 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<this.tempt)
{
con.setBackground(Color.blue);
jl3.setText("Too low!");
jtf.setText("");
}
else
{
jtf.setEnabled(false);
jl3.setText("Ok,you guest the num!!");
con.setBackground(Color.white);
}
}
}
catch(NumberFormatException nfe)
{
jd.setSize(100,100);
jd.setVisible(true);
}
if(e.getSource()==jb1)
{
this.tempt=Math.floor(1000*Math.random());
jtf.setEnabled(true);
}
if(e.getSource()==jb2)
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -