JAVA综合图形界面程序设计.doc_第1页
JAVA综合图形界面程序设计.doc_第2页
JAVA综合图形界面程序设计.doc_第3页
JAVA综合图形界面程序设计.doc_第4页
JAVA综合图形界面程序设计.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

精品文档实验报告(2013 / 2014 学年 第 二 学期)课 程 名 称Java语言程序设计实 验 名 称综合图形界面程序设计实 验 时 间2014年5月5日指 导 单 位计算机学院 软件教学中心指 导 教 师薛景学 生 姓 名 周媛班 级 学 号12001108学 院(系)计算机学院专 业计算机科学与技术(信息安全)1欢迎下载。实验名称综合图形界面程序设计指导教师薛景实验类型上机实验学时2实验时间2014-5-5一、实验目的1.学习使用Java Swing设计GUI界面2.学习Java的事件监听机制的基本原理3.学习监听器处理Java中的事件二、实验环境1.每位同学配备实验计算机一台2.安装JDK和Eclipse三、实验内容1、编写一个算数测试小软件,用来训练小学生的算数能力。程序有3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确;ComputerFrame对象负责为算数题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MainClass是软件的主类。(请在下方空白处填写本程序的全部程序代码及软件界面截图)public class Mainclass public static void main(String args) ComputerFrame frame; frame=new ComputerFrame(); frame.setTitle(算术测试); frame.setBounds(100,100,650,180); import java.awt.*;import java.awt.event.*;import 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.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); textOne=new JTextField(5);/创建textOne,其可见字符长是5 textTwo=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 JLabel(=); 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); getProblem.addActionListener(teacherZhang); giveAnswer.addActionListener(teacherZhang); textResult.addActionListener(teacherZhang); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); import java.util.Random;import java.awt.event.*;import javax.swing.*;public class Teacher implements ActionListener int 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=+; else operator=-; textOne.setText(+numberOne); textTwo.setText(+numberTwo); operatorLabel.setText(operator); message.setText(请回答); textResult.setText(null); else if(str.equals(answer) String answer=textResul.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=numberOne-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=label0; message=label1; 2、编写一个简单的计算器软件,实现简单的四则运算。(请在下方空白处填写本程序的全部程序代码及软件界面截图)import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame private Container container; private GridBagLayout layout; private GridBagConstraints constraints; private JTextField displayField;/ 计算结果显示区 private String lastCommand; private double result;/ 保存计算结果 private boolean start;/ 判断是否为数字的开始 public Calculator() super(Calculator); container = getContentPane(); layout = new GridBagLayout(); container.setLayout(layout); constraints = new GridBagConstraints(); start = true; result = 0; lastCommand = =; displayField = new JTextField(20); displayField.setHorizontalAlignment(JTextField.RIGHT); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 4; constraints.gridheight = 1; constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 100; constraints.weighty = 100; layout.setConstraints(displayField, constraints); container.add(displayField); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); addButton(7, 0, 2, 1, 1, insert); addButton(8, 1, 2, 1, 1, insert); addButton(9, 2, 2, 1, 1, insert); addButton(/, 3, 2, 1, 1, command); addButton(4, 0, 3, 1, 1, insert); addButton(5, 1, 3, 1, 1, insert); addButton(6, 2, 3, 1, 1, insert); addButton(*, 3, 3, 1, 1, command); addButton(1, 0, 4, 1, 1, insert); addButton(2, 1, 4, 1, 1, insert); addButton(3, 2, 4, 1, 1, insert); addButton(-, 3, 4, 1, 1, command); addButton(0, 0, 5, 1, 1, insert); addButton(., 2, 5, 1, 1, insert); addButton(+, 3, 5, 1, 1, command); addButton(=, 0, 6, 4, 1, command); this.setResizable(false); setSize(180, 200); setVisible(true); private void addButton(String label, int row, int column, int with, int height, ActionListener listener) JButton button = new JButton(label); constraints.gridx = row; constraints.gridy = column; constraints.gridwidth = with; constraints.gridheight = height; constraints.fill = GridBagConstraints.BOTH; button.addActionListener(listener); layout.setConstraints(button, constraints); container.add(button); private class InsertAction implements ActionListener public void actionPerformed(ActionEvent event) String input = event.getActionCommand(); if (start) displayField.setText(); start = false; if (input.equals(+/-) displayField.setText(displayField.getText() + -); if (!input.equals(+/-) if (input.equals(Backspace) String str = displayField.getText(); if (str.length() 0) displayField .setText(str.substring(0, str.length() - 1); else if (input.equals(CE) | input.equals(C) displayField.setText(0); start = true; else displayField.setText(displayField.getText() + input); private class CommandAction implements ActionListener public void actionPerformed(ActionEvent evt) String command = evt.getActionCommand(); if (start) lastCommand = command; else calculate(Double.parseDouble(displayField.getText(); lastCommand = command; start = true; public void calculate(double x) if (lastCommand.equals(+) result += x; else if (lastCommand.equals(-) result -= x; else if (lastCommand.equals(*) result *= x; else if (lastCommand.equals(/) result /= x; else if (lastCommand.equals(=) result = x; displayField.setText( + result); public static void main(String args) Calculator calculator = new Calculator(); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 3、使用Java Swing中的各个组件,编写一个自定义的软件界面,例如:学生成绩管理系统、图书馆管理系统、人事管理系统等等(请在下方空白处填写本程序的全部程序代码及软件界面截图)import javax.swing.*;public class Program public static void main(String args) WindowBoxLayout win= new WindowBoxLayout(); win.setBounds(

温馨提示

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

评论

0/150

提交评论