}
ASP.Net+Android+IOS开发------期待与您交流
<图形化界面>
<GUI小练习练习>
使用GUI配合IO流实现Windows下的记事本功能:
package cn.Notepad;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Notepad {
public Notepad() {
init();
}
// 屏幕的宽度和高度
private static final int WIDTH = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int HEIGHT = Toolkit.getDefaultToolkit()
.getScreenSize().height;
//定义记事本的窗体
private Frame frm = null;
//菜单栏
private MenuBar mb = null;
//菜单项(文件)
private Menu m = null;
//打开,保存
private MenuItem open = null,save = null;
//显示文本的区域
private TextArea ta = null;
//文件对话框
private FileDialog fd = null;
//建立File对象
private File f = null;
//获取路径和文件的字符串
private String path=null,name=null;
//记录文本区域填充的内容
private String textArea = null;
private void init() {
//对记事本的窗体进行设置
frm = new Frame("模仿记事本");
frm.setBounds((WIDTH-600)/2, (HEIGHT-550)/2,600, 550);
frm.setLayout(null);
frm.setVisible(true);
mb = new MenuBar();
//菜单栏设置到窗体中
frm.setMenuBar(mb);
//初始化文件菜单
m = new Menu("文件");
mb.add(m);
//保存,打开的初始化
save = new MenuItem("保存");
open = new MenuItem("打开");
m.add(open);
m.add(save);
//显示文本的区域
ta = new TextArea();
ta.setBounds(10, 60, 580, 450);
frm.add(ta);
myEvent();
}
private void myEvent(){
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//添加打开菜单的事件
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//对话框组件初始化
fd = new FileDialog(frm,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
path = fd.getDirectory();
System.out.println(path);
name = fd.getFile();
// System.out.println(path+"------"+name);
//判断获取路径和文件是不是空
if(path==null || name == null)
return;
else
f = new File(path, name);
// System.out.println(f);
FileReader fr = null;
BufferedReader bfr = null;
try{
bfr = new BufferedReader(new FileReader(f));
String line = null;
ta.setText("");
while((line = bfr.readLine())!=null){
// System.out.println(line);
ta.append(line+"\r\n");
}
textArea = ta.getText();
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件读取失败");
}finally{
try{
if(bfr!=null)
bfr.close();
}catch(IOException ex){
throw new RuntimeException("文件关闭失败");
}
}
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(f==null){
fd = new FileDialog(frm, "保存文件", FileDialog.SAVE);
fd.setVisible(true);
path = fd.getDirectory();
System.out.println("-------" +path);
name = fd.getFile();
System.out.println("-------" +name);
}
//获取到用户指定的文件和路径
if(path==null || name==null)
return;
// File f = new File(path, name);
FileWriter fr = null;
BufferedWriter bfw = null;
try{
bfw = new BufferedWriter(new FileWriter(f));
String text = ta.getText();
//System.out.println(text);
bfw.write(text);
bfw.flush();
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件写入失败");
}finally{
try{
if(bfw!=null)
bfw.close();
}catch(IOException ex){
throw new RuntimeException("文件写入关闭失败");
}
}
}
});
}
}
ASP.Net+Android+IOS开发------期待与您交流
详细请查看:http://edu.csdn.net