import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class Test//主运行类
{
public static void main(String[] args)
{
new Guess();
}
}
class Guess extends JFrame //设置程序的界面
{
JTextField input;
JLabel mess;
JButton bStart;
JPanel pSouth;
public Guess()
{
super("Java猜数字游戏");
input=new JTextField(20);
input.setFocusable(false);
mess=new JLabel("请输入1-100之间您猜的数");
pSouth=new JPanel();
bStart=new JButton("开局");
add(input,BorderLayout.NORTH);
add(mess,BorderLayout.CENTER);
pSouth.add(bStart);
add(pSouth,BorderLayout.SOUTH);
MyListener ml=new MyListener(input,bStart,mess);
input.addActionListener(ml);
bStart.addActionListener(ml);
setBounds(300,300,400,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyListener implements ActionListener//文本输入框和"开局"按钮的监听器类
{
JTextField input;
JButton bStart;
JLabel mess;
static int ranNum;
static int count;
static int time=0;
boolean flag;
public MyListener(JTextField input,JButton bStart,JLabel mess)
{
this.input=input;
this.bStart=bStart;
this.mess=mess;
}
public void actionPerformed(ActionEvent e)//判断用户所猜的数字和系统随机生成的数字的大小
{ //根据判断情况给出相应的提示
if(e.getSource()==input)
{
String sTemp=input.getText();
int userNum=Integer.parseInt(sTemp);
if(userNum>ranNum)
{
mess.setText("您猜的数字偏大");
count++;
}
else if(userNum<ranNum)
{
mess.setText("您猜的数字偏小");
count++;
}
else if(userNum==ranNum)
{
count++;
flag=true;
mess.setText("恭喜,您猜对了,总共猜了"+count+"次");
try
{
if(time==1) //如果用户第一次运行该程序,则程序自动建立一个记录文件
{ //此时不要求用户输入自己的姓名,但会把猜测次数记录到文件指定位置
RandomAccessFile raf=new RandomAccessFile("d:/recordName.txt","rw");
raf.write("abcdefgh".getBytes()); //此处"abcdefgh"为占位字符,共八位
raf.write(count);
raf.close();
}
else //如果用户接着游戏,程序将用户猜对的次数与先前的记录文件中的
{ //次数相比较,如果小于记录,则要求用户输入自己的名字,程序将名字和次数记录
//到文件中。
RandomAccessFile rf=new RandomAccessFile("d:/recordName.txt","r");
rf.seek(0);
rf.skipBytes(8);
int i=rf.read();
if(count<i)
new RecordName("破记录了",count);
rf.close();
}
}
catch(IOException ee)
{
ee.printStackTrace();
}
bStart.setEnabled(true);
input.setFocusable(false);
}
input.setText("");
}
else if(e.getSource()==bStart)
{
input.setFocusable(true);
count=0;
time++;
flag=false;
mess.setText("请输入1-100之间您猜的数");
ranNum=(int)(Math.random()*100+1);
bStart.setEnabled(false);
}
}
}
class RecordName extends JFrame //提示用户输入姓名的用户窗口
{
JTextField inputName;
JPanel pCenter;
int count;
String name=null;
JButton bName;
public RecordName(String user_name,int count)
{
super(user_name);
this.count=count;
bName=new JButton("确定");
pCenter=new JPanel();
add(new JLabel("输入您的名字(英文字母):"),BorderLayout.NORTH);
inputName=new JTextField(20);
pCenter.add(inputName);
pCenter.add(bName);
add(pCenter,BorderLayout.CENTER);
bName.addActionListener(new nameRecord());
setLocation(400,400);
pack();
setVisible(true);
}
class nameRecord implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
name=inputName.getText();
try //判断用户输入的名字(英文字母)是否为8位,如果
{ //超过8位,则截取前8个字母,如果不够8为,则用空格符填补
if(name.length()>8)
name=name.substring(0,8);
else
{
while(name.length()<8)
name+="/u0000";
}
RandomAccessFile ra=new RandomAccessFile("d:/recordName.txt","rw");
ra.write(name.getBytes());
ra.write(count);
ra.close();
}
catch(IOException ee)
{
ee.printStackTrace();
}
}
}
}