* @Description: 1.将“1.猜数游戏(按钮事件)”敲完。
* 1.1每赢一次计数,每输一次计数。
* 1.2在原有界面的基础上增加一个按键“保存”,将输赢次数保存一文件game.txt;
* 1.3.下次运行程序,将文件中保存的数显示在界面上:如下提示。
第一次启动,如下:
点击“开始游戏”
如果不输入,文本框背景变色
点击“猜”
在文本框重新输入要猜的数,点击猜。
继续游戏
点击“保存”
并闭应用
点击”确定“,退出。
再次启动APP
package student;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
* @ClassName MyUtil
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/14 0013-9:09
* @Version V1.0
**/
/**
* * @Description:
* * 1.将“1.猜数游戏(按钮事件)”敲完。
* * 1.1每赢一次计数,每输一次计数。
* * 1.2在原有界面的基础上增加一个按键“保存”,将输赢次数保存一文件game.txt;
* * 1.3.下次运行程序,将文件中保存的数显示在界面文本上:你上次赢了xx次输出了xx次
*/
public class GuiNumber extends JFrame {
JLabel lblShow;
JButton btnClick;
JButton btnSave;
JTextField txtInput;
int computer;
int correct;
int mistake;
String filePath = "game.txt";
public static void main(String[] args) {
new GuiNumber();
}
public GuiNumber() {
this.setTitle("按键事件学习");
Container cp = this.getContentPane();
//边框布局
cp.setLayout(new BorderLayout());
txtInput = new JTextField();
txtInput.setFont(new Font("黑体", Font.BOLD, 24));
txtInput.setPreferredSize(new Dimension(400, 50));
cp.add(BorderLayout.NORTH, txtInput);
//如果文件不存在则创建文件。
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取文件
lblShow = new JLabel();
lblShow.setFont(new Font("黑体", Font.BOLD, 24));
lblShow.setForeground(Color.RED);
if (file.length() <= 0) {
lblShow.setText("猜数游戏。");
} else {
lblShow.setText(Utility.reads(file));
}
cp.add(BorderLayout.CENTER, lblShow);
btnClick = new JButton("开始游戏");
cp.add(BorderLayout.SOUTH, btnClick);
btnSave = new JButton("保存");
cp.add(BorderLayout.EAST, btnSave);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Random r = new Random();
computer = r.nextInt(100) + 1;
System.out.println(computer);
btnClick.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnClick.setText("猜");
lblShow.setText("请输入你要猜的数");
if (txtInput.getText() == null || "".equals(txtInput.getText().trim())) {
txtInput.setBackground(Color.CYAN);
return;
}
int i = Integer.parseInt(txtInput.getText());
if (i > computer) {
lblShow.setText("你猜大了");
mistake++;
} else if (i < computer) {
lblShow.setText("你猜小了");
mistake++;
} else {
lblShow.setText("你猜对了,答案是" + computer);
btnClick.setText("开始游戏");
correct++;
computer = r.nextInt(100) + 1;
System.out.println(computer);
}
txtInput.setText("");
}
});
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringBuffer sb = new StringBuffer("<html><body><p align=\"center\">" + Utility.Date2String(new Date()));
sb.append("猜对了" + correct + "次,猜错了" + mistake + "次");
boolean flag = Utility.write(filePath, sb.toString());
JOptionPane.showMessageDialog(null, flag ? "保存成功" : "保存失败");
}
});
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(null, "确定要退出吗?", "提示", JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(-1);
}
}
});
}
}
final class Utility {
/**
* 将日期转为字符串
*
* @param date
* @return
*/
public static String Date2String(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EE");
return sdf.format(date);
}
//将内容保存到文件
public static boolean write(String writeSite, String str) {
File file = new File(writeSite);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
fos.write(str.getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
//读取文件内容
public static String reads(File fileName) {
FileReader fr = null;
BufferedReader br = null;
StringBuilder str = new StringBuilder();
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
str.append(line);
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return str.toString();
}
}