希望帮我实现下 那题目按钮的事件 例如当题目本来是第一题 当我按了第2题就令一个题目显示在文本域中 按 第3 又有一题 题目不能重复 当我按下 下一题 按钮时 文本域中出现下一道题目 如果开始是第1题 那么按下一题按钮出现第2题 再按 第3题 与题目按钮的文本域显示相同
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class StartExam extends JFrame implements ItemListener,ActionListener{
private JLabel prompt ,prompt1; //题目,和提示两个变量
private JTextArea title ;
private JTextField answer[];
private JRadioButton A , B ,C ,D ;
private JButton finish,restart,front,next;
private JButton t[];
public StartExam(){
super("驾驶理论模拟考试");
int a[];
a = this.getrandom(); //随机产生10个题目
title = new JTextArea(this.getTimu(a[0])); // 调用出题目
prompt = new JLabel("选择答案:");
prompt1 = new JLabel("您选的答案是:");
answer = new JTextField[10];
A = new JRadioButton("A");
B = new JRadioButton("B");
C = new JRadioButton("C");
D = new JRadioButton("D");
front = new JButton("上一题");
next = new JButton("下一题");
ButtonGroup bg = new ButtonGroup();
bg.add(A);
bg.add(B);
bg.add(C);
bg.add(D);
finish = new JButton("完成交卷");
restart = new JButton("重新开始");
JPanel south = new JPanel();
south.add(finish);
south.add(restart);
this.getContentPane().add(south,BorderLayout.SOUTH);
t = new JButton[10];
t[0] = new JButton("第1题");
t[1] = new JButton("第2题");
t[2] = new JButton("第3题");
t[3] = new JButton("第4题");
t[4] = new JButton("第5题");
t[5] = new JButton("第6题");
t[6]= new JButton("第7题");
t[7]= new JButton("第8题");
t[8] = new JButton("第9题");
t[9] = new JButton("第10题");
JPanel center = new JPanel();
center.add(title);
this.getContentPane().add(center,BorderLayout.CENTER);
JPanel north = new JPanel();
north.add(prompt);
north.add(A);
north.add(B);
north.add(C);
north.add(D);
north.add(front);
north.add(next);
this.getContentPane().add(north,BorderLayout.NORTH);
JPanel east = new JPanel(new GridLayout(10,2));
//依次添加10个按钮进画板
for(int i=0; i<10; i++) {
answer[i] = new JTextField(1);
}
east.add(t[0]);
east.add(answer[0]);
east.add(t[1]);
east.add(answer[1]);
east.add(t[2]);
east.add(answer[2]);
east.add(t[3]);
east.add(answer[3]);
east.add(t[4]);
east.add(answer[4]);
east.add(t[5]);
east.add(answer[5]);
east.add(t[6]);
east.add(answer[6]);
east.add(t[7]);
east.add(answer[7]);
east.add(t[8]);
east.add(answer[8]);
east.add(t[9]);
east.add(answer[9]);
this.getContentPane().add(east,BorderLayout.EAST);
//为每个按钮添加监听对象
A.addItemListener(this);
B.addItemListener(this);
C.addItemListener(this);
D.addItemListener(this);
t[0].addItemListener(this);
t[1].addItemListener(this);
t[2].addItemListener(this);
t[3].addItemListener(this);
t[4].addItemListener(this);
t[5].addItemListener(this);
t[6].addItemListener(this);
t[7].addItemListener(this);
t[8].addItemListener(this);
t[9].addItemListener(this);
finish.addItemListener(this);
restart.addItemListener(this);
front.addItemListener(this);
next.addItemListener(this);
}
public void actionPerformed(ActionEvent e) { //实现ItemListener接口的方法
if(e.getSource() == t[1]) {
title = new JTextArea(this.getTimu(1));
}
if(e.getSource() == t[2]) {
title = new JTextArea(this.getTimu(2));
title.getText();
}
if(e.getSource()== next){
}
}
public void itemStateChanged(ItemEvent e){ //实现itemSateChanged接口的方法
}
public static void main(String args[]) {
StartExam start = new StartExam();
start.setSize(500,300);
start.setResizable(true);
start.setVisible(true);
}
public int[] getrandom(){ //获得随机题目序列
int t = 0 ,count = 0,i = 0;
int s[] = new int[10];
//char b[] = new char[s.length];
s[0] = (int)(Math.random()*100) +1;
for(i=1; i<s.length; ) {
t = (int)(Math.random()*100)+1;
boolean a = true;
for(int j=0; j<i; j++){ //判断是否与前面产生的数字相同
if(t == s[j]){
a = false;
break;
}
}
if(a){
s[i] = t;
++i;
}
}
return s;
}
public String getTimu(int num) {//按随机产生数字 打出相应的题目
String t;
String thisLine;
String question = "";
BufferedReader in;
try {
in = new BufferedReader(new FileReader("驾驶理论考试.txt"));
while (true) {
thisLine = in.readLine();
if (thisLine == null) {
break;
} else if (thisLine.startsWith(num + "、")) { //选出以num、开头的行
thisLine = thisLine.substring(5,thisLine.length());
question = thisLine + "\n"; // 把题目加入到字符串question中
while (true) {
thisLine = in.readLine(); //读选择答案行
if (thisLine == null || thisLine.length() < 2) {
break;
}
question += thisLine + "\n"; //把选择答案加入到字符串中
}
}
}
}
catch (FileNotFoundException e) {}
catch (IOException e) {}
return question;
}
public String setText(int num) { //获取题目的字符串
String str;
str = this.getTimu(5);
return str ;
}
}
驾驶理论考试.TXT如下
1、 行车中遇有浓雾或特大雾天,能见度过低行车困难时,应_____。
A、开启前照灯行驶
B、开启示廓灯、雾灯行驶
C、选择安全地点停车
D、开启危险报警闪光灯行驶 2、 驾驶人驾驶机动车上道路行驶前,
应当对机动车的_____进行认真检查。
A、安全技术状况
B、整体结构
C、所有部件
D、齿轮油 3、 行车中发动机突然熄火后,应_____。
A、立即减速停车
B、关闭点火开关
C、将变速器操纵杆置于空挡行驶
D、开启右转向灯,将车缓慢滑行到路边停车检查 4、 公安机关交通管理部门对非本辖区机动车有违法行为记录的,
可以将记录违法行为的信息、证据转至_____公安机关交通管理部门。
A、机动车号牌核发地
B、驾驶证核发地
C、资格证核发地
D、身份证核发地 5、 驾驶人进入驾驶室前,首先应_____并确认安全。
A、观察车辆周围情况
B、不用观察周围情况
C、开启车门直接上车
D、注意观察天气情况
6、 驾驶人在观察后方无来车的情况下,未开转向灯就
变更车道也是合理的。
对 错 7、 在泥泞路上制动时,车轮易发生侧滑或甩尾,导致
交通事故。
对 错 8、 交通事故当事人逃逸造成证据丢失,逃逸当事人承
担事故全部责任。
对 错 9、 骨折伤员固定伤处力求稳妥牢固,要固定骨折的两
端和上下两个关节。
对 错 10、 倒车前,应仔细观察倒车路线,确认具备安全倒车
条件后方可进行倒车。
对 错
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class StartExam extends JFrame implements ItemListener,ActionListener{
private JLabel prompt ,prompt1; //题目,和提示两个变量
private JTextArea title ;
private JTextField answer[];
private JRadioButton A , B ,C ,D ;
private JButton finish,restart,front,next;
private JButton t[];
public StartExam(){
super("驾驶理论模拟考试");
int a[];
a = this.getrandom(); //随机产生10个题目
title = new JTextArea(this.getTimu(a[0])); // 调用出题目
prompt = new JLabel("选择答案:");
prompt1 = new JLabel("您选的答案是:");
answer = new JTextField[10];
A = new JRadioButton("A");
B = new JRadioButton("B");
C = new JRadioButton("C");
D = new JRadioButton("D");
front = new JButton("上一题");
next = new JButton("下一题");
ButtonGroup bg = new ButtonGroup();
bg.add(A);
bg.add(B);
bg.add(C);
bg.add(D);
finish = new JButton("完成交卷");
restart = new JButton("重新开始");
JPanel south = new JPanel();
south.add(finish);
south.add(restart);
this.getContentPane().add(south,BorderLayout.SOUTH);
t = new JButton[10];
t[0] = new JButton("第1题");
t[1] = new JButton("第2题");
t[2] = new JButton("第3题");
t[3] = new JButton("第4题");
t[4] = new JButton("第5题");
t[5] = new JButton("第6题");
t[6]= new JButton("第7题");
t[7]= new JButton("第8题");
t[8] = new JButton("第9题");
t[9] = new JButton("第10题");
JPanel center = new JPanel();
center.add(title);
this.getContentPane().add(center,BorderLayout.CENTER);
JPanel north = new JPanel();
north.add(prompt);
north.add(A);
north.add(B);
north.add(C);
north.add(D);
north.add(front);
north.add(next);
this.getContentPane().add(north,BorderLayout.NORTH);
JPanel east = new JPanel(new GridLayout(10,2));
//依次添加10个按钮进画板
for(int i=0; i<10; i++) {
answer[i] = new JTextField(1);
}
east.add(t[0]);
east.add(answer[0]);
east.add(t[1]);
east.add(answer[1]);
east.add(t[2]);
east.add(answer[2]);
east.add(t[3]);
east.add(answer[3]);
east.add(t[4]);
east.add(answer[4]);
east.add(t[5]);
east.add(answer[5]);
east.add(t[6]);
east.add(answer[6]);
east.add(t[7]);
east.add(answer[7]);
east.add(t[8]);
east.add(answer[8]);
east.add(t[9]);
east.add(answer[9]);
this.getContentPane().add(east,BorderLayout.EAST);
//为每个按钮添加监听对象
A.addItemListener(this);
B.addItemListener(this);
C.addItemListener(this);
D.addItemListener(this);
t[0].addItemListener(this);
t[1].addItemListener(this);
t[2].addItemListener(this);
t[3].addItemListener(this);
t[4].addItemListener(this);
t[5].addItemListener(this);
t[6].addItemListener(this);
t[7].addItemListener(this);
t[8].addItemListener(this);
t[9].addItemListener(this);
finish.addItemListener(this);
restart.addItemListener(this);
front.addItemListener(this);
next.addItemListener(this);
}
public void actionPerformed(ActionEvent e) { //实现ItemListener接口的方法
if(e.getSource() == t[1]) {
title = new JTextArea(this.getTimu(1));
}
if(e.getSource() == t[2]) {
title = new JTextArea(this.getTimu(2));
title.getText();
}
if(e.getSource()== next){
}
}
public void itemStateChanged(ItemEvent e){ //实现itemSateChanged接口的方法
}
public static void main(String args[]) {
StartExam start = new StartExam();
start.setSize(500,300);
start.setResizable(true);
start.setVisible(true);
}
public int[] getrandom(){ //获得随机题目序列
int t = 0 ,count = 0,i = 0;
int s[] = new int[10];
//char b[] = new char[s.length];
s[0] = (int)(Math.random()*100) +1;
for(i=1; i<s.length; ) {
t = (int)(Math.random()*100)+1;
boolean a = true;
for(int j=0; j<i; j++){ //判断是否与前面产生的数字相同
if(t == s[j]){
a = false;
break;
}
}
if(a){
s[i] = t;
++i;
}
}
return s;
}
public String getTimu(int num) {//按随机产生数字 打出相应的题目
String t;
String thisLine;
String question = "";
BufferedReader in;
try {
in = new BufferedReader(new FileReader("驾驶理论考试.txt"));
while (true) {
thisLine = in.readLine();
if (thisLine == null) {
break;
} else if (thisLine.startsWith(num + "、")) { //选出以num、开头的行
thisLine = thisLine.substring(5,thisLine.length());
question = thisLine + "\n"; // 把题目加入到字符串question中
while (true) {
thisLine = in.readLine(); //读选择答案行
if (thisLine == null || thisLine.length() < 2) {
break;
}
question += thisLine + "\n"; //把选择答案加入到字符串中
}
}
}
}
catch (FileNotFoundException e) {}
catch (IOException e) {}
return question;
}
public String setText(int num) { //获取题目的字符串
String str;
str = this.getTimu(5);
return str ;
}
}
驾驶理论考试.TXT如下
1、 行车中遇有浓雾或特大雾天,能见度过低行车困难时,应_____。
A、开启前照灯行驶
B、开启示廓灯、雾灯行驶
C、选择安全地点停车
D、开启危险报警闪光灯行驶 2、 驾驶人驾驶机动车上道路行驶前,
应当对机动车的_____进行认真检查。
A、安全技术状况
B、整体结构
C、所有部件
D、齿轮油 3、 行车中发动机突然熄火后,应_____。
A、立即减速停车
B、关闭点火开关
C、将变速器操纵杆置于空挡行驶
D、开启右转向灯,将车缓慢滑行到路边停车检查 4、 公安机关交通管理部门对非本辖区机动车有违法行为记录的,
可以将记录违法行为的信息、证据转至_____公安机关交通管理部门。
A、机动车号牌核发地
B、驾驶证核发地
C、资格证核发地
D、身份证核发地 5、 驾驶人进入驾驶室前,首先应_____并确认安全。
A、观察车辆周围情况
B、不用观察周围情况
C、开启车门直接上车
D、注意观察天气情况
6、 驾驶人在观察后方无来车的情况下,未开转向灯就
变更车道也是合理的。
对 错 7、 在泥泞路上制动时,车轮易发生侧滑或甩尾,导致
交通事故。
对 错 8、 交通事故当事人逃逸造成证据丢失,逃逸当事人承
担事故全部责任。
对 错 9、 骨折伤员固定伤处力求稳妥牢固,要固定骨折的两
端和上下两个关节。
对 错 10、 倒车前,应仔细观察倒车路线,确认具备安全倒车
条件后方可进行倒车。
对 错
解决方案 »
- 采用ZipInputStream解压zip格式的文件,遇到一个问题。。。。。
- Eclipse如何安装SVN插件啊?
- 关于作用域
- 在调用obj.wait()前为什么要先得到obj的monitor
- 请教一个关于继承的问题
- 在线等个JAVA 和SQL2000 不通过连接池的问题?
- 提示这种错误“Exception in thread "main" java.lang.NoClassDefFoundError: first"
- javaApplet是否不能加菜单?如果可以怎么加?
- 请问哪里有可以接收HTTP格式数据请求的JAVA组件下载
- 小弟新学 Java ,想各位同人问好!!!
- 总结了一些日期时间常用的方法,做为自己的工具类很适用.
- java 如何实现对远程主机UDP端口的扫描?
定義一個10元素的boolean類型的數組(boolean [] answered;),表示對應的問題是否已經出現過.
定義一個整型的實例變量(如:int currentQuestion;),代表當前的題目,點擊題目按鈕的時候,調用它的set方法(setCurrentQuestion(int questionIndex)),並且在set方法監測對應問題是否已經回答過(answered數組的對應元素是否是true),如果沒有回答過,就顯示對應的題目,同時設置數組的對應元素為true.如果已經回答過,再調用setCurrentQuestion(questionIndex + 1).
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}