




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验八 图形用户界面与对话框1. 实验目的1、学会处理ActionEvent事件2、学会使用布局类3、学习焦点、鼠标和键盘事件4、学习使用输入和消息对话框2. 实验内容1、根据附录里的源代码,按照注释要求,完成代码填空,使程序能够运行得出结果。1) 实验1算术测试2) 实验2布局与日历3) 实验3华容道4) 实验4字体对话框5) 实验5计算平方根6) 实验6简易计算器2、设计编写程序完成以下任务。 1)修改实验1的代码,再增加“小学生”级别,并增加测试乘、除法的功能。2)编写一个应用程序,用户可以在一个文本框里输入数字字符,按Enter键后将数字放入一个文本区。当输入的数字大于1000时,弹出一个有模式的对话框,提示用户数字已经大于1000,是否继续将该数字放入文本区。3)编写应用程序,有一个标题为“移动”的窗口,窗口布局为null,在窗口中有两个按钮,单击一个按钮让另一个按钮移动。4)仿照操作系统中的简易计算机,自行设计一个能进行加减乘除运算的计算器。需要考虑先进性乘除运算再进行加减运算。3. 实验步骤略4. 评分标准1. A内容功能完善,编程风格好,人机接口界面好; 2. B内容功能完善,编程风格良好,人机接口界面良好;3. C完成必做内容;4. D能完成必做内容;5. E未按时完成必做内容,或者抄袭(雷同者全部为E).参照书上实验按模版要求,将【代码】替换为Java程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。(压缩包的文件名为:学号后三位和名字开头字母,如109zhhRAR|ZIP)附录:实验1 算术测试模板代码Teacher.javaimport java.util.Random;import java.awt.event.*;import javax.swing.*;public class Teacher implements ActionListenerint numberOne,numberTwo;String operator= ;boolean isRight;Random random;int maxInteger;JTextField textOne,textTwo,textResult;JLabel operatorLabel,message;Teacher()random=new Random();public void setMaxInteger(int n)maxInteger=n;public void actionPerformed(ActionEvent e)String str=e.getActionCommand();if(str.equals(getProblem)numberOne=random.nextInt(maxInteger)+1;numberTwo=random.nextInt(maxInteger)+1;double d=Math.random();if(d=0.5)operator=+;elseoperator=-;textOne.setText(+numberOne);textTwo.setText(+numberTwo);operatorLabel.setText(operator);message.setText(请回答);textResult.setText(null);else if(str.equals(answer)String answer=textResult.getText();tryint result=Integer.parseInt(answer);if(operator.equals(+)if(result=numberOne+numberTwo)message.setText(你回答正确);elsemessage.setText(你回答错误);else if(operator.equals(-)if(result=numberOne-numberTwo)message.setText(你回答正确);elsemessage.setText(你回答错误);catch(NumberFormatException ex)message.setText(请输入数字字符);public void setJTextField(JTextField.t)textOne =t0;textTwo =t1;textResult =t2;public void setJLabel(JLabel.label)operatorLabel=label0;message=label1;ComputerFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class ComputerFrame extends JFrameJMenuBar menubar;JMenu choiceGrade;JMenuItem grade1,grade2;JTextField textOne,textTwo,textResult;JButton getProblem,giveAnswer;JLabel operatorLabel,message;Teacher teacherZhang;ComputerFrame()teacherZhang=new Teacher();teacherZhang.setMaxInteger(20);setLayout(new FlowLayout();menubar=new JMenuBar();choiceGrade=new JMenu(选择级别);grade1=new JMenuItem(幼儿级别);grade2=new JMenuItem(儿童级别);grade1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)teacherZhang.setMaxInteger(10););grade2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)teacherZhang.setMaxInteger(50););choiceGrade.add(grade1);choiceGrade.add(grade2);menubar.add(choiceGrade);setJMenuBar(menubar);/【代码1】 /创建textOne,其可见字符长是5textTwo=new JTextField(5);textResult=new JTextField(5);operatorLabel=new JLabel(+);operatorLabel.setFont(new Font(Arial,Font.BOLD,20);message=new JLabel(你还没有回答呢);getProblem=new JButton(获取题目);giveAnswer=new JButton(确认答案); add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new Label(=); add(textResult); add(giveAnswer); add(message); textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false); getProblem.setActionCommand(getProblem); textResult.setActionCommand(answer); giveAnswer.setActionCommand(answer); teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message); /【代码2】/将teacherZhang注册为getProblem的ActionEvent事件监视器 /【代码3】/将teacherZhang注册为giveAnswer的ActionEvent事件监视器 /【代码4】/将teacherZhang注册为textResult的ActionEvent事件监视器 setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE);MainClass.javapublic class MainClass public static void main(String args) ComputerFrame frame;frame=new ComputerFrame();frame.setTitle(算术测试);frame.setBounds(100,100,650,180);实验2 布局与日历模板代码CalendarBean.javaimport java.util.Calendar;public class CalendarBean String day; int year=2008,month=0; public int getYear() return year; public void setYear(int year) this.year = year; public int getMonth() return month; public void setMonth(int month) this.month = month; public String getCalendar() String a=new String42; Calendar 日历=Calendar.getInstance(); 日历.set(year,month-1,1); int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1; int day=0; if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day=31; if(month=4|month=6|month=9|month=11) day=30; if(month=2) if(year%4=0)&(year%100!=0)|(year%400=0) day=29; else day=28; for(int i=星期几,n=1;i星期几+day;i+) ai=String.valueOf(n) ; n+; return a; CalendarFrame.javaimport java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.util.*;public class CalendarFrame extends JFrame implements ActionListener JLabel labelDay=new JLabel42;JButton titleName=new JButton7;String name=日,一,二,三,四,五,六;JButton nextMonth,previousMonth;CalendarBean calendar;JLabel showMessage=new JLabel(,JLabel.CENTER);int year=2011,month=2;public CalendarFrame()JPanel pCenter=new JPanel();/【代码1】/将pCenter的布局设置为7行7列的GridLayout布局.for(int i=0;i7;i+)titleNamei=new JButton(namei);titleNamei.setBorder(new SoftBevelBorder(BevelBorder.RAISED);/【代码2】/pCenter中添加组件titleNameifor(int i=0;i42;i+)labelDayi=new JLabel(,JLabel.CENTER);labelDayi.setBorder(new SoftBevelBorder(BevelBorder.LOWERED);/【代码3】/pCenter中添加组件labelDayicalendar=new CalendarBean();nextMonth=new JButton(下月);previousMonth=new JButton(上月);nextMonth.addActionListener(this);previousMonth.addActionListener(this);JPanel pNoth=new JPanel(),pSouth=new JPanel();pNoth.add(previousMonth);pNoth.add(nextMonth);pSouth.add(showMessage);/【代码4】/将窗口pCenter添加到中央区域/【代码5】/将窗口pNoth添加到北面区域/【代码6】/将窗口pSouth添加到南面区域setYearAndMonth(year,month);setDefaultCloseOperation(DISPOSE_ON_CLOSE);public void setYearAndMonth(int y,int m)calendar.setYear(y);calendar.setMonth(m);String day= calendar.getCalendar();for(int i=0;i12)month=1;calendar.setMonth(month);String day=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);else if(e.getSource()=previousMonth)month=month-1;if(month1)month=12;calendar.setMonth(month);String day=calendar.getCalendar();for(int i=0;i42;i+)labelDayi.setText(dayi);showMessage.setText(日历:+calendar.getYear()+年+calendar.month+月);CalendarMainClass.javapublic class CalendarMainClass public static void main(String args) CalendarFrame frame=new CalendarFrame();frame.setBounds(100,100,360,300);frame.setVisible(true);frame.setYearAndMonth(2013,5);实验3 华容道模板代码MainClassHRR.javapublic class MainClassHRR public static void main(String args) Hua_Rong_Road HRR=new Hua_Rong_Road(); Person.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class Person extends JButton implements FocusListener int number;Color c=new Color(255,245,170);Font font=new Font(宋体,Font.BOLD,12);Person(int number,String s)super(s);setBackground(c);setFont(font);this.number=number;c=getBackground();addFocusListener(this);public void focusGained(FocusEvent e)setBackground(Color.red);public void focusLost(FocusEvent e)setBackground(c);Hua_Rong_Road.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener, ActionListener Person person=new Person10;JButton left,right,above,below; JButton restart=new JButton(重新开始);public Hua_Rong_Road()init();setBounds(100,100,320,360);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);validate();public void init()setLayout(null); add(restart);restart.setBounds(100,320,120,25);restart.addActionListener(this);String name=曹操,关羽,张,刘,周,黄,兵,兵,兵,兵;for(int k=0;kh/2)go(man,below); if(yh/2)go(man,above); if(xw/2)go(man,right);public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e) public void go(Person man,JButton direction)boolean move=true;Rectangle manRect=man.getBounds();int x=man.getBounds().x;int y=man.getBounds().y;if(direction=below)y=y+50;else if(direction=above)y=y-50;else if(direction=left)x=x-50;else if(direction=right)x=x+50;manRect.setLocation(x,y);Rectangle directionRect=direction.getBounds();for(int k=0;k10;k+)Rectangle personRect=personk.getBounds();if(manRersects(personRect)&(man.number!=k)move=false;if(manRersects(directionRect)move=false; if(move=true)man.setLocation(x,y);public void actionPerformed(ActionEvent e)dispose(); new Hua_Rong_Road();实验4 字体对话框模板代码FontFamilyNames.javaimport java.awt.GraphicsEnvironment;public class FontFamilyNames String allFontName; public String getFontName() GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); allFontName=ge.getAvailableFontFamilyNames(); return allFontName; FontDialog.javaimport java.awt.event.*;import java.awt.*;import javax.swing.*;public class FontDialog extends JDialog implements ItemListener,ActionListenerFontFamilyNames fontFamilyNames; int fontSize=38; String fontName; JComboBox fontSizeList,fontNameList; JLabel label; Font font; JButton yes,cancel; static int YES=1,NO=0; int state=-1; FontDialog(Frame f) super(f);setTitle(字体);font=new Font(宋体,Font.PLAIN,12); fontFamilyNames=new FontFamilyNames(); /【代码1】/当前对话框调用setModal(boolean b)设置为有模式 yes=new JButton(Yes); cancel=new JButton(cancel); yes.addActionListener(this); cancel.addActionListener(this); label=new JLabel(hello,奥运,JLabel.CENTER); fontSizeList=new JComboBox(); fontNameList=new JComboBox(); String name=fontFamilyNames.getFontName(); fontNameList.addItem(字体); for(int k=0;kname.length;k+) fontNameList.addItem(namek); fontSizeList.addItem(大小); for(int k=8;k=0)inputComplete=true;catch(NumberFormatException exp)/【代码2】/弹出消息对话框inputComplete=false;double sqrtRoot=Math.sqrt(result);System.out.println(result+平方根:+sqrtRoot);实验6 简易计算器模板代码UserFrm.javaimport java.awt.*;import java.awt.event.*;public class UserFrm extends Frame implements ActionListener private MenuBar jmb = new MenuBar();private MenuItem item = new MenuItem(退 出);public static Font font = new Font(宋体, 1, 16);public UserFrm(String title) throws HeadlessException super(title);/【代码1】/设置该Frame位置与大小,具体值为(100, 100, 250, 200)setVisible(true);Menu menu = new Menu(文 件);menu.add(item);item.setFont(font);Panel panelNorth = new Panel();/【代码2】/把panelNorth加入窗体的北区add(new Caculator(), BorderLayout.CENTER);item.addActionListener(this);jmb.add(menu);jmb.setFont(font);setMenuBar(jmb);/【代码3】/用匿名类的方式设计完成窗体关闭的监听和实现关闭的方法validate();public void actionPerformed(ActionEvent e) Object o = e.getSource();if (o = item)System.exit(1);public static void main(String args) new UserFrm(用户界面);class Caculator extends Panel implements ActionListener, KeyListener private TextField tf = new TextField();private float x = 0;private float y = 0;private int code = 0;private boolean enable;private boolean first;private String str = ;public Caculator() setLayout(new BorderLayout();enable = true;first = true;add(tf, BorderLayout.NORTH);Panel panel = new Panel();/【代码4】/把panel设置为GridLayout布局Button btn = null;/【代码5】/创建btn并把标题设为1;把btn加入panel中;设置该按钮的字体格式为UserFrm.font;设置输入确认监听;设置键盘输入监听/【代码6】/创建btn并把标题设为2;把btn加入panel中;设置该按钮的字体格式为UserFrm.font;设置输入确认监听;设置键盘输入监听/【代码7】/创建btn并把标题设为3;把btn加入panel中;设置该按钮的字体格式为UserFrm.font;设置输入确认监听;设置键盘输入监听/【代码8】/创建btn并把标题设为+;把btn加入panel中;设置该按钮的字体格式为UserFrm.fon
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025浙江温州市国资委公开遴选市属国有企业外部董事专家库人选40人笔试参考题库附带答案详解
- 2025江苏无锡市宜兴市宜能实业有限公司招聘线路设计辅助人员2人笔试参考题库附带答案详解
- 2025广西文化产业集团有限公司春季招聘36人笔试参考题库附带答案详解
- 2025年合肥高新美城物业有限公司招聘30人笔试参考题库附带答案详解
- 2025年中国邮政集团有限公司甘肃省分公司春季校园招聘笔试参考题库附带答案详解
- 卸料安全培训课件
- 2025山东省国际信托股份有限公司社会招聘8人笔试参考题库附带答案详解
- 2025安徽蚌埠市城市投资控股集团有限公司所属公司社会招聘11人笔试参考题库附带答案详解
- 2025四川成都三和来科技有限公司招聘50人笔试参考题库附带答案详解
- 2025内蒙古能源集团智慧运维公司校园招聘(55人)笔试参考题库附带答案详解
- 陪诊服务培训课件模板
- 严禁管制刀具进校园主题班会课件
- 2024年山东省春季高考技能考试汽车专业试题库-上(单选题汇总)
- 国庆、中秋双节前安全排查记录
- 八年级上学期轴对称练习题
- 双姿培训课件
- GB/Z 41082.2-2023轮椅车第2部分:按GB/Z 18029.5测得的尺寸、质量和操作空间的典型值和推荐限制值
- 实施项目经理岗位的工作职责描述
- 中频操作评分标准
- 生活中的理财原理知到章节答案智慧树2023年暨南大学
- GB/T 22588-2008闪光法测量热扩散系数或导热系数
评论
0/150
提交评论