• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

JAVA 事例教程(1)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 // ************************************
// 一个简单的JAVA 程序
// 功能是演示一个小窗口,并具有读写文件的功能。
// @author Gaogao
// @date 2004-11-04
// ************************************

// ************************************
// 引入GUI和GUI事件处理的类,和I/O管理的类
// ************************************
import java.awt.*;
import java.awt.event.*;
import java.io.*;

// ************************************
// TestOne,一个演示菜单和按钮的Application
// 程序。
// ************************************
public class TestOne extends Frame implements ActionListener {
//继承自Frame,实现ActionListener接口
//Frame是一个窗体的框架类,Actionlistener是事件接口,实现他
//可以做事件处理。

// ************************************
// 声明变量
// ************************************
// 菜单控制
private MenuBar mb;
private Menu mFile;
private MenuItem miLoad;
private MenuItem miSave;
private MenuItem miExit;
// 按钮
private TextArea textArea;
// 磁盘管理
private File file;
// 文件名
public static final String FILENAME_STR = "Love.TXT";
// ************************************
// 构造
// ************************************
public TestOne() {
init();

putObjectsToMyFrame();
addListenersToObjects();
setTheWindow();
}
// ************************************
// 分配对象内存
// ************************************
private void init() {
mb = new MenuBar();
mFile = new Menu("File");
miLoad = new MenuItem("Load");
miSave = new MenuItem("Save");
miExit = new MenuItem("Exit");

textArea = new TextArea("");
}

// ************************************
// 装各个部件到窗口
// ************************************
private void putObjectsToMyFrame() {
setMenuBar(mb);
mb.add(mFile);
mFile.add(miLoad);
mFile.add(miSave);
mFile.add(miExit);
add(textArea);
}

// ************************************
// 设置窗口
// ************************************
private void setTheWindow() {
this.setSize(400,300);
this.show();
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
// ************************************
// 设置事件监听器
// ************************************
private void addListenersToObjects() {
miLoad.addActionListener(this);//将miLoad这个菜单的事件监听放在这个窗口的实例中
miSave.addActionListener(this);
miExit.addActionListener(this);

}
// ************************************
// 事件监听器(就是这个类的事例)监听到事件后
// 调用。此方法是ActionListener接口中抽象
// 方法的实现。
// ************************************
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == miLoad)
{

//
System.out.println("Loading");
DataInputStream dis;

try {
file = new File(FILENAME_STR);
dis = new DataInputStream(new FileInputStream(file));
textArea.setText("");
////没JAVA DOC 文档 不会写了。待完善。
dis.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}




}
else if (ae.getSource() == miSave)
{
//
System.out.println("Saving");
DataOutputStream dos;
try {
file = new File(FILENAME_STR);
dos = new DataOutputStream(new FileOutputStream(file));
int i = 0;
String temp = textArea.getText();
while (i < temp.length())
{
dos.writeChar(temp.charAt(i++));
}

dos.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}


}
else if (ae.getSource() == miExit)
{
//退出应用程序
System.out.println("Exit");
System.exit(0);
}

}
// ************************************
// 程序入口。
// ************************************
public static void main(String[] args) {
new TestOne();
}
}



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网