实训内容:模仿“百词斩”手机App,设计并用Java语言实现一个“百词斩”图形界面程序(根据自己的能力,可以适当地增加或删除部分功能)。
最低要求:
(1)事先将一定数量的英语单词、四个选项(英语单词的汉语解释)及正确答案存放在一个文本文件中(一个单词的信息占一行,各项之间用一个空格分隔)。
(2)从文件中取出所有单词的信息,存入数组中。
(3)从单词数组中,随机找出一个单词,以单项选择题的形式显示出来,供用户答题。答对时显示√,答错时显示×并显示正确结果。每答完一题,都要统计并显示目前答对的单词数量。
(4)对于已经回答正确的单词,以后不会再出现。回答错误的单词,以后还会随机出现。
提示:
假如总共有n个单词,存入数组WORDS。
抽题时:
a. 随机产生一个[0,n)之间的随机整数i。
b. 取出WORDS[i]中的单词,作为选择题供用户选择。
答题时:
如果用户答对了,将WORDS[i]与WORDS[n-1]互换,n的值减1。
(5)所有单词都回答正确时n=0,程序结束。也可以关闭窗口强行退出。
import javax.swing.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class BczApp implements ActionListener{
private JFrame frame;
private ArrayList<String[]> wordslist;
private String str[];//存放单词
private String list[];
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JRadioButton button4;
private JButton pre; //显示上一题的按钮
private JButton next; //显示下一题的按钮
private int okNum=0;
private int totalNum=10;
private JLabel wordLabel;//显示题目
private JLabel answer;//显示答案
private JLabel labScore;//显示答对题数
private ImageIcon icon ;
private int index ;
private int num;
//初始化
public void init() {
Myframe();//创建窗体 必须放在最前面
Mylable();
LoadWords();
randomlist();
MyButton();
MyIcon();
display();//关闭和显示窗体,必须放在最后面
}
private ArrayList LoadWords() {
//导入数据
ArrayList<String[]> arrayList = new ArrayList<>();//初始化
try {
//设置存放单词的数组
str = new String[100];
String [] word1;
String [] word2;
String [] word3;
String [] word4;
String [] word5; String [] word6;
String [] word7; String [] word8;
FileReader fileReader = new FileReader(new File("C:\\Users\\splendid\\Desktop\\百词斩.txt"));
BufferedReader bufferedReader = new BufferedReader(fileReader);
for (int i = 0; i < 8; i++) {
str[i] = bufferedReader.readLine();//每次读取一行信息
//创建几组数组 分别存放单词 四个选项 正确答案
}
word1 = str[0].split(" ");
word2 = str[1].split(" ");
word3 = str[2].split(" ");
word4 = str[3].split(" ");
word5 = str[4].split(" ");
word6 = str[5].split(" ");
word7 = str[6].split(" ");
word8 = str[7].split(" ");
//将几个单词数组存放在集合中
arrayList.add(word1);
arrayList.add(word2);
arrayList.add(word3);
arrayList.add(word4);
arrayList.add(word5);
arrayList.add(word6);
arrayList.add(word7);
arrayList.add(word8);
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return arrayList;
}
private void MyIcon() {
icon =new ImageIcon("src\\bcz.jpg");
frame.setIconImage(icon.getImage());
}
private void loadsection(){
button1.setText(list[1]);
button2.setText(list[2]);
button3.setText(list[3]);
button4.setText(list[4]);
}
private void Mylable() {
//显示题目的标签
wordLabel =new JLabel();
wordLabel.setFont(new Font("宋体",Font.PLAIN,32));
wordLabel.setBounds(60, 60,300,30);
//显示答对单词数的标签
labScore=new JLabel( "答对单词数:"+okNum+"/"+totalNum);
labScore.setForeground(new Color(225, 54, 54));//设置答对单词数的文字颜色
labScore.setFont(new Font("黑体",Font.PLAIN,24)); //设置答对单词数的字体
labScore.setBounds(180, 20,200,30);
frame.add(labScore);
//显示答案的标签
answer=new JLabel();
answer.setBounds(70,350,300,50);
answer.setFont(new Font("黑体",Font.PLAIN,32));
answer.setForeground(Color.red);//设置答案的文字颜色
frame.add(answer);
}
private void randomlist(){
//实现随机抽取动态数组中的元素
Random r =new Random();
index =r.nextInt(8);//随机生成1—8之间的整数
num=index;
wordslist = new ArrayList<>();
wordslist=LoadWords();
list =wordslist.get(num);
}
public void MyButton() {
pre = new JButton("上一题");
pre.setFont(new Font("黑体", Font.PLAIN, 24));
pre.setBounds(100, 450, 120, 30);
frame.add(pre, BorderLayout.EAST);
next = new JButton("下一题");
next.setFont(new Font("黑体", Font.PLAIN, 24));
next.setBounds(250, 450, 120, 30);
frame.add(next, BorderLayout.WEST);
//设置对答单词的标签名
wordLabel.setText(list[0]);
frame.add(wordLabel);
ButtonGroup group = new ButtonGroup();//单选按钮组
button1 = new JRadioButton(list[1]);
button2 = new JRadioButton(list[2]);
button3 = new JRadioButton(list[3]);
button4 = new JRadioButton(list[4]);
//将每个选项存放在 ButtonGroup中
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
//给每个选项添加字体
button1.setFont(new Font("黑体", Font.BOLD, 35));
button2.setFont(new Font("黑体", Font.BOLD, 35));
button3.setFont(new Font("黑体", Font.BOLD, 35));
button4.setFont(new Font("黑体", Font.BOLD, 35));
//设置边界
button1.setBounds(60, 120, 220, 70);
button2.setBounds(60, 170, 220, 70);
button3.setBounds(60, 220, 220, 70);
button4.setBounds(60, 270, 220, 70);
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.CENTER);
frame.add(button3, BorderLayout.SOUTH);
frame.add(button4, BorderLayout.AFTER_LAST_LINE);
//添加监听事件
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
pre.addActionListener(this);
next.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(list[5])) {
okNum++;
labScore.setText("答对单词数:" + okNum + "/" + totalNum);
answer.setText("√ ");
}
else if (e.getActionCommand().equals("上一题")) {
if (num == 0) {
num = wordslist.size() - 1;
} else {
num--;
}
list = wordslist.get(num);
wordLabel.setText(list[0]);
answer.setText("");
loadsection();
}
else if (e.getActionCommand().equals("下一题")) {
//如果是最后一个单词
if(num ==wordslist.size()-1){
num=0;
}
else {
num++;
}list = wordslist.get(num);
wordLabel.setText(list[0]);
answer.setText("");
loadsection();
}
else {
answer.setText("x "+list[5]);
JOptionPane.showMessageDialog(null, "再试一次");
}
}
private void display() {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void Myframe() {
frame = new JFrame("百词斩");
frame.setBounds(400, 450, 500, 550);
frame.setResizable(false);
frame.setLayout(null);
}
public static void main(String[] args) {
BczApp bczApp= new BczApp();
bczApp.init();
}
}