猿问

java 异常处理,要想看效果写个main方法new出对象

package com.weixin.test17;


import java.util.Date;

import java.awt.Checkbox;

import java.awt.CheckboxGroup;

import java.awt.Choice;

import java.awt.Color;

import java.awt.ItemSelectable;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.ObjectInputStream.GetField;

import java.nio.CharBuffer;

import java.text.ParseException;

import java.text.SimpleDateFormat;


import javax.sound.midi.ControllerEventListener;

import javax.sound.sampled.Line;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;


/**

 * 考试模拟系统(单机)的设计

 * @author wangtenghui

 *

 */

public class TestDemo {

private JFrame window;

private JLabel title;

private JLabel titleright;

private JLabel left;

private JLabel right;

private JLabel leftDown;

private JLabel time;

private JLabel tishi;

private Choice choice;

private JCheckBox chose;

private TextField score;

private TextField content;

private JButton a;

private JButton b;

private JButton c;

private JButton d;

private JButton start;

private JDialog winDialog;

static int allsecond = 2*60*60;//考试时间定为2小时

String str[] ;

String p;

static int j = 1;

Thread th = new Thread(new StarLister());

public TestDemo(){

/*

* 主窗体的搭建

*/

window = new JFrame("标准化模拟考试");

window.setSize(800, 600);

window.setVisible(true);

window.setLayout(null);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//标签的搭建

title = new JLabel("欢迎考试,您的选择试题是:" );

title.setBounds(300,0,170,30);

title.setForeground(Color.blue);

window.add(title);

titleright = new JLabel("");

titleright.setBounds(480, 0, 90, 30);

titleright.setForeground(Color.blue);

window.add(titleright);

left = new JLabel("选择试题文件");

left.setBounds(150,50,100,30);

window.add(left);

right = new JLabel("试题内容:");

right.setBounds(500,50,100,30);

window.add(right);

leftDown = new JLabel("剩余时间:");

leftDown.setBounds(150,460,100,20);

window.add(leftDown);

time = new JLabel("");

time.setBounds(200, 480, 100, 20);

window.add(time);

//按钮的搭建

a = new JButton("查看分数");

a.setForeground(Color.blue);

a.setBounds(160,420,90,30);

window.add(a);

start = new JButton("开始答题");

start.setForeground(Color.blue);

start.setBounds(60, 420, 90, 30);

window.add(start);

start.addActionListener(new StarLister());

b = new JButton("暂停计时");

b.setBounds(150,520,100,30);

window.add(b);

c = new JButton("提交该题答案");

c.setForeground(Color.blue);

c.setBounds(530,500,120,30);

window.add(c);

d = new JButton("读取下一题");

d.setForeground(Color.blue);

d.setBounds(660, 500, 120, 30);

window.add(d);

d.addActionListener(new StarLister());

//文本内容框搭建

score = new TextField();

score.setBounds(20,140,310,280);

window.add(score);

content = new TextField();

content.setBounds(350,80,420,400);

window.add(content);

//选题框架的搭建

choice = new Choice();

choice.setBounds(20, 100, 310, 30);

choice.add("");

choice.add("A.text");

choice.add("B.text");

window.add(choice);

choice.addItemListener(new MyLister());

JPanel jp=new JPanel();//定义一个JPanel布局????

jp.add(choice);

window.add(jp);

chose = new JCheckBox();

Checkbox c1 = new Checkbox("A",false); 

Checkbox c2 = new Checkbox("B",false); 

Checkbox c3 = new Checkbox("C",false); 

Checkbox c4 = new Checkbox("D",false); 

c1.setBounds(350, 480, 50, 50);

c2.setBounds(400, 480, 50, 50);

c3.setBounds(450, 480, 50, 50);

c4.setBounds(500, 480, 50, 50);

window.add(c1);

window.add(c2);

window.add(c3);

window.add(c4);

tishi = new JLabel();

window.add(tishi);

winDialog = new JDialog(window,"考试提示",false);

winDialog.setBounds(500,200,400,200);

winDialog.setVisible(false);

winDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

tishi = new JLabel();

winDialog.add(tishi);

}

class MyLister implements ItemListener{

public void itemStateChanged(ItemEvent e) {

if(e.getSource()==choice){

Choice temp=(Choice)e.getSource();

p =temp.getSelectedItem();

titleright.setText(p);

}

}

}

class StarLister implements ActionListener,Runnable{


public void actionPerformed(ActionEvent e) {

String operator = e.getActionCommand();

  if("开始答题".equals(operator)){

  read();

  th = new Thread(this);

  th.start();

  }else if("读取下一题".equals(operator)){

  next();

  }

}

public void run() {

int hour = 0;

int second = 0;

int minutes = 0;

String time1 = "02:00:00";

String t = "HH:mm:ss";

SimpleDateFormat r = new SimpleDateFormat(t);

Date date = null;

while(allsecond > 0){

try {

th.sleep(1000);

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//时间的递减

allsecond--;

hour = hour/3600;

minutes = (allsecond-hour*3600)/60;

second = (allsecond-hour*3600-minutes*60);

time1 = hour + ":" + minutes + ":" + second;//将处理后的时分秒组合成字符串

try {

date = r.parse(time1);

} catch (ParseException e) {

tishi.setText(" 程序出现了异常");

winDialog.setVisible(true);

e.printStackTrace();

}

}

time.setText(r.format(date));//将格式化后的字符串输出

}


}

public void read() {          //读文件

BufferedReader in = null;

String  line = null;

String s = null;

int i = 0;

try {

   in = new BufferedReader(new FileReader("d:\\维信\\考试模拟试题\\" + p  ));

line = in.readLine();//按行来读取

while (line != null) {

line = in.readLine();

if(line.equals("****")){

str[i] = s;

i++;

}else{

s = s + line + "\n";

}

}

   content.setText(str[0]);

} catch (IOException e) {

tishi.setText("                      您未选择题目或未开始答题程序出现异常         ");

winDialog.setVisible(true);

e.printStackTrace();

}finally{

try {

in.close();

} catch (IOException e) {

tishi.setText("程序出现异常");

winDialog.setVisible(true);

e.printStackTrace();

}

}

}

public void next(){

if(j < 8)

content.setText(str[j]);

j++;

}

}


Jameslvt
浏览 1738回答 0
0回答
随时随地看视频慕课网APP

相关分类

Java
我要回答