java实验 JAVA Swing 图形用户界面和对话框_第1页
java实验 JAVA Swing 图形用户界面和对话框_第2页
java实验 JAVA Swing 图形用户界面和对话框_第3页
java实验 JAVA Swing 图形用户界面和对话框_第4页
java实验 JAVA Swing 图形用户界面和对话框_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JAVA实验报告课程名称 java实验 实验名称 JAVA Swing 图形用户界面和对话框 学 院_计算机学院_ 专业班级_软件 _姓 名_ _ 学号_ _实验日期:2015年4月30 日一. 实验目的(1) 学习处理ActionEven事件;(2) 学习使用布局类;(3) 学习焦点、鼠标和键盘事件;(4) 学习使用对话框。二. 实验内容()实验题目一:算术测试 ,编写一个算术测试小软件,用来训练小学生的算术能力。1.要点分析: 程序有三个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确。ComputerFrame对象负责为算术题目提供视图,比如用户可以

2、通过ComputerFrame对象提供的GUI界面给出题目的答案;MainClass是软件的主类。2.程序源代码:package 算术测试;public class MainClass public static void main(String args)ComputerFrame frame;frame=new ComputerFrame();frame.setTitle("算术测试");frame.setBounds(100,100,650,180);package 算术测试;import java.awt.*;import java.awt.event.*;impo

3、rt javax.swing.*;public class ComputerFrame extends JFrame JMenuBar menubar;JMenu choiceGrade;JMenuItem grade1,grade2;JTextField textOne,textTwo,textResult;JButton getProblem,giveAnswer;JLabel operatorLabel,message;Teacher teacherZhang;ComputerFrame()teacherZhang=new Teacher();teacherZhang.setMaxInt

4、eger(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);

5、 );grade2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) teacherZhang.setMaxInteger(50); ); choiceGrade.add(grade1); choiceGrade.add(grade2); menubar.add(choiceGrade); setJMenuBar(menubar); textOne=new JTextField(5);/创建文本框,其可见字符长为5 textTwo=new JTextField(5); textRe

6、sult=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(operatorLab

7、el); add(textTwo); add(new JLabel("="); add(textResult); add(giveAnswer); add(message); textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false); getProblem.setActionCommand("getProblem"); textResult.setActionCommand("anwser"); giveAnswer.setAct

8、ionCommand("answer"); teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message); getProblem.addActionListener(teacherZhang); giveAnswer.addActionListener(teacherZhang); textResult.addActionListener(teacherZhang); setVisible(true); validate(); set

9、DefaultCloseOperation(DISPOSE_ON_CLOSE);package 算术测试;import java.util.Random;import java.awt.event.*;import javax.swing.*;public class Teacher implements ActionListenerint numberOne,numberTwo;String operator=""boolean isRigth;Random random;int maxInteger;JTextField textOne,textTwo,textResu

10、lt;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;/1最大整数之间的随机数 numberTwo=random.nextInt(

11、maxInteger)+1; double d=Math.random();/获取01之间的随机数 if(d>=0.5) operator="+" else operator="-" textOne.setText(""+numberOne); textTwo.setText(""+numberTwo); operatorLabel.setText(operator); message.setText("请回答"); textResult.setText(null); else if(st

12、r.equals("answer") String answer=textResult.getText(); try int result=Integer.parseInt(answer); if(operator.equals("+") if(result=numberOne+numberTwo) message.setText("你回答正确"); else message.setText("你回答错误"); else if(operator.equals("-") if(result=num

13、berOne-numberTwo) message.setText("你回答正确"); else message.setText("你回答错误"); catch(NumberFormatException ex) message.setText("请输入数字字符"); public void setJTextField(JTextField. t) textOne=t0; textTwo=t1; textResult=t2; public void setJLabel(JLabel.label) operatorLabel=label

14、0; message=label1; 3.实验结果:4.实验后练习:(1)模仿本实验的代码,再增加小学生级别。(2)给上述程序增加测试乘法的功能。(二)实验题目二: 布局与日历。1.要点分析: 设置一个窗口,该窗口的布局为BorderLayout布局。窗口的中心添加一个JPanel容器pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter中放置49个标签,用来显示日历。窗口的北面添加一个JPanel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth,单击nextMonth按钮,可以显示当

15、前月的下一月日历;单击previousMonth按钮,可以显示当前月的上一月的日历。2.程序源代码:package 布局与日历;public class CalendarMainClass public static void main(String args) CalendarFrame frame=new CalendarFrame(); frame.setBounds(100,100,360,300); frame.setVisible(true); frame.setYearAndMonth(2015,5); package 布局与日历;import java.util.*;impor

16、t java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class CalendarFrame extends JFrame implements ActionListener/* * */private static final long serialVersionUID = -4568452730166791416L;JLabel labelDay=new JLabel42;JButton titleName=new JButton7;String name=&

17、quot;日","一","二","三","四","五","六"JButton nextMonth,previousMonth;CalendarBean calendar;JLabel showMessage=new JLabel("",JLabel.CENTER);int year=2011,month=2;public CalendarFrame() / TODO 自动生成的构造函数存根JPanel pCenter=new JPanel();p

18、Center.setLayout(new GridLayout(7,7);for(int i=0;i<7;i+)titleNamei=new JButton(namei);titleNamei.setBorder(new SoftBevelBorder(BevelBorder.RAISED);pCenter.add(titleNamei);for(int i=0;i<42;i+)labelDayi=new JLabel("",JLabel.CENTER);labelDayi.setBorder(new SoftBevelBorder(BevelBorder.LO

19、WERED);pCenter.add(labelDayi);calendar=new CalendarBean();nextMonth=new JButton("下月");previousMonth=new JButton("上月");nextMonth.addActionListener(this);previousMonth.addActionListener(this);JPanel pNorth=new JPanel(),pSouth=new JPanel();pNorth.add(previousMonth);pNorth.add(nextMo

20、nth);pSouth.add(showMessage);add(pCenter, BorderLayout.CENTER);add(pNorth,BorderLayout.NORTH);add(pSouth,BorderLayout.SOUTH);setYearAndMonth(year,month);setDefaultCloseOperation(DISPOSE_ON_CLOSE);public void setYearAndMonth(int year2, int month2) / TODO 自动生成的方法存根calendar.setyear(year2);calendar.setM

21、onth(month2);String day=calendar.getCalender();for(int i=0;i<42;i+)labelDayi.setText(dayi);showMessage.setText("日历"+calendar.getYear()+"年"+calendar.getMonth()+"月");Overridepublic void actionPerformed(ActionEvent e) / TODO 自动生成的方法存根if(e.getSource()=nextMonth)month=mon

22、th+1;if(month>12)month=1;calendar.setMonth(month);String day=calendar.getCalender();for(int i=0;i<42;i+)labelDayi.setText(dayi);else if(e.getSource()=previousMonth)month=month-1;if(month<1)month=12;calendar.setMonth(month);String day=calendar.getCalender();for(int i=0;i<42;i+)labelDayi.s

23、etText(dayi);showMessage.setText("日历"+calendar.getYear()+"年"+calendar.getMonth()+"月");package 布局与日历;import java.util.Calendar;public class CalendarBean String day; int year=2005,month=0;public void setyear(int year) / TODO 自动生成的方法存根this.year=year;public int getYear() /

24、TODO 自动生成的方法存根return year;public void setMonth(int month) / TODO 自动生成的方法存根this.month=month;public int getMonth() / TODO 自动生成的方法存根return month;public String getCalender() / TODO 自动生成的方法存根String a=new String42;Calendar 日历=Calendar.getInstance();日历.set(year,month-1,1);int 星期几=日历.get(Calendar.DAY_OF_WEE

25、K)-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|month=10)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;3.实验结果4.课后练习:在Calende

26、rFrame类中增加一个JTextField文本框,用户可以通过在文本框中输入年份来修改calender对象的int成员。(三)实验题目三: 华容道1.要点分析:编写GUI程序,用户通过键盘和鼠标时间来实现曹操、关羽等人物的移动。2.程序源代码:package 华容道;public class MainClass public static void main(String args)new Hua_Rong_Dao();package 华容道;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class

27、 Person extends JButton implements FocusListenerint 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(F

28、ocusEvent e) / TODO Auto-generated method stubsetBackground(Color.red);public void focusLost(FocusEvent e) / TODO Auto-generated method stubsetBackground(c);package 华容道;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Hua_Rong_Dao extends JFrame implements MouseListener,Ke

29、yListener,ActionListener Person person=new Person10;JButton left,rigth,above,below;JButton restart=new JButton("重新开始");public Hua_Rong_Dao()init();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100,100,320,500);setVisible(true);validate();public void init()setLayout(null);add(

30、restart);restart.setBounds(100,320,120,35);restart.addActionListener(this);String name="曹操","关羽","张","刘","周","黄","兵","兵","兵","兵",;for(int k=0;k<name.length;k+)personk=new Person(k,namek);personk.ad

31、dMouseListener(this);personk.addKeyListener(this);add(personk);person0.setBounds(104,54,100,100);person1.setBounds(104,154,100,50);person2.setBounds(54,154,50,100);person3.setBounds(204,154,50,100);person4.setBounds(54,54,50,100);person5.setBounds(204,254,50,100);person6.setBounds(54,254,50,50);pers

32、on7.setBounds(204,254,50,50);person8.setBounds(104,204,50,50);person9.setBounds(154,204,50,50);person10.requestFocus();left=new JButton();rigth=new JButton();above=new JButton();below=new JButton();add(left);add(rigth);add(above);add(below);left.setBounds(49,49,5,260);rigth.setBounds(254,49,5,260);a

33、bove.setBounds(49,49,210,5);below.setBounds(49,304,210,5);validate();public void keyTyped(KeyEvent e) public void keyReleased(KeyEvent e) public void keyPressed(KeyEvent e) Person man=(Person)e.getSource();if(e.getKeyCode()=KeyEvent.VK_DOWN)go(man,below);if(e.getKeyCode()=KeyEvent.VK_UP)go(man,above

34、);if(e.getKeyCode()=KeyEvent.VK_LEFT)go(man,left);if(e.getKeyCode()=KeyEvent.VK_RIGHT)go(man,rigth);public void mousePressed(MouseEvent e) Person man=(Person)e.getSource();int x=-1,y=-1;x=e.getX();y=e.getY();int w=man.getBounds().width;int h=man.getBounds().height;if(y>h/2)go(man,below);if(y<h

35、/2)go(man,above);if(x<w/2)go(man,left);if(x>w/2)go(man,rigth);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 manR

36、ect=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=rigth)x=x+50;manRect.setLocation(x,y);Rectangle directionRect=direction.getBounds();for(int k=0;k<10;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_Dao();3.实验结果4.课后练习:一个按钮button调用

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论