发布时间 : 星期六 文章天津理工大学计算机专业Java实验报告三更新完毕开始阅读a5190c24bcd126fff7050b99
学院(系)名称:计算机与通信工程学院 姓名 班级 实验报告
计算机科学与技术(合作) GUI设计与运用 中加4班 课程名称 实验时间 学号 实验项目 2009 专业 Java程序设计 2011年12月22日 课程代码 实验地点 成绩 0666166 主校区7—219计算机基础实验室 批改意见 教师签字: 一,实验目的 理解GUI中各种接口和方法的使用,学会设计用户界面,思考swing包和awt包的区别于联系 二,实验方式 Way: Debug program and get results Equipment: 1 PC / person Group: 1 person/ group Time: 4 hours 三,实验内容和要求 在图形用户界面中实现数的平方的运算,和计时器的设计方法; 要求设计按钮,当点击按钮后显示结果信息,并且计时器开始及时; 四,实验步骤 原名代码以及结果截图:
- 1 -
import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class Jishiqi {
public static void main(String args[]) {
TimeWin Win=new TimeWin(); } }
class TimeWin extends JFrame implements ActionListener //主窗口 {
int number=1,start=1; char chinaWorld[]; Timer time;
JButton bStart,bStop,bContinue; JTextField inputText,showText; JLabel showWorld,labwest; JDia dia; TimeWin() {
setTitle(\制作的计时器和计算器\);//super()
setBounds(400,300,500,360);//setSize(300,180); setVisible(true);
setResizable(false); //窗口大小不可被更改 JLabel lab1,lab2;
lab1=new JLabel(\数字:\); lab2=new JLabel(\平方:\);
inputText=new JTextField(15); showText=new JTextField(15); JPanel pNorth=new JPanel(); pNorth.add(lab1);
pNorth.add(inputText); pNorth.add(lab2);
pNorth.add(showText); labwest=new JLabel(); Font fnt1;
fnt1=new Font(\微软雅黑\,Font.BOLD,50); labwest.setFont(fnt1); showWorld=new JLabel();
showWorld.setHorizontalAlignment(JLabel.CENTER);
showWorld.setFont(new Font(\微软雅黑\,Font.BOLD,58)); bStart=new JButton(\开始\); time=new Timer(1000,this); bStop=new JButton(\暂停\);
bContinue=new JButton(\继续\);
- 2 -
bStart.addActionListener(this); bStop.addActionListener(this);
bContinue.addActionListener(this); JPanel pSouth=new JPanel(); pSouth.add(bStart); pSouth.add(bStop);
pSouth.add(bContinue);
add(pNorth,new BorderLayout().NORTH); add(pSouth,new BorderLayout().SOUTH);
add(showWorld,new BorderLayout().CENTER); add(labwest,new BorderLayout().WEST);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chinaWorld=new char[100];
for(int k=0,i='中';k dia=new JDia(this,\警告!\,true);//输入错误时弹出对话框 } public void actionPerformed(ActionEvent e) { if(e.getSource()==time) { String t=Integer.toString(number+1); labwest.setText(t); showWorld.setText(\+chinaWorld[number]); number++; if(number==chinaWorld.length-1) number=0; } else if(e.getSource()==bStart) { try { String s=inputText.getText(); int a=Integer.parseInt(s); a=a*a; String b=Integer.toString(a); showText.setText(b); } catch(NumberFormatException nfe) { dia.setVisible(true); inputText.setText(null); showText.setText(null); } time.start(); - 3 - } else if(e.getSource()==bStop) time.stop(); else if(e.getSource()==bContinue) time.restart(); } class JDia extends JDialog//类中嵌套类,,设计弹出对话框界面 { JButton bt1; JDia(Frame frm,String str,boolean b1) { super(frm,str,b1); setSize(200,150); setResizable(false); setVisible(false); setLocation(550,430); setLayout(new FlowLayout()); JLabel lb1=new JLabel(\ 请输入数字! JLabel lb2=new JLabel(\); add(lb2); add(lb1); bt1=new JButton(\确定\); add(bt1); bt1.addActionListener(new act()); } class act implements ActionListener //类中类实现接口 { public void actionPerformed(ActionEvent e) { if(e.getSource()==bt1) setVisible(false); } } } } 程序执行结果: - 4 - ); \