《基于AWT及SWINIG的GUI及事件处理实验报告》由会员分享,可在线阅读,更多相关《基于AWT及SWINIG的GUI及事件处理实验报告(7页珍藏版)》请在金锄头文库上搜索。
1、 实 验 报 告实验课程名称: 基于AWT及SWINIG的GUI及事件处理实验 学 生 姓 名: 朱军 班 级 软件122 学 号 12477229 学 院(系): 信息数理学院 指 导 教 师: 杨亚南 成 绩: 实 验 时 间: 2014 年 10 月 17 日 2014 年 10 月 17 日一 实验目的1 熟悉JAVA的图形用户界面的基本组件,以及它们的层次关系和使用方法。2 掌握常见的布局管理设计器,如FlowLayout,GridLayout,BorderLayout。学会如何进行布局设计。3 掌握事件驱动原理,使GUI中的基本组件能够响应用户的操作。二实验原理 在应用程序的开发中
2、,开发出友好的图形用户界面是极为重要的。在Java中有AWT(Abstract Windows Toolkit 抽象窗口工具集)和Swing两类工具集,其中AWT和本地代码有关并不完全独立于平台,而Swing成为轻量级组件完全支持跨平台。但是两者的机制完全一样,例如事件处理机制都采用监听方式;都提供了容器、布局管理器的机制。三实验内容1 编写程序,包括一个标签、文本框和命令按钮。当用户单击按钮时,程序把文本框中的内容复制到当前的位置。2 编写程序,利用JtextField和JpasswordField分别接收用户输入的用户名和密码,并对用户输入的密码进行检验。对于每个用户名有三次密码输入机会。
3、3 编写程序实现一个计算器,包括10个数字(0-9)按钮和4个运算符(加、减、乘、除)按钮,以及等号和清空两个辅助按钮,还有一个显示输入输出的文本框。主要代码:package calculate;import java.util.*;/* * * author _USER_ */public class calculator extends javax.swing.JFrame private float s, t;private int op;/* Creates new form calculator */public calculator() initComponents();priva
4、te void jButton20ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jTextField1.setText();private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 7;jTextField1.setTe
5、xt();private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 6;jTextField1.setText();private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s= new
6、Float(jTextField1.getText().floatValue();op = 5;jTextField1.setText();private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 4;jTextField1.setText();private void jButton15ActionPerformed(java.awt.
7、event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 3;jTextField1.setText();private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 2;jTe
8、xtField1.setText();private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:s = new Float(jTextField1.getText().floatValue();op = 1;jTextField1.setText();private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling cod
9、e here:double result = 0;switch (op) case 1:t= new Float(jTextField1.getText().floatValue();result = s + t;break;case 2:t= new Float(jTextField1.getText().floatValue();result = s - t;break;case 3:t= new Float(jTextField1.getText().floatValue();result = s * t;break;case 4:t= new Float(jTextField1.get
10、Text().floatValue();result = s / t;break;case 5:result = 1 / (s);break;case 6:t= new Float(jTextField1.getText().floatValue();result = (int) s % (int) t;break;case 7:t= new Float(jTextField1.getText().floatValue();result = Math.pow(s, t);jTextField1.setText( + result);private void jButton11ActionPer
11、formed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton9ActionPerformed(java.awt.event.ActionEvent
12、 evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling cod
13、e here:jButton1ActionPerformed(evt);private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) / TODO add your handling code here:jButton1ActionPerformed(evt);private void jButton3ActionPerformed(java.awt.event