程序开发简介:
【开发环境】:eclipse
【开发人员】:Ives & 杜殷浩
【博客地址】:杜殷浩
【开发时间】:2015-04-09
【版本】:1.0
【功能】:
1.随机获取算式
2.答题结束后能显示用户答对数目
3.答题结束后显示全部题目的答案
4.用户第一次答题输入用户逆臣
5.能够改变界面的皮肤
【分工】:Ives:
界面的设计、改变界面的背景、保存用户昵称,连接数据库assess
杜殷浩:
随机获取算式,检验用户答案正确与否,显示题目答案,
【特点】:
1.连接了access数据库,能够保存并调用用户昵称
2.能够改变界面的皮肤
3.用户答完题弹出窗体提示题目答案与答对数目
【缺陷】:
1.不能处理小数(四舍五入)与分数
2.不能重新做上次的题目
3.没有限制用户答题的时间
4.没有指出用户答错的题目
5.不能选择运算的符号
6.没有带括号的表达式
7.用户不能选择答题数目和设置答题的时间
8.最后没有算出用户得到的分数
9.不能保存用户答题的记录和答题的分数记录
10.答题界面不能更换皮肤
程序具体代码:
1 package com.ives; 2 3 4 import java.awt.*; 5 6 import javax.swing.*; 7 import com.ives.Login; 8 import java.awt.event.ActionListener; 9 import java.awt.event.ActionEvent; 10 import java.sql.Connection; 11 import java.sql.DriverManager; 12 import java.sql.SQLException; 13 import java.sql.Statement; 14 import com.ives.Jisuan; 15 /** 16 * 作者 :Ives 17 * 创建时间:2015年4月5日 下午19::16 18 * 类说明:四则运算小程序 19 */ 20 public class Login { 21 22 private JFrame frame; 23 private JLabel lblNewLabel_1; 24 private JButton button; 25 public static JTextField textField; 26 static JButton btnNewButton; 27 //使用MPanel这个类来创建panel,这样可以创建带背景的面板 28 MPanel panel = new MPanel(this.getClass().getResource("/img/background2.jpg")); 29 public static void main(String[] args) { 30 EventQueue.invokeLater(new Runnable() { 31 public void run() { 32 try { 33 Login window = new Login(); 34 window.frame.setVisible(true); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 }); 40 } 41 42 public Login() { 43 initialize(); 44 } 45 46 private void initialize() { 47 frame = new JFrame(); 48 frame.setTitle("\u7528\u6237\u767B\u9646"); 49 frame.setSize(493,333); 50 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 51 frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS)); 52 53 Toolkit kit = Toolkit.getDefaultToolkit(); 54 Dimension screen = kit.getScreenSize();//获取屏幕高宽 55 int x = screen.width; 56 int y = screen.height; 57 int xcenter = (x-493)/2; 58 int ycenter = (y-333)/2; 59 frame.setLocation(xcenter,ycenter);//设置窗体居中 60 61 frame.getContentPane().add(panel); 62 panel.setLayout(null); 63 64 JLabel lblNewLabel = new JLabel(" \u5C0F\u5B66\u751F\u56DB\u5219\u8FD0\u7B97\u7A0B\u5E8F "); 65 lblNewLabel.setBounds(0, 29, 504, 34); 66 panel.add(lblNewLabel); 67 lblNewLabel.setFont(new Font("华文行楷", Font.BOLD, 30)); 68 69 lblNewLabel_1 = new JLabel("\u6635\u79F0 "); 70 lblNewLabel_1.setBounds(95, 121, 68, 28); 71 panel.add(lblNewLabel_1); 72 lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT); 73 lblNewLabel_1.setVerticalAlignment(SwingConstants.TOP); 74 lblNewLabel_1.setFont(new Font("华文行楷", Font.PLAIN, 25)); 75 76 textField = new JTextField(); 77 textField.setBounds(181, 123, 156, 27); 78 panel.add(textField); 79 textField.setFont(new Font("华文行楷", Font.PLAIN, 18)); 80 textField.setColumns(15); 81 82 button = new JButton("\u786E\u5B9A"); 83 button.addActionListener(new buttonActionListener()); 84 button.setBounds(137, 229, 77, 29); 85 panel.add(button); 86 button.setFont(new Font("华文行楷", Font.PLAIN, 18)); 87 88 btnNewButton = new JButton("\u53D6\u6D88"); 89 btnNewButton.addActionListener(new ActionListener() { 90 public void actionPerformed(ActionEvent e) { 91 frame.dispose();//窗体关闭 92 } 93 }); 94 btnNewButton.setBounds(283, 229, 77, 29); 95 panel.add(btnNewButton); 96 btnNewButton.setFont(new Font("华文行楷", Font.PLAIN, 18)); 97 } 98 99 class buttonActionListener implements ActionListener { 100 public void actionPerformed(ActionEvent e) { 101 try { 102 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//调用jdbc.odbc驱动 103 } catch (ClassNotFoundException e1) { 104 e1.printStackTrace(); 105 } 106 try{ //连接access数据库,Nichen是数据库的名臣 107 String url = "jdbc:odbc:Nichen.mdb"; 108 Connection con=DriverManager.getConnection(url); 109 Statement sql=con.createStatement(); 110 String nichen=textField.getText().trim(); 111 if(textField.getText().equals("")) 112 { //如果用户没有输入昵称,则提示用户 113 JOptionPane.showMessageDialog(null,"请输入昵称","提示!", JOptionPane.YES_NO_OPTION); 114 } 115 else{ 116 //向Nichen表的属性nichen插入数据 117 String s="insert into Nichen(nichen) values('"+textField.getText()+"')"; 118 Jisuan jisuan = new Jisuan(); 119 jisuan.setVisible(true); 120 frame.setVisible(false); 121 con.close(); //断开与数据库的连接 122 } 123 } catch(SQLException g) { 124 System.out.println("E Code"+g.getErrorCode()); //获取sql server的系统错误代号 125 System.out.println("E M"+g.getMessage()); 126 } 127 } 128 } 129 } 130 131 132 package com.ives; 133 134 import javax.swing.JFrame; 135 import javax.swing.JLabel; 136 import java.awt.Font; 137 import javax.swing.JButton; 138 import java.awt.event.ActionListener; 139 import java.awt.event.ActionEvent; 140 import java.awt.event.MouseAdapter; 141 import java.awt.event.MouseEvent; 142 143 import com.dyh.suanshi; 144 145 public class Jisuan extends JFrame{ 146 147 int i = 1;//计数器 148 private JFrame frame; 149 public MPanel panel = new MPanel(this.getClass().getResource("/img/background2.jpg")); 150 151 public static void main(String args[]) { 152 try { 153 Jisuan frame = new Jisuan(); 154 frame.setVisible(true); 155 } catch (Exception e) { 156 e.printStackTrace(); 157 } 158 } 159 160 public Jisuan() { 161 super();//继承父类的构造方法 162 setTitle("\u56DB\u5219\u8FD0\u7B97\u7CFB\u7EDF"); 163 setBounds(100, 100, 550, 550); 164 setAlwaysOnTop(true); 165 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 166 panel.setLayout(null);// 设置面板的布局管理器为null 167 getContentPane().add(panel);// 将面板添加到窗体 168 169 JLabel label = new JLabel("\u56DB\u5219\u8FD0\u7B97\u7CFB\u7EDF"); 170 label.setLocation(166, 10); 171 label.setSize(220, 40); 172 panel.add(label); 173 label.setFont(new Font("华文行楷", Font.PLAIN, 35)); 174 175 176 JButton btnNewButton = new JButton("\u8BBE\u7F6E\u76AE\u80A4"); 177 btnNewButton.setLocation(79, 117); 178 btnNewButton.setSize(122, 45); 179 panel.add(btnNewButton); 180 btnNewButton.setFont(new Font("华文行楷", Font.PLAIN, 20)); 181 182 btnNewButton.addMouseListener(new MouseAdapter()//鼠标监听 183 { 184 185 public void mouseClicked(MouseEvent e) 186 { 187 if(e.getClickCount()==1) 188 { 189 i++; 190 System.out.println("i="+i); 191 } 192 } 193 }); 194 195 btnNewButton.addActionListener(new ActionListener()//按钮监听 196 { 197 public void actionPerformed(ActionEvent e) { 198 199 if(i == 1) 200 panel.mpanel(this.getClass().getResource("/img/backgrouda.jpg")); 201 202 if(i == 2) 203 panel.mpanel(this.getClass().getResource("/img/background2.jpg")); 204 205 if(i ==3) 206 { 207 panel.mpanel(this.getClass().getResource("/img/lvse.jpg")); 208 i = 0; 209 } 210 } 211 }); 212 213 214 215 JButton button_1 = new JButton("\u8BBE\u7F6E\u65F6\u95F4"); 216 button_1.setLocation(340, 117); 217 button_1.setSize(122, 45); 218 panel.add(button_1); 219 button_1.addActionListener(new ActionListener() { 220 public void actionPerformed(ActionEvent e) { 221 } 222 }); 223 button_1.setFont(new Font("华文行楷", Font.PLAIN, 20)); 224 225 JButton btnNewButton_1 = new JButton("\u67E5\u770B\u5206\u6570"); 226 btnNewButton_1.setLocation(340, 214); 227 btnNewButton_1.setSize(122, 45); 228 panel.add(btnNewButton_1); 229 btnNewButton_1.addActionListener(new ActionListener() { 230 public void actionPerformed(ActionEvent e) { 231 } 232 }); 233 btnNewButton_1.setFont(new Font("华文行楷", Font.PLAIN, 20)); 234 235 236 JButton button = new JButton("\u81EA\u4E3B\u547D\u9898"); 237 button.setLocation(79, 214); 238 button.setSize(122, 45); 239 panel.add(button); 240 button.setFont(new Font("华文行楷", Font.PLAIN, 20)); 241 242 JButton btnNewButton_2 = new JButton("\u8FDB\u5165\u7B54\u9898"); 243 btnNewButton_2.setFont(new Font("华文行楷", Font.PLAIN, 25)); 244 btnNewButton_2.setBounds(198, 330, 152, 53); 245 panel.add(btnNewButton_2); 246 247 btnNewButton_2.addActionListener(new ActionListener() { 248 public void actionPerformed(ActionEvent e) { 249 suanshi suanshi=new suanshi(); 250 suanshi.setVisible(true); 251 frame.setDefaultCloseOperation(HIDE_ON_CLOSE); 252 } 253 }); 254 } 255 } 256 257 public class MPanel extends JPanel { 258 259 private ImageIcon imageIcon;// 声明一个图片对象 260 protected MPanel MPanel1; 261 262 public MPanel(URL imgUrl) { 263 super();// 继承父类的构造方法 264 imageIcon = new ImageIcon(imgUrl);// 根据传入的URL创建ImageIcon对象 265 setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());// 设置面板与图片等大 266 } 267 public void mpanel(URL imgUrl) { 268 //setLayout(new GridBagLayout());// 将布局管理器修改为网格组布局 269 imageIcon = new ImageIcon(imgUrl);// 根据传入的URL创建ImageIcon对象 270 setSize(imageIcon.getIconWidth(), imageIcon.getIconHeight());// 设置面板与图片等大 271 } 272 protected void paintComponent(Graphics g) {// 重写JPanel类的paintComponent()方法 273 super.paintComponent(g);// 调用JPanel类的paintComponent()方法 274 Image image = imageIcon.getImage();// 通过ImageIcon对象获得Image对象 275 g.drawImage(image, 0, 0, null);// 绘制Image对象,即将图片绘制到面板中 276 } 277 }