Java图形用户界面程序设计作业
实验要求:英语完型填空测试编写一个文本文件“English.txt”,该文件的每一行字符串是符号“#”。第一个语言符号是一个练习填空的英语句子,第2个到第5个语言符号是供...
实验要求:英语完型填空测试
编写一个文本文件“English.txt”,该文件的每一行字符串是符号“#”。第一个语言符号是一个练习填空的英语句子,第2个到第5个语言符号是供选择的答案,第6个是正确的答案,如下所示:
#1.Take an umbrella with you in case #it will rain#it rains#it raining#it rained#it rains#
#2.He is no longer the honest man he has#who#whom#which#that#that#
… …
endend
要求每次读取文本文件的一行,然后使用字符串解析器把语言符号分解出来。第一个语言符号放在文本框中,其余四个语言符号作为选择框的名字,供选择答案用。最后一个语言符号是答案,根据选择的情况来给出分数。每当做完一个题目之后(即选择框发生事件之后),就判定分数。单击“下一题目”按钮,就再读取一行。当读到最后一行字母endend时,就关闭流,通知用户练习已完成。当单击“重新练习”按钮时,程序重新将流指向文件“English.txt”。
请大家给出详细的解答!或者发到邮箱w4222475@163.com
效果图如下: 展开
编写一个文本文件“English.txt”,该文件的每一行字符串是符号“#”。第一个语言符号是一个练习填空的英语句子,第2个到第5个语言符号是供选择的答案,第6个是正确的答案,如下所示:
#1.Take an umbrella with you in case #it will rain#it rains#it raining#it rained#it rains#
#2.He is no longer the honest man he has#who#whom#which#that#that#
… …
endend
要求每次读取文本文件的一行,然后使用字符串解析器把语言符号分解出来。第一个语言符号放在文本框中,其余四个语言符号作为选择框的名字,供选择答案用。最后一个语言符号是答案,根据选择的情况来给出分数。每当做完一个题目之后(即选择框发生事件之后),就判定分数。单击“下一题目”按钮,就再读取一行。当读到最后一行字母endend时,就关闭流,通知用户练习已完成。当单击“重新练习”按钮时,程序重新将流指向文件“English.txt”。
请大家给出详细的解答!或者发到邮箱w4222475@163.com
效果图如下: 展开
2个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private ButtonGroup buttonGroup = new ButtonGroup();
private JPanel pnlUp;
private JPanel pnlDown;
private JLabel lblSub;
private JLabel lblChoose;
private JRadioButton rbtn1;
private JRadioButton rbtn2;
private JRadioButton rbtn3;
private JRadioButton rbtn4;
private JButton btnRes;
private JButton btnNext;
private JTextArea txtArea;
private JScrollPane scrollPane;
private ArrayList<String> list;
private BufferedReader buf;
private String answer;
private int line;
private int scro;
public void init() {
getContentPane().setLayout(null);
setResizable(false);
setTitle("Test");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pnlUp = new JPanel();
pnlUp.setLayout(null);
pnlUp.setBounds(0, 0, 494, 21);
getContentPane().add(pnlUp);
lblSub = new JLabel();
lblSub.setText("Subject :");
lblSub.setBounds(80, 0, 86, 21);
pnlUp.add(lblSub);
pnlDown = new JPanel();
pnlDown.setLayout(null);
pnlDown.setBounds(0, 260, 494, 83);
getContentPane().add(pnlDown);
lblChoose = new JLabel();
lblChoose.setText("Choose :");
lblChoose.setBounds(26, 10, 56, 16);
pnlDown.add(lblChoose);
rbtn1 = new JRadioButton();
buttonGroup.add(rbtn1);
rbtn1.setText("");
rbtn1.setBounds(88, 6, 102, 24);
rbtn1.addActionListener(this);
pnlDown.add(rbtn1);
rbtn2 = new JRadioButton();
buttonGroup.add(rbtn2);
rbtn2.setText("");
rbtn2.setBounds(223, 6, 102, 24);
rbtn2.addActionListener(this);
pnlDown.add(rbtn2);
rbtn3 = new JRadioButton();
buttonGroup.add(rbtn3);
rbtn3.setText("");
rbtn3.setBounds(88, 36, 102, 24);
rbtn3.addActionListener(this);
pnlDown.add(rbtn3);
rbtn4 = new JRadioButton();
buttonGroup.add(rbtn4);
rbtn4.setText("");
rbtn4.setBounds(223, 36, 102, 24);
rbtn4.addActionListener(this);
pnlDown.add(rbtn4);
btnRes = new JButton();
btnRes.setText("Restart");
btnRes.setBounds(354, 10, 106, 26);
btnRes.addActionListener(this);
pnlDown.add(btnRes);
btnNext = new JButton();
btnNext.setText("Next");
btnNext.setBounds(354, 47, 106, 26);
btnNext.addActionListener(this);
pnlDown.add(btnNext);
scrollPane = new JScrollPane();
scrollPane.setBounds(0, 21, 494, 239);
getContentPane().add(scrollPane);
txtArea = new JTextArea();
txtArea.setEditable(false);
scrollPane.setViewportView(txtArea);
readTxt("test.txt");
setName(0);
setVisible(true);
}
public void readTxt(String path) {
String read = "";
list = new ArrayList<String>();
try {
buf = new BufferedReader(new FileReader(path));
while ((read = buf.readLine()) != null) {
if (read.equals("endend")) {
break;
}
list.add(read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setName(int line) {
String[] str = list.get(line).split("#");
txtArea.setText(str[1]);
rbtn1.setText(str[2]);
rbtn2.setText(str[3]);
rbtn3.setText(str[4]);
rbtn4.setText(str[5]);
answer = str[6];
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnNext) {
rbtn1.setEnabled(true);
rbtn2.setEnabled(true);
rbtn3.setEnabled(true);
rbtn4.setEnabled(true);
line = line + 1;
if (line >= list.size()) {
JOptionPane
.showMessageDialog(null, "End! Your Scro is " + scro);
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
} else {
setName(line);
rbtn1.setSelected(true);
}
}
if (e.getSource() == btnRes) {
rbtn1.setEnabled(true);
rbtn2.setEnabled(true);
rbtn3.setEnabled(true);
rbtn4.setEnabled(true);
rbtn1.setSelected(true);
scro = 0;
line = 0;
setName(line);
}
if (e.getSource() == rbtn1) {
if (rbtn1.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn2) {
if (rbtn2.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn3) {
if (rbtn3.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn4) {
if (rbtn4.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
}
public static void main(String args[]) {
Test test = new Test();
test.init();
}
}
//写得好累,要求追加分~~不懂百度HI我~
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private ButtonGroup buttonGroup = new ButtonGroup();
private JPanel pnlUp;
private JPanel pnlDown;
private JLabel lblSub;
private JLabel lblChoose;
private JRadioButton rbtn1;
private JRadioButton rbtn2;
private JRadioButton rbtn3;
private JRadioButton rbtn4;
private JButton btnRes;
private JButton btnNext;
private JTextArea txtArea;
private JScrollPane scrollPane;
private ArrayList<String> list;
private BufferedReader buf;
private String answer;
private int line;
private int scro;
public void init() {
getContentPane().setLayout(null);
setResizable(false);
setTitle("Test");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pnlUp = new JPanel();
pnlUp.setLayout(null);
pnlUp.setBounds(0, 0, 494, 21);
getContentPane().add(pnlUp);
lblSub = new JLabel();
lblSub.setText("Subject :");
lblSub.setBounds(80, 0, 86, 21);
pnlUp.add(lblSub);
pnlDown = new JPanel();
pnlDown.setLayout(null);
pnlDown.setBounds(0, 260, 494, 83);
getContentPane().add(pnlDown);
lblChoose = new JLabel();
lblChoose.setText("Choose :");
lblChoose.setBounds(26, 10, 56, 16);
pnlDown.add(lblChoose);
rbtn1 = new JRadioButton();
buttonGroup.add(rbtn1);
rbtn1.setText("");
rbtn1.setBounds(88, 6, 102, 24);
rbtn1.addActionListener(this);
pnlDown.add(rbtn1);
rbtn2 = new JRadioButton();
buttonGroup.add(rbtn2);
rbtn2.setText("");
rbtn2.setBounds(223, 6, 102, 24);
rbtn2.addActionListener(this);
pnlDown.add(rbtn2);
rbtn3 = new JRadioButton();
buttonGroup.add(rbtn3);
rbtn3.setText("");
rbtn3.setBounds(88, 36, 102, 24);
rbtn3.addActionListener(this);
pnlDown.add(rbtn3);
rbtn4 = new JRadioButton();
buttonGroup.add(rbtn4);
rbtn4.setText("");
rbtn4.setBounds(223, 36, 102, 24);
rbtn4.addActionListener(this);
pnlDown.add(rbtn4);
btnRes = new JButton();
btnRes.setText("Restart");
btnRes.setBounds(354, 10, 106, 26);
btnRes.addActionListener(this);
pnlDown.add(btnRes);
btnNext = new JButton();
btnNext.setText("Next");
btnNext.setBounds(354, 47, 106, 26);
btnNext.addActionListener(this);
pnlDown.add(btnNext);
scrollPane = new JScrollPane();
scrollPane.setBounds(0, 21, 494, 239);
getContentPane().add(scrollPane);
txtArea = new JTextArea();
txtArea.setEditable(false);
scrollPane.setViewportView(txtArea);
readTxt("test.txt");
setName(0);
setVisible(true);
}
public void readTxt(String path) {
String read = "";
list = new ArrayList<String>();
try {
buf = new BufferedReader(new FileReader(path));
while ((read = buf.readLine()) != null) {
if (read.equals("endend")) {
break;
}
list.add(read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setName(int line) {
String[] str = list.get(line).split("#");
txtArea.setText(str[1]);
rbtn1.setText(str[2]);
rbtn2.setText(str[3]);
rbtn3.setText(str[4]);
rbtn4.setText(str[5]);
answer = str[6];
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnNext) {
rbtn1.setEnabled(true);
rbtn2.setEnabled(true);
rbtn3.setEnabled(true);
rbtn4.setEnabled(true);
line = line + 1;
if (line >= list.size()) {
JOptionPane
.showMessageDialog(null, "End! Your Scro is " + scro);
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
} else {
setName(line);
rbtn1.setSelected(true);
}
}
if (e.getSource() == btnRes) {
rbtn1.setEnabled(true);
rbtn2.setEnabled(true);
rbtn3.setEnabled(true);
rbtn4.setEnabled(true);
rbtn1.setSelected(true);
scro = 0;
line = 0;
setName(line);
}
if (e.getSource() == rbtn1) {
if (rbtn1.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn2) {
if (rbtn2.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn3) {
if (rbtn3.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
if (e.getSource() == rbtn4) {
if (rbtn4.getText().equals(answer)) {
scro = scro + 10;
}
rbtn1.setEnabled(false);
rbtn2.setEnabled(false);
rbtn3.setEnabled(false);
rbtn4.setEnabled(false);
}
}
public static void main(String args[]) {
Test test = new Test();
test.init();
}
}
//写得好累,要求追加分~~不懂百度HI我~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询