设计一个计算机有四则运算java,Java实现小学四则运算练习系统(UI)(示例代码)...

1 packageui_test;2

3 import java.awt.*;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.io.BufferedOutputStream;7 importjava.io.File;8 importjava.io.FileOutputStream;9 importjava.io.IOException;10 importjava.util.ArrayList;11

12 import javax.swing.*;13

14

15 public class MyExGUI extendsJFrame{16 ArrayList user_zongti = new ArrayList();17 ArrayList user_zonganswer = new ArrayList();18 ArrayList user_answer = new ArrayList();19 ArrayList true_answer = new ArrayList();20 ArrayList jta_timu = new ArrayList();21 ArrayList jta_zong = new ArrayList();22 ArrayList user_fenshu = new ArrayList();23 JMenuBar jmb; //菜单条组件

24 JMenu menu1, menu2, menu3, menu4, menu5;//菜单

25 JMenuItem item1, item2, item3, item4, item5, item6;//菜单项

26 JMenu build; //二级菜单

27 JMenuItem file, project;28 TextArea answer_all = newTextArea();29 TextField jta = newTextField();30 TextField jta_answer = newTextField();31 JLabel num_answer = newJLabel();32 JLabel answer1;33 JToolBar jtb;//工具条

34 JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7,jb_next;35 intanswer_count;36 intanswer_fenshu;37 publicMyExGUI()38 {39 //创建菜单

40 jmb = newJMenuBar();41

42 menu1 = new JMenu("文件(F)");43 menu1.setMnemonic(\'f\'); //助记符

44 menu2 = new JMenu("编辑");45 menu2.setMnemonic(\'E\');46 menu3 = new JMenu("格式");47 menu4 = new JMenu("查看");48 menu5 = new JMenu("帮助");49

50 build = new JMenu("新建");51

52 file = new JMenuItem("文件");53 project = new JMenuItem("答题");54 item1 = new JMenuItem("打开");55 item2 = new JMenuItem("保存(S)");56 item3 = new JMenuItem("另存为");57 item4 = new JMenuItem("页面设置");58 item5 = new JMenuItem("打印");59 item6 = new JMenuItem("退出");60

61 answer1 = new JLabel("第 1 题");62

63

64

65 //添加菜单项至菜单上

66 build.add(file);67 build.add(project);68

69 menu1.add(build);70 menu1.add(item1);71 menu1.add(item2);72 menu1.add(item3);73 menu1.addSeparator();74 menu1.add(item4);75 menu1.add(item5);76 menu1.add(item6);77 //将菜单加入至菜单栏

78 jmb.add(menu1);79 jmb.add(menu2);80 jmb.add(menu3);81 jmb.add(menu4);82 jmb.add(menu5);83

84

85 JPanel contentPanel = newJPanel();86 contentPanel.setLayout(null);87 JLabel daan = new JLabel("答案");88 JLabel dengyu = new JLabel("=");89 num_answer=answer1;90 num_answer.setFont(new Font("宋体",Font.BOLD, 22));91 jb_next = new JButton("下一题");92 jta.setFont(new Font("宋体",Font.BOLD, 22));93 jta_answer.setFont(new Font("宋体",Font.BOLD, 22));94 jb_next.setFont(new Font("宋体",Font.BOLD, 22));95 daan.setFont(new Font("宋体",Font.BOLD, 22));96 dengyu.setFont(new Font("宋体",Font.BOLD, 22));97

98 contentPanel.add(num_answer);99 contentPanel.add(daan);100 contentPanel.add(dengyu);101 contentPanel.add(jta);102

103 contentPanel.add(jta_answer);104 contentPanel.add(answer_all);105 contentPanel.add(jb_next);106

107 num_answer.setBounds(90, 20, 130, 50);108 daan.setBounds(250, 20, 90, 50);109 jta.setBounds(50, 70, 150, 30);110 dengyu.setBounds(205, 70, 20, 20);111 jta_answer.setBounds(230, 70, 100, 30);112 jb_next.setBounds(350, 70, 110, 30);113 answer_all.setBounds(50, 120, 400, 300);114

115 this.setJMenuBar(jmb); //添加菜单栏,不能设定位置,会自动放在最上部

116 this.add(contentPanel);117

118 this.setTitle("在线答题系统");119 this.setSize(600, 500);120 this.setVisible(true);121 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);122 item1.addActionListener(newActionListener() {123

124 @Override125 public voidactionPerformed(ActionEvent arg0) {126 //TODO Auto-generated method stub

127 JFileChooser jfc=newJFileChooser();128 jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );129 jfc.showDialog(new JLabel(), "选择");130 File file=jfc.getSelectedFile();131 if(file.isDirectory()){132 //System.out.println("文件夹:"+file.getAbsolutePath());

133 }else if(file.isFile()){134 String s=file.getAbsolutePath();135 }136 }137 });138 item2.addActionListener(newActionListener() {139

140 @Override141 public voidactionPerformed(ActionEvent arg0) {142 FileOutputStream outSTr = null;143 BufferedOutputStream Buff = null;144 boolean flag = true;145 File file;146 //String test ;

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

149

150 String inputValue = JOptionPane.showInputDialog("Please input file name");151 file = new File(inputValue+".txt");152 if (!file.exists()) {153 //创建文件

154 try{155

156 flag=file.createNewFile();157

158 } catch(IOException e) {159 e.printStackTrace();160

161 }162 flag=false;163 }164 else{165

166 JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE);167 flag=true;168 }169 }while(flag);170 //写入文件

171 String u_answer;172 try{173 outSTr = newFileOutputStream(file);174 Buff = newBufferedOutputStream(outSTr);175 System.out.println("选择是后执行的代码"+user_zongti.size()+user_answer.size());176 for (int i = 0; i < user_zongti.size(); i++)177 {178 try{179 Buff.write(user_zongti.get(i).getBytes());180 Buff.write(" ".getBytes());181 u_answer =user_answer.get(i);182 if(u_answer.equals(""))183 u_answer ="没有作答";184

185 Buff.write(u_answer.getBytes());186 Buff.write("\\r\\n".getBytes());187 } catch(IOException e) {188 e.printStackTrace();189 i--;190 }191 }192 Buff.flush();193 Buff.close();194

195 } catch(IOException e) {196 e.printStackTrace();197 }198 //Buff.close();

199 try{200 outSTr.close();201 } catch(IOException e) {202 e.printStackTrace();203 }204 user_zongti.clear();205 user_answer.clear();206 }207 });208

209

210 project.addActionListener(newActionListener() {211

212 @Override213 public voidactionPerformed(ActionEvent arg0) {214 arithmetic art = newarithmetic();215 true_answer=art.list_answer;216 jta_timu =art.list_timu;217 jta_zong =art.list;218 answer_count=1;219 answer_all.setText("");220 for (int i = 0; i < art.list_timu.size(); i++)221 {222 user_zongti.add(jta_zong.get(i));223 answer_all.append(jta_timu.get(i));224 answer_all.append("\\r\\n");225 }226 num_answer.setText("第 "+answer_count+" 题");227 jta.setText(jta_timu.get(answer_count-1));228 answer_count++;229

230

231 }232 });233 jb_next.addActionListener(newActionListener() {234

235 @Override236 public voidactionPerformed(ActionEvent arg0) {237 String temp;238 temp =jta_answer.getText();239

240 if(jta.getText().equals(""))241 {242 JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE);243 return;244 }245 jta_answer.setText("");246 if(answer_count<=20)247 {248 if(isInteger(temp))249 {250

251 user_answer.add(temp);252 System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size());253 num_answer.setText("第 "+answer_count+" 题");254 jta.setText(jta_timu.get(answer_count-1));255 answer_count++;256 }257 else{258 JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE);259 }260 }261 else{262 user_answer.add(temp);263 System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size());264 answer_fenshu=0;265 for(int i=0;i

279 }else{280 Object[] option = { "是", "取消"};281 int res1=JOptionPane.showOptionDialog(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值