📄 simplemaths.java
字号:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class SimpleMaths extends JFrame implements ActionListener{
private static final long serialVersionUID = 3617859655330969141L; //警告提示后加进去的,但不知道有什么用
private static int keys = 0; //记录临时参考答案
private static String[] question; //问题记录集
private static String[] answer; //答题的记录集
private static int[] key; //参考答案记录集
private static int numOfItem; //记录临时标题号
private static int rangeOfQuestion; //记录计算范围
private static int sumOfQuestion; //记录问题总数
private static int numOfQuestion; //记录临时题目号
private static double recordOfExam; //记录临时分数
private static String historyName; //记录历史记录的名字
private static int screenHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏高
private static int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获取屏高
AudioClip audioClip,exam,exercise,goodhistory,badhistory; //音频记录集
boolean examFocus = false,exerciseFocus = false,historyFocus = false; //启动音频判断
JDialog examMode,exerciseMode; //模式对话框
JLabel lblNum; //标题的标签
JLabel lblQuestion; //问题的标签
JLabel lblKey = new JLabel(""); //练习模式中的答案标签
JTextField Answer; //答案输入框
JMenu mFile,mSearch,mMusic,mHelp; //菜单标题集
JMenuItem mFile_NewTurn,mFile_Exit; //文件按钮集
JMenuItem mSearch_History; //历史记录按钮
JCheckBoxMenuItem mMusic_BackMusic; //背景音乐
JMenuItem mHelp_HelpTopics, mHelp_About; // 帮助按钮集
JButton NextQuestion,FrontQuestion,Complete,SearchKey; //模式内的按钮集
File HistoryList; //历史记录保存文件
public SimpleMaths(){ //构造函数
super("数学加减法"); //构造标题
JMenuBar MenuBar = new JMenuBar(); //创建标题栏及各标题
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭主线程
mFile = new JMenu("文件(F)", true);
mSearch = new JMenu("查看(S)", true);
mMusic = new JMenu("背景音乐(B)", true);
mHelp = new JMenu("帮助(H)", true);
mFile.setMnemonic('F'); //添加快捷键"alt"+相应的字母
mSearch.setMnemonic('S');
mMusic.setMnemonic('B');
mHelp.setMnemonic('H');
MenuBar.add(mFile); //向菜单栏中添加下拉按钮
MenuBar.add(mSearch);
MenuBar.add(mMusic);
MenuBar.add(mHelp);
mFile_NewTurn = new JMenuItem("重新开始(N)", 'N');
mFile_Exit = new JMenuItem("退出(X)", 'X');
mFile_NewTurn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));//快捷键
mFile_NewTurn.addActionListener(this); // 注册事件监听
mFile_Exit.addActionListener(this);
mFile.add(mFile_NewTurn); // 新一轮菜单项
mFile.add(mFile_Exit);
mSearch_History = new JMenuItem("历史记录(H)", 'H');
mSearch_History.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
mSearch_History.addActionListener(this);
mSearch.add(mSearch_History);
mMusic_BackMusic = new JCheckBoxMenuItem("音乐开关(M)");
mMusic_BackMusic.setMnemonic('W');
mMusic_BackMusic.setState(true);
mMusic_BackMusic.addActionListener(this);
mMusic.add(mMusic_BackMusic);
mHelp_HelpTopics = new JMenuItem("帮助(H)", 'H');
mHelp_About = new JMenuItem("关于(A)", 'A');
mHelp_HelpTopics.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
mHelp_HelpTopics.addActionListener(this);
mHelp_About.addActionListener(this);
mHelp.add(mHelp_HelpTopics);
mHelp.add(mHelp_About);
JLabel pic = new JLabel(new ImageIcon("icon/background.gif"));
pic.setPreferredSize(new Dimension(600,450));
this.setJMenuBar(MenuBar);
this.add(pic);
this.setSize(600, 500); //设置对话框大小
this.setResizable(false); //大小不可变
this.setLocation(screenWidth/2-300,screenHeight/2-250); //设置显示位置,屏中央
this.setVisible(true); //显示对话框
URL url = getClass().getResource("audio/theme.au"); //记录声音路径
audioClip = Applet.newAudioClip(url); //获取声音文件
url = getClass().getResource("audio/exam.au");
exam = Applet.newAudioClip(url);
url = getClass().getResource("audio/exercise.au");
exercise = Applet.newAudioClip(url);
url = getClass().getResource("audio/badhistory.mid");
badhistory = Applet.newAudioClip(url);
url = getClass().getResource("audio/goodhistory.au");
goodhistory = Applet.newAudioClip(url);
audioClip.loop(); //播放该文件
}
public static String grade(int num){//评分方法
String instrument;
int rightnum = 0;
double record;
for(int i = 0;i < num;i ++){
if(answer[i].equals(String.valueOf(key[i]))) rightnum ++;
}
record = (int)(rightnum * 10000 / sumOfQuestion) / 100;
if(record < 60){
instrument = "很遗憾,你没有及格!你的分数是:" + record;
}
else if(record < 70){
instrument = "加把劲,你能做得更好!你的分数是:" + record;
}
else if(record < 80){
instrument = "再加把劲,争取做到更棒!你的分数是:" + record;
}
else if(record < 90){
instrument = "做得好,别骄傲,你能做到最好!你的分数是:" + record;
}
else if(record < 100){
instrument = "非常好,成绩很优秀!你的分数是:" + record;
}
else instrument = "太棒了,你是最优秀的!你得到了:" + record;
recordOfExam = record;
return instrument;
}
public void checklist(){//确认是否保存记录
if(numOfQuestion == sumOfQuestion){
int exitChoice = JOptionPane.showConfirmDialog(this, "你确定完成了么?", "确认", JOptionPane.YES_NO_OPTION);
if(exitChoice == JOptionPane.YES_OPTION){
int saveChoice = JOptionPane.showConfirmDialog(this, grade(sumOfQuestion) + "分,是否保存?", "成绩", JOptionPane.YES_NO_OPTION);
examMode.dispose();
mSearch_History.setEnabled(true);
exam.stop();
examFocus = false;
if (saveChoice == JOptionPane.YES_OPTION) {
historyName = JOptionPane.showInputDialog("请输入你的名字:");
savehistory();
}
}
}
else{
int exitChoice = JOptionPane.showConfirmDialog(this, "你尚未完成,确定退出?", "确认", JOptionPane.YES_NO_OPTION);
if(exitChoice == JOptionPane.YES_OPTION){
int saveChoice = JOptionPane.showConfirmDialog(this, " 你尚未完成!\n" + grade(numOfQuestion) + "分,是否保存?", "成绩", JOptionPane.YES_NO_OPTION);
examMode.dispose();
mSearch_History.setEnabled(true);
exam.stop();
examFocus = false;
if (saveChoice == JOptionPane.YES_OPTION) {
historyName = JOptionPane.showInputDialog("请输入你的名字:");
savehistory();
}
}
}
}
public void savehistory(){//保存历史记录
String[][] historyRecord = new String[10][2];
String str;
int i = 0;
try{
FileReader fr = new FileReader("History.dat");
BufferedReader bfr = new BufferedReader(fr);
while ((str = bfr.readLine()) != null) { // 每次读取一行,直到文件结束
historyRecord[i][0] = str;
historyRecord[i][1] = bfr.readLine();
i ++;
}
fr.close();
}catch (IOException io){}
String sth = "";
if(i == 10){
for(int j = 0;j < i;j ++){
if(Double.parseDouble(historyRecord[j][1]) <= recordOfExam){
for(int k = i - 1;k > j; k --){
historyRecord[k][0] = historyRecord[k - 1][0];
historyRecord[k][1] = historyRecord[k - 1][1];
}
historyRecord[j][0] = historyName;
historyRecord[j][1] = recordOfExam + "";
break;
}
}
for(int x = 0;x < 10;x ++)
sth += historyRecord[x][0] + '\r' + historyRecord[x][1] + '\r';
}
else if(i > 0){
for(int j = 0;j < i;j ++){
if(Double.parseDouble(historyRecord[j][1]) <= recordOfExam){
for(int k = i;k > j; k --){
historyRecord[k][0] = historyRecord[k - 1][0];
historyRecord[k][1] = historyRecord[k - 1][1];
}
historyRecord[j][0] = historyName;
historyRecord[j][1] = recordOfExam + "";
break;
}
else if(Double.parseDouble(historyRecord[i - 1][1]) > recordOfExam){
historyRecord[i][0] = historyName;
historyRecord[i][1] = recordOfExam + "";
}
}
for(int x = 0;x < i + 1 && x < 10;x ++)
sth += historyRecord[x][0] + '\r' + historyRecord[x][1] + '\r';
}
else
sth = historyName + '\r' + recordOfExam + '\r';
try{
FileWriter fw = new FileWriter("History.dat");
fw.write(sth);
fw.close();
}catch (Exception e){}
if(recordOfExam >= 60&&mMusic_BackMusic.getState()) goodhistory.loop();
else if(mMusic_BackMusic.getState()) badhistory.loop();
showhistory();
}
public void showhistory(){ //显示历史记录
JPanel panel0,panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8,panel9,panel10;
String[][] historyRecord = new String[10][2];
String str;
int i = 0;
try{
FileReader fr = new FileReader("History.dat");
BufferedReader bfr = new BufferedReader(fr);
while ((str = bfr.readLine()) != null) { // 每次读取一行,直到文件结束
historyRecord[i][0] = str;
historyRecord[i][1] = bfr.readLine();
i ++;
}
fr.close();
}catch (IOException io){}
if(i == 0){
JOptionPane.showMessageDialog(this,"历史记录为空!", "空历史", JOptionPane.INFORMATION_MESSAGE);
}
else{ //创建历史记录显示对话框
historyFocus = true;
final JDialog historyList = new JDialog(this,"历史记录列表",false);
Container con = historyList.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
panel0 = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
panel7 = new JPanel();
panel8 = new JPanel();
panel9 = new JPanel();
panel10 = new JPanel();
Font historyFont = new Font("", Font.BOLD, 30);
Font listFont = new Font("", Font.BOLD, 20);
JLabel lblhistory = new JLabel(" 成绩排名 ");
JLabel lbl1 = new JLabel(" 姓名");
JLabel lbl2 = new JLabel(" 分数");
JLabel lblhistory1 = new JLabel("第一名 ");
JLabel lblhistory2 = new JLabel("第二名 ");
JLabel lblhistory3 = new JLabel("第三名 ");
JLabel lblhistory4 = new JLabel("第四名 ");
JLabel lblhistory5 = new JLabel("第五名 ");
JLabel lblhistory6 = new JLabel("第六名 ");
JLabel lblhistory7 = new JLabel("第七名 ");
JLabel lblhistory8 = new JLabel("第八名 ");
JLabel lblhistory9 = new JLabel("第九名 ");
JLabel lblhistory10 = new JLabel("第十名 ");
JLabel lblhistory1a = new JLabel(historyRecord[0][0]);
JLabel lblhistory2a = new JLabel(historyRecord[1][0]);
JLabel lblhistory3a = new JLabel(historyRecord[2][0]);
JLabel lblhistory4a = new JLabel(historyRecord[3][0]);
JLabel lblhistory5a = new JLabel(historyRecord[4][0]);
JLabel lblhistory6a = new JLabel(historyRecord[5][0]);
JLabel lblhistory7a = new JLabel(historyRecord[6][0]);
JLabel lblhistory8a = new JLabel(historyRecord[7][0]);
JLabel lblhistory9a = new JLabel(historyRecord[8][0]);
JLabel lblhistory10a = new JLabel(historyRecord[9][0]);
JLabel lblhistory1b = new JLabel(historyRecord[0][1]);
JLabel lblhistory2b = new JLabel(historyRecord[1][1]);
JLabel lblhistory3b = new JLabel(historyRecord[2][1]);
JLabel lblhistory4b = new JLabel(historyRecord[3][1]);
JLabel lblhistory5b = new JLabel(historyRecord[4][1]);
JLabel lblhistory6b = new JLabel(historyRecord[5][1]);
JLabel lblhistory7b = new JLabel(historyRecord[6][1]);
JLabel lblhistory8b = new JLabel(historyRecord[7][1]);
JLabel lblhistory9b = new JLabel(historyRecord[8][1]);
JLabel lblhistory10b = new JLabel(historyRecord[9][1]);
JButton Ok = new JButton("确定");
Ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
historyList.dispose();
historyFocus = false;
mSearch_History.setEnabled(true);
goodhistory.stop();
badhistory.stop();
if(mMusic_BackMusic.getState()) audioClip.loop();
}
});
historyList.addWindowListener(new WindowAdapter(){//关闭窗口时的音乐判断
public void windowClosing(WindowEvent evt){
mSearch_History.setEnabled(true);
historyFocus = false;
goodhistory.stop();
badhistory.stop();
if(mMusic_BackMusic.getState()) audioClip.loop();
}
});
lblhistory.setFont(historyFont);
lblhistory1.setFont(listFont);
lblhistory2.setFont(listFont);
lblhistory3.setFont(listFont);
lblhistory4.setFont(listFont);
lblhistory5.setFont(listFont);
lblhistory6.setFont(listFont);
lblhistory7.setFont(listFont);
lblhistory8.setFont(listFont);
lblhistory9.setFont(listFont);
lblhistory10.setFont(listFont);
lblhistory1a.setFont(historyFont);
lblhistory2a.setFont(historyFont);
lblhistory3a.setFont(historyFont);
lblhistory4a.setFont(historyFont);
lblhistory5a.setFont(historyFont);
lblhistory6a.setFont(historyFont);
lblhistory7a.setFont(historyFont);
lblhistory8a.setFont(historyFont);
lblhistory9a.setFont(historyFont);
lblhistory10a.setFont(historyFont);
lblhistory1b.setFont(historyFont);
lblhistory2b.setFont(historyFont);
lblhistory3b.setFont(historyFont);
lblhistory4b.setFont(historyFont);
lblhistory5b.setFont(historyFont);
lblhistory6b.setFont(historyFont);
lblhistory7b.setFont(historyFont);
lblhistory8b.setFont(historyFont);
lblhistory9b.setFont(historyFont);
lblhistory10b.setFont(historyFont);
lblhistory1a.setPreferredSize(new Dimension(140, 25));
lblhistory2a.setPreferredSize(new Dimension(140, 25));
lblhistory3a.setPreferredSize(new Dimension(140, 25));
lblhistory4a.setPreferredSize(new Dimension(140, 25));
lblhistory5a.setPreferredSize(new Dimension(140, 25));
lblhistory6a.setPreferredSize(new Dimension(140, 25));
lblhistory7a.setPreferredSize(new Dimension(140, 25));
lblhistory8a.setPreferredSize(new Dimension(140, 25));
lblhistory9a.setPreferredSize(new Dimension(140, 25));
lblhistory10a.setPreferredSize(new Dimension(140, 25));
Ok.setPreferredSize(new Dimension(300, 25));
panel0.add(lbl1);
panel0.add(lbl2);
panel1.add(lblhistory1);
panel2.add(lblhistory2);
panel3.add(lblhistory3);
panel4.add(lblhistory4);
panel5.add(lblhistory5);
panel6.add(lblhistory6);
panel7.add(lblhistory7);
panel8.add(lblhistory8);
panel9.add(lblhistory9);
panel10.add(lblhistory10);
panel1.add(lblhistory1a);
panel2.add(lblhistory2a);
panel3.add(lblhistory3a);
panel4.add(lblhistory4a);
panel5.add(lblhistory5a);
panel6.add(lblhistory6a);
panel7.add(lblhistory7a);
panel8.add(lblhistory8a);
panel9.add(lblhistory9a);
panel10.add(lblhistory10a);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -