java面向对象编程的实验心得体会_王颖奇 201771010129《面向对象程序设计Java》第十八周实验总结...

packageui_test;import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.ArrayList;import javax.swing.*;public class MyExGUI extendsJFrame {

ArrayList user_zongti = new ArrayList();

ArrayList user_zonganswer = new ArrayList();

ArrayList user_answer = new ArrayList();

ArrayList true_answer = new ArrayList();

ArrayList jta_timu = new ArrayList();

ArrayList jta_zong = new ArrayList();

ArrayList user_fenshu = new ArrayList();

JMenuBar jm;//菜单条组件

JMenu menu;//菜单

JMenuItem item1, item2;//菜单项

JMenu build; //二级菜单

JMenuItem file, project;

TextArea answer_all= newTextArea();

TextField jta= newTextField();

TextField jta_answer= newTextField();

JLabel num_answer= newJLabel();

JLabel answer;

JToolBar jtb;//工具条

JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb_next;intanswer_count;intanswer_fenshu;publicMyExGUI() {//创建菜单

jm = newJMenuBar();

menu= new JMenu("文件(F)");

menu.setMnemonic('f'); //助记符

build= new JMenu("新建");

file= new JMenuItem("文件");

project= new JMenuItem("答题");

item1= new JMenuItem("保存(S)");

item2= new JMenuItem("退出");

answer= new JLabel("第 1 题");//添加菜单项至菜单上

build.add(file);

build.add(project);

menu.add(build);

menu.add(item1);

menu.add(item2);

menu.addSeparator();//将菜单加入至菜单栏

jm.add(menu);

JPanel contentPanel= newJPanel();

contentPanel.setLayout(null);

JLabel daan= new JLabel("答案");

JLabel dengyu= new JLabel("=");

num_answer=answer;

num_answer.setFont(new Font("宋体", Font.BOLD, 22));

jb_next= new JButton("下一题");

jta.setFont(new Font("宋体", Font.BOLD, 22));

jta_answer.setFont(new Font("宋体", Font.BOLD, 22));

jb_next.setFont(new Font("宋体", Font.BOLD, 22));

daan.setFont(new Font("宋体", Font.BOLD, 22));

dengyu.setFont(new Font("宋体", Font.BOLD, 22));

contentPanel.add(num_answer);

contentPanel.add(daan);

contentPanel.add(dengyu);

contentPanel.add(jta);

contentPanel.add(jta_answer);

contentPanel.add(answer_all);

contentPanel.add(jb_next);

num_answer.setBounds(90, 20, 130, 50);

daan.setBounds(250, 20, 90, 50);

jta.setBounds(50, 70, 150, 30);

dengyu.setBounds(205, 70, 20, 20);

jta_answer.setBounds(230, 70, 100, 30);

jb_next.setBounds(350, 70, 110, 30);

answer_all.setBounds(50, 120, 400, 300);this.setJMenuBar(jm); //添加菜单栏,不能设定位置,会自动放在最上部

this.add(contentPanel);this.setTitle("在线答题系统");this.setSize(600, 500);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

item1.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent arg0) {

FileOutputStream outSTr= null;

BufferedOutputStream Buff= null;boolean flag = true;

File file;//String test ;

do{//test = "test"+count;

String inputValue= JOptionPane.showInputDialog("Please input file name");

file= new File(inputValue + "test.txt");if (!file.exists()) {//创建文件

try{

flag=file.createNewFile();

}catch(IOException e) {

e.printStackTrace();

}

flag= false;

}else{

JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE);

flag= true;

}

}while(flag);//写入文件

String u_answer;try{

outSTr= newFileOutputStream(file);

Buff= newBufferedOutputStream(outSTr);

System.out.println("选择是后执行的代码" + user_zongti.size() +user_answer.size());for (int i = 0; i < user_zongti.size(); i++) {try{

Buff.write(user_zongti.get(i).getBytes());

Buff.write(" ".getBytes());

u_answer=user_answer.get(i);if (u_answer.equals(""))

u_answer= "没有作答";

Buff.write(u_answer.getBytes());

Buff.write("\r\n".getBytes());

}catch(IOException e) {

e.printStackTrace();

i--;

}

}

Buff.flush();

Buff.close();

}catch(IOException e) {

e.printStackTrace();

}try{

outSTr.close();

}catch(IOException e) {

e.printStackTrace();

}

user_zongti.clear();

user_answer.clear();

}

});

project.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent arg0) {

arithmetic art= newarithmetic();

true_answer=art.list_answer;

jta_timu=art.list_timu;

jta_zong=art.list;

answer_count= 1;

answer_all.setText("");for (int i = 0; i < art.list_timu.size(); i++) {

user_zongti.add(jta_zong.get(i));

answer_all.append(jta_timu.get(i));

answer_all.append("\r\n");

}

num_answer.setText("第 " + answer_count + " 题");

jta.setText(jta_timu.get(answer_count- 1));

answer_count++;

}

});

jb_next.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent arg0) {

String temp;

temp=jta_answer.getText();if (jta.getText().equals("")) {

JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE);return;

}

jta_answer.setText("");if (answer_count <= 10) {if(isInteger(temp)) {

user_answer.add(temp);

System.out.println("选择否后执行的代码" + temp + "user_size" +user_answer.size());

num_answer.setText("第 " + answer_count + " 题");

jta.setText(jta_timu.get(answer_count- 1));

answer_count++;

}else{

JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE);

}

}else{

user_answer.add(temp);

System.out.println("选择否后执行的代码" + temp + "user_size" +user_answer.size());

answer_fenshu= 0;for (int i = 0; i < user_answer.size(); i++) {if(user_answer.get(i).equals(true_answer.get(i)))

answer_fenshu+= 5;

}

user_fenshu.add(answer_fenshu);

Object[] options= { "是", "取消"};int res = JOptionPane.showOptionDialog(null, "是否查看成绩", "答题完毕", JOptionPane.DEFAULT_OPTION,

JOptionPane.YES_NO_OPTION,null, options, options[0]);if (res ==JOptionPane.YES_OPTION) {

chart ct= newchart(user_fenshu);

ct.setVisible(true);

}else{

Object[] option= { "是", "取消"};int res1 = JOptionPane.showOptionDialog(null, "是否退出程序", "终止框", JOptionPane.DEFAULT_OPTION,

JOptionPane.YES_NO_OPTION,null, option, option[0]);if (res1 ==JOptionPane.YES_OPTION) {

dispose();

System.exit(0);

}else{

}

}

}

}

});

item2.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {

dispose();

System.exit(0);

}

});

}public static booleanisInteger(String str) {for (int i = str.length(); --i >= 0;) {if (!Character.isDigit(str.charAt(i))) {return false;

}

}return true;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值