JButton b3 = new JButton(\"3\"); JButton b4 = new JButton(\"+\"); JButton b5 = new JButton(\"^\"); JButton b6 = new JButton(\"4\"); JButton b7 = new JButton(\"5\"); JButton b8 = new JButton(\"6\"); JButton b9 = new JButton(\"-\"); JButton b10 = new JButton(\"√\"); JButton b11 = newJButton(\"7\"); JButton b12 = new JButton(\"8\"); JButton b13 = newJButton(\"9\"); JButton b14 = new JButton(\"*\"); JButton b15 = newJButton(\"%\"); JButton b16 = new JButton(\".\");
JButton b17 = newJButton(\"0\"); JButton b18 = new JButton(\"=\"); JButton b19 = newJButton(\"/\"); JButton b20 = new JButton(\"log\"); JButton clc = newJButton(\"clear\"); 5.构建两个小面板对象p1 and p2
JPanel p1 = new JPanel();JPanel p2 = new JPanel(); JPanel p3 = new JPanel();
public Computer1(){setTitle(\"计算器\");setResizable(false); setBounds(200, 200, 330, 330); setDefaultCloseOperation;
6.使用BoderLayout布局,对文本框进行设计 add(p1, \"North\");
(false); quals(\"0\")) {(s);}
else {String str = () + s;(str);} }
7.符号处理,使用if语句对对应符号响应进行。 if(i==1){ ndexOf('.') < 0) {s = () + \".\";(s); } } 8.字符串转Double类型
public void eql() { num2 = ());
if (add)num3 = num1 + num2;if (mul)num3 = num1 * num2;
if (sub)num3 = num1 - num2;if (div)num3 = num1 / num2; if (squ) num3 = num1 * num1;if (ext) num3 = (num1); if (rem) num3 = num1 % num2;if (dus) num3 = (num1); String s = (num3); 9.将结果附给文本框 (s);end = true; public static void main(String[] args) { new Computer1();}} 4.3.2手机信息管理系 开始 手机信息管理系统 是否连接 Y N 录入 修改 查询 删除 连接失败 结束 手机基础信息管理系统流程图4.3.2 1.在Microsoft SQL Serverz中建立数据库useinfo并在其中建立表mphone。如下表所示: 数据库表mphone设计表4.3.1 列名 number brand type system edition 数据类型 长度 允许空 int varcher varcher varcher varcher 4 50 50 50 50 50 √ √ √ √ √ √ unitprice varcher 2.在MyEclipse中建立与数据库连接的程序。建立类 DBConn public Connection mphone(String name, String pass) { Connection c = null; 立手机信息管理系统的面板,并使其可以与数据库表mphone连接并与查询、录入、修改、删除命令连接起来,实现对表mphone的查询、录入、修改和删除。 (1)建立 面板PhoneManager 继承 JFrame 和 ActionListener PhoneSituation 基本信息录入=null; ModifySituation基本信息修改=null; Inquest 基本信息查询=null; Delete基本信息删除=null; JMenuBar bar; JMenu fileMenu; JMenuItem 录入,修改,查询,删除; Container con=null; Hashtable 基本信息=null; File file=null; CardLayout card=null; JLabel label=null; JPanel pCenter; public StudentManager() (2)建立菜单选项
录入=new JMenuItem(\"录入手机基本信息\"); 修改=new JMenuItem(\"修改手机基本信息\"); 查询=new JMenuItem(\"查询手机基本信息\"); 删除=new JMenuItem(\"删除手机基本信息\"); bar=new JMenuBar();
fileMenu=new JMenu(\"菜单选项\"); (3)加入菜单选项 (录入);(修改); (查询);(删除);
(fileMenu);setJMenuBar(bar); (4)初始化标签
label=new JLabel(\"欢迎使用手机基本信息管理\
(new Font(\"TimesRoman\ ;基本信息=new Hashtable(); (5)加入监听
录入.addActionListener(this);修改.addActionListener(this); 查询.addActionListener(this);删除.addActionListener(this); card=new CardLayout();con=getContentPane(); pCenter=new JPanel();(card); file=new File(\"基本信息.txt\");
if(!()){try{FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); (基本信息);
();();}catch(IOException e){}}
基本信息录入=new StudentSituation(file); 基本信息修改=new ModifySituation(file); 基本信息查询=new Inquest(this,file); 基本信息删除=new Delete(file); (\"欢迎语界面\ (\"录入界面\基本信息录入); (\"修改界面\基本信息修改); (\"删除界面\基本信息删除); (pCenter,;
();addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e){(0);}}); setVisible(true);setBounds(100,50,420,380);validate();} public void actionPerformed(ActionEvent e) { if()==录入){(pCenter,\"录入界面\"); }else if()==修改) {(pCenter,\"修改界面\");
}else if()==查询){基本信息查询.setVisible(true); }else if()==删除){(pCenter,\"删除界面\");}}
public static void main(String args[]){ new PhoneManager();}} 4.设计删除程序。
(1)连接数据库连接程序DBConn,并于PhoneManager连接让删除基础信息可以操作先设计删除面板。
Hashtable 基本信息表 = null;
JTextField 号码,品牌 ,型号,版本, 单价;JRadioButton 苹果, 安卓; JButton 删除;ButtonGroup group = null;FileInputStream inOne = null; ObjectInputStream inTwo = null;FileOutputStream outOne = null;
ObjectOutputStream outTwo = null;
File file = null;private static Connection con = null; private static PreparedStatement sql = null; public Delete(File file) { = file;
号码 = new JTextField(10);删除 = new JButton(\"删除\"); 号码.addActionListener(this);删除.addActionListener(this); 品牌 = new JTextField(10);品牌.setEditable(false); 型号 = new JTextField(10);型号.setEditable(false); 版本 = new JTextField(10);版本.setEditable(false); 单价 = new JTextField(10);单价.setEditable(false); 苹果 = new JRadioButton(\"苹果\ 安卓 = new JRadioButton(\"安卓\ group = new ButtonGroup();(苹果);(安卓); Box box1 = ();
(new JLabel(\"输入要删除的号码:\ (号码);(删除); Box box2 = ();
(new JLabel(\"品牌:\ (品牌);Box box3 = (); (new JLabel(\"系统:\ (苹果);(安卓); Box box4 = ();
(new JLabel(\"型号:\ (型号);Box box5 = (); (new JLabel(\"版本:\ (版本);Box box6 = (); (new JLabel(\"单价:\
(单价);Box boxH = (); (box1);(box2);(box3);(box4); (box5);(box6);());
JPanel pCenter = new JPanel(); (boxH);setLayout(new BorderLayout()); add(pCenter, ;validate();}
public void actionPerformed(ActionEvent e) { if () == 删除 || () == 号码) {
(2)从数据库中删除指定记录,先连接数据库然后对数据库进行删除处理。 DBConn link=new DBConn(); con =(\"sa\
if (con == null) {\"连接失败\");(0);}
try {sql=(\"DELETE FROM MPHONE WHERE NUMBER=\"); (1,号码.getText());
();} catch (SQLException e1) { ();}finally{try {();
} catch (SQLException e1) {();}}}}} 5. 建立查询。
(1)建立一个查询面板。 Hashtable 基本信息表=null;
JTextField 号码,品牌,型号,版本,单价;JRadioButton 苹果,安卓; JButton 查询;ButtonGroup group=null;FileInputStream inOne=null; ObjectInputStream inTwo=null; File file=null;
private static Connection con = null;private static Statement sql = null;
private static ResultSet rs = null;public Inquest(JFrame f,File file)
{super(f,\"查询对话框\
号码=new JTextField(10);查询=new JButton(\"查询\");
号码.addActionListener(this);查询.addActionListener(this); 品牌=new JTextField(10);品牌.setEditable(false); 型号=new JTextField(10);型号.setEditable(false); 版本=new JTextField(10);版本.setEditable(false); 单价=new JTextField(10);单价.setEditable(false);
苹果=new JRadioButton(\"苹果\安卓=new JRadioButton(\"安卓\
group=new ButtonGroup();(苹果);(安卓); Box box1=();
(new JLabel(\"输入要查询的号码:\ (号码);(查询);
Box box2=(); (new JLabel(\"品牌:\品牌); Box box3=(); (new JLabel(\"系统:\ (苹果); (安卓);
Box box4=(); (new JLabel(\"型号:\型号); Box box5=(); (new JLabel(\"版本:\版本); Box box6=();
(new JLabel(\"单价:\单价); Box boxH=(); (box1);(box2); (box3); (box4); (box5); (box6); ());
JPanel pCenter=new JPanel(); (boxH);
Container con=getContentPane(); (pCenter,; ();
setVisible(false);
setBounds(100,200,360,270);
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent
e){setVisible(false);}});}
public void actionPerformed(ActionEvent e)
{品牌.setText(null);型号.setText(null);版本.setText(null);
单价.setText(null); if()==查询||()==号码){ (2)连接数据库并从数据库查询数据
DBConn link=new DBConn();con =(\"sa\ if (con == null) {\"连接失败\");(0);} try {sql = ();
while ()) {品牌.setText(2)); 型号.setText(4));
版本.setText(5));单价.setText(6)); if(3).equals(\"苹果\")){苹果.setSelected(true);
}else{安卓.setSelected(true);}}} catch (SQLException e1) {();} finally{try {();}
catch (SQLException e1) {();}}立修改类 ModifySituation 继承 JPanel和 ActionListene
(1)设计修改手机信息版面
Hashtable 基本信息表 = null;
JTextField 号码, 品牌, 型号, 版本, 单价;JRadioButton 苹果,安卓; ButtonGroup group = null; JButton 开始修改, 录入修改, 重置;
FileInputStream inOne = null;ObjectInputStream inTwo = null; FileOutputStream outOne = null;ObjectOutputStream outTwo = null; File file = null;
private static Connection con = null;private static PreparedStatement sql = null;
public ModifySituation(File file) { = file;
号码 = new JTextField(10);品牌 = new JTextField(10); 型号 = new JTextField(10);版本 = new JTextField(10); 单价 = new JTextField(10);group = new ButtonGroup(); 苹果 = new JRadioButton(\"苹果\ 安卓 = new JRadioButton(\"安卓\ (苹果);(安卓);
开始修改 = new JButton(\"开始修改\");重置 = new JButton(\"重置\"); 号码.addActionListener(this);开始修改.addActionListener(this); 重置.addActionListener(this); Box box1 = ();
(new JLabel(\"输入要修改信息的号码:\ (号码);(开始修改); Box box2 = ();
(new JLabel(\"(新)品牌:\品牌); Box box3 = ();
(new JLabel(\"(新)系统:\苹果); (安卓);
Box box4 = ();
(new JLabel(\"(新)型号:\型号); Box box5 = ();
(new JLabel(\"(新)版本:\版本); Box box6 = ();
(new JLabel(\"(新)单价:\单价); Box boxH = ();
(box1);(box2);(box3);(box4); (box5);(box6);());
JPanel pCenter = new JPanel();(boxH); setLayout(new BorderLayout());add(pCenter, ; (重置);add(pSouth, ;validate();}
public void actionPerformed(ActionEvent e) { if () == 开始修改 || () == 号码) {
(2)更新数据库信息,先连接数据库在对数据库进行修改。 DBConn link=new DBConn();con =(\"sa\ if (con == null) {\"连接失败\");(0);} try {sql=(sql1); String system = null;
if (苹果.isSelected()) {system = 苹果.getText();} else {system = 安卓.getText();}
(1, 号码.getText());(2, 品牌.getText()); (6, system);(3, 型号.getText());
(4, 版本.getText());(5, 单价.getText()); ();} catch (SQLException e1) {();}
finally{try {();} catch (SQLException e1) {();}} etText(null);品牌.setText(null);
型号.setText(null);版本.setText(null);单价.setText(null);}}}
7.录入数据库手机管理系统信息(1)
建立类 StudentSituation继承 JPanel 和ActionListener设计录入面板 Hashtable 基本信息表=null; JTextField 号码,品牌,型号,版本,单价; JRadioButton 苹果,安卓; Phone 手机=null; ButtonGroup group=null; JButton 录入,重置;FileInputStream inOne=null;
ObjectInputStream inTwo=null; FileOutputStream outOne=null; ObjectOutputStream outTwo=null; File file=null; private static Connection con = null; private static PreparedStatement sql = null;
public StudentSituation(File file) { = file; 号码 = new JTextField(10);品牌 = new JTextField(10); 型号 = new JTextField(10);版本 = new JTextField(10); 单价 = new JTextField(10);group = new ButtonGroup(); 苹果 = new JRadioButton(\"苹果\ 安卓 = new JRadioButton(\"安卓\ (苹果);(安卓);
录入 = new JButton(\"录入\");重置 = new JButton(\"重置\"); 录入.addActionListener(this);重置.addActionListener(this); Box box1 = ();
(new JLabel(\"号码:\号码); Box box2 = ();
(new JLabel(\"品牌:\品牌); Box box3 = ();
(new JLabel(\"系统:\苹果); (安卓); Box box4 = ();
(new JLabel(\"型号:\型号);
Box box5 = ();
(new JLabel(\"版本:\版本); Box box6 = ();
(new JLabel(\"单价:\单价); Box boxH = ();
(box1); (box2); (box3); (box4); (box5); (box6); ());
JPanel pCenter = new JPanel();(boxH); setLayout(new BorderLayout());add(pCenter, ; JPanel pSouth = new JPanel();(录入); (重置); add(pSouth, ; validate();}
(2)实现录入,将数据保存到数据库中,打开连接SQLServer的驱动 public void actionPerformed(ActionEvent e) { if () == 录入) {DBConn link=new DBConn(); con =(\"sa\ \"连接失败\");(0);}String system = null;
if (苹果.isSelected()) {system = 苹果.getText(); } else {system = 安卓.getText();}try { sql=(\"INSERT INTO
MPHONE(number,brand,type,edition,unitprice,system) VALUES(,,,,,)\");
(1, 号码.getText());(2, 品牌.getText()); (6, system); (3, 型号.getText()); (4, 版本.getText());(5, 单价.getText()); ();} catch (SQLException e1) {();
}finally {try {();} catch (SQLException e1) {();}} } if () == 重置) {
号码.setText(null);品牌.setText(null);型号.setText(null);
版本.setText(null);单价.setText(null); }}}
8.建立Phone类继承 用于连接修改数据ModifySituation用于对数据进行处
理
String number,brand,type,edition,unitprice,system; public Student(){}public void setNumber(String number) { =number; }public String getNumber(){return number;} public void setBrand(String brand) {=brand; }
public String getBrand(){return brand; }public void setType(String type){
=type; }public String getType(){return type; } public void setEdition(String edition) {=edition; } public String getEdition(){return edition; }
public void setUnitprice(String unitprice) {=unitprice; } public String getUnitprice(){return unitprice; } public void setSystem(String system) {=system; } public String getSystem(){return system; }}
测试与分析 4.4.1测试 一、简单计算器测试 示例1:求1234+4321输出结果如下 示例2:求25的平方输出结果如下 二、手机管理系统测试 示例1:录入手机基本信息如下图 示例2:手机信息查询如下图所示 4.4.2分析
因为第一次设计JAVA程序,有很多地方没有想到,这次设计计算器,也有很多没有完善的地方,比如:计算器除数不能为0,但我们这次设计的计算器没考虑到这个方面,使得计算器在这种情况下不能正常计算,后经过老师的知道和查阅参考书,解决了这个问题,只需一行代码即可:if(d3==0) (\"除数不能为零\")。
5 总结与展望
课程设计是培养学生综合运用所学知识、发现、提出、分析并解决实际问题和锻炼实践能力的重要环节, 通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和思考的能力。本次课程设计虽然很辛苦,实在是受益匪浅。在设计的过程中发现了自己的不足之处,对以前所学过的知识理解得不够深刻,掌握得不够牢固,通过这次课程设计之后,一定把以前所学过的知识重新温故。
在此次的课程设计实践中,我学到了教科书上没有的东西。这次课程设计,我按照课程设计的要求,分块实现程序的功能。在实践中我遇到许多的问题,在老师和同学的帮助下最后都克服了,并成功的完成了这次课程设计。
本次课程设计中我充分理解了Java面对对象的特性。在实践的同时,我又从中成长了许多,特别是在对程序的编译和调试中掌握了许多的技巧和方法,为今后的其他计算机语言的学习奠定了一定的基础。这次的程序设计,从一个简单的计算器就可以看出JAVA的程序设计不是那么简单,仅仅是一个小小的计算器,就已经有很多需要注意的问题,一不小心就会出错,还会有很多地方没怎么明白,而计算器的设计是最简单的几个设计其中之一,可见,平时的知识积累是很重要的。没有知识的积累,再简单的东西也不可能设计出来。
课程设计让我们练习了动手能力,使我们平时学习的知识得以在实际中应用,不会学死书,活学活用,很大程度上提高了我们的能力,对以后的学习、工作、生活也会有一定的帮助。 参考文献 [1] 刘万军,郑少京,王松波,梁清华,等.JAVA 6程序设计实践教程[M].2009年5月第1版, 北京:清华大学出版社 [2] 王珊,萨师煊.数据库系统概论(第4版)[M].2006年5月第4版, 北京:高等教育出版社 [3] 耿祥义等.JAVA课程设计第二版[M].北京:清华大学出版社 [4]屈辉立,陈可明,石武信.JSP网站编程教程[M].第1版, 北京:北京希望电子出版社,2005 [5] 白勇.用B/S模式构建在线考试系统[J],重庆电力高等专科学校学报,2003,10(4): 100~130. [6] Jiang Guo,Yuehong Liao,Behzad Survey of J2EE Application Performance Management Systems[J],Proceedings of the IEEE International Conference on Web Services (ICWS'04),(5):17~32. [7] Java程序设计管理范例: 成绩评定 成绩 教师签字 目录
1设计时间 ............................... 错误!未定义书签。 2设计目的 ............................... 错误!未定义书签。 3设计任务 ............................... 错误!未定义书签。 4设计内容 ............................... 错误!未定义书签。 需求分析 .............................. 错误!未定义书签。 总体设计 .............................. 错误!未定义书签。 简单计算器 ......................... 错误!未定义书签。 手机信息管理系统 ................... 错误!未定义书签。 详细设计 .............................. 错误!未定义书签。 简单计算器设计 ..................... 错误!未定义书签。 手机信息管理系 ..................... 错误!未定义书签。 测试与分析 ............................ 错误!未定义书签。 测试 ............................... 错误!未定义书签。 分析 ............................... 错误!未定义书签。 5 总结与展望 ............................. 错误!未定义书签。 参考文献 ................................. 错误!未定义书签。 成绩评定 ................................. 错误!未定义书签。
软 件 学 院
课程设计报告书
课程名称 面向对象程序设计 设计题目 简单计算器的设计与实现、信息管理模块设计 专业班级 财升本13--2 学 号 27
姓 名 赵鑫宇 指导教师 曲海成
2014 年 1 月
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务