Java图形化界面GUI-02-----黑马程序员

}

 

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值