java开发简单记事本
2010-11-21 17:38
183 查看
通过记事本的开发,主要是让像我这样的初学者熟悉java文件流。
这是自己的源代码:
import java.io.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Notepad extends JFrame {
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenu jm2=null;
JMenuItem jm2_jmi1=null;
JMenuItem jm2_jmi2=null;
JMenuItem jm2_jmi3=null;
JMenuItem jm1_jmi1=null;
JMenuItem jm1_jmi2=null;
JMenuItem jm1_jmiAdd=null;
JMenuItem jm1_jmiClear=null;
JMenuItem jm1_jmiQuit=null;
JMenuItem jm1_jmiPrint=null;
//添加一个JScrollPane
JScrollPane jsp=null;
JPanel jp=null;
public Notepad(){
jmb=new JMenuBar();
jm1=new JMenu("文件(F)");
jm1.setMnemonic('F');
jm1_jmi1=new JMenuItem("打开");
jm1_jmi1.setIcon(new ImageIcon("open.gif"));
jm1_jmi1.addMouseListener(new OpenHandle());
jm1_jmi2=new JMenuItem("保存");
jm1_jmi2.addMouseListener(new SaveHandle());
jm1_jmi2.setIcon(new ImageIcon("save.gif"));
jm1_jmiAdd=new JMenuItem("新建",new ImageIcon("create.gif"));
jm1_jmiAdd.addMouseListener(new AddNew());
jm1_jmiClear=new JMenuItem("清空",new ImageIcon("delete.gif"));
jm1_jmiClear.addMouseListener(new ClearAll());
jm1_jmiPrint=new JMenuItem("打印");
jm1_jmiQuit=new JMenuItem("退出");
jm1_jmiQuit.addMouseListener(new QuitForm());
jm1.add(jm1_jmi1);
jm1.add(jm1_jmi2);
jm1.add(jm1_jmiAdd);
jm1.add(jm1_jmiClear);
jm1.add(jm1_jmiPrint);
jm1.add(jm1_jmiQuit);
jmb.add(jm1);
jm2=new JMenu("设置(S)");
jm2.setMnemonic('S');
jm2_jmi1=new JMenuItem("字体");
jm2_jmi1.setIcon(new ImageIcon("fontsize.gif"));
jm2_jmi1.addMouseListener(new FontSetting());
jm2_jmi2=new JMenuItem("背景颜色");
jm2_jmi2.setIcon(new ImageIcon("backcolor.jpg"));
jm2_jmi2.addMouseListener(new BackColorHandle());
jm2_jmi3=new JMenuItem("字体颜色");
jm2_jmi3.setIcon(new ImageIcon("fontcolor.jpg"));
jm2_jmi3.addMouseListener(new FontColorHandle());
jm2.add(jm2_jmi1);
jm2.add(jm2_jmi2);
jm2.add(jm2_jmi3);
jmb.add(jm2);
this.setJMenuBar(jmb);
//将JTextArea添加到JScrollPane中
jta=new JTextArea();
jp=new JPanel(new BorderLayout());
jsp=new JScrollPane(jp);
jp.add(jta);
this.add(jsp,BorderLayout.CENTER);
this.setSize(600, 400);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowClose());
this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-600)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-400)/2);
this.setTitle("NotePad--usharei");
this.setVisible(true);
}
//打开文件
private class OpenHandle extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
JFileChooser jfc=null;
jfc=new JFileChooser();
String str=null;
//显示JAVA源文件
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) { //设定可用的文件的后缀名
if(f.getName().endsWith(".java")||f.isDirectory()){
return true;
}
return false;
}
public String getDescription() {
return "JAVA源程序(*.java)";
}
});
jfc.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
if(f.getName().endsWith(".txt")||f.isDir
4000
ectory()){
return true;
}
return false;
}
public String getDescription(){
return "文本文件(*.txt)";
}
});
if(jfc.showOpenDialog(Notepad.this)!=0){
return ;
}
File f=jfc.getSelectedFile();
setTitle(f.getName()+"--Notepad-usharei");
str=f.getAbsolutePath();
//打开txt文件
if(f.getName().endsWith(".txt")||f.getName().endsWith(".java")){
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(str);
br=new BufferedReader(fr);
String text="",s=null;
while((s=br.readLine())!=null){
text+=s+"/r/n";
}
jta.setText(text);
}catch(Exception ee){ee.printStackTrace();}finally{try {
fr.close();br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
}
//打开二进制文件
else{
FileInputStream fis=null;
try{
fis=new FileInputStream(str);
byte buf[]=new byte[1024];
int n=0;
String ss="";
while((n=fis.read(buf))!=-1){
ss+=new String(buf,0,n);
}
jta.setText(ss.trim());
}catch(Exception ee){ee.printStackTrace();}finally{try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}}
}
//System.out.println(str);
}
}
//关闭文件
private class SaveHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
JFileChooser jfc=null;
jfc=new JFileChooser();
int i=jfc.showSaveDialog(Notepad.this);
if(i!=0){
return;
}
File f=jfc.getSelectedFile();
String str=jfc.getSelectedFile().getAbsolutePath();
setTitle(f.getName()+"--Notpad-usharei");
FileOutputStream fos=null;
byte buf[]=jta.getText().getBytes();
try{
fos=new FileOutputStream(str);
fos.write(buf);
}catch(Exception ee){ee.printStackTrace();}finally{try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
}
}
//字体的设置
private class FontSetting extends MouseAdapter{
public void mousePressed(MouseEvent e) {
//JFontChooser jfc=new JFontChooser();
//FontChooserDialog fcd=new FontChooserDialog();
//Font f=fcd.showDialog(Notepad.this, true);
Font ff=jta.getFont();
Font f=FontChooserDialog.showDialog(Notepad.this, true,ff);
//Font f=fcd.getFont();
jta.setFont(f);
//System.out.println(f.getName()+f.getSize());
}
}
//背景颜色的处理
private class BackColorHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getBackground());
jta.setBackground(c);
}
}
//字体颜色设置
private class FontColorHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getForeground());
jta.setForeground(c);
}
}
//打开新窗体
private class AddNew extends MouseAdapter{
public void mousePressed(MouseEvent e) {
new Notepad();
}
}
//清空所有文本内容
private class ClearAll extends MouseAdapter{
public void mousePressed(MouseEvent e) {
jta.setText("");
}
}
//退出处理
private class QuitForm extends MouseAdapter{
public void mousePressed(MouseEvent e) {
System.exit(0);
}
}
//窗体退出
private class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Notepad();
}
}
这是自己的源代码:
import java.io.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Notepad extends JFrame {
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenu jm2=null;
JMenuItem jm2_jmi1=null;
JMenuItem jm2_jmi2=null;
JMenuItem jm2_jmi3=null;
JMenuItem jm1_jmi1=null;
JMenuItem jm1_jmi2=null;
JMenuItem jm1_jmiAdd=null;
JMenuItem jm1_jmiClear=null;
JMenuItem jm1_jmiQuit=null;
JMenuItem jm1_jmiPrint=null;
//添加一个JScrollPane
JScrollPane jsp=null;
JPanel jp=null;
public Notepad(){
jmb=new JMenuBar();
jm1=new JMenu("文件(F)");
jm1.setMnemonic('F');
jm1_jmi1=new JMenuItem("打开");
jm1_jmi1.setIcon(new ImageIcon("open.gif"));
jm1_jmi1.addMouseListener(new OpenHandle());
jm1_jmi2=new JMenuItem("保存");
jm1_jmi2.addMouseListener(new SaveHandle());
jm1_jmi2.setIcon(new ImageIcon("save.gif"));
jm1_jmiAdd=new JMenuItem("新建",new ImageIcon("create.gif"));
jm1_jmiAdd.addMouseListener(new AddNew());
jm1_jmiClear=new JMenuItem("清空",new ImageIcon("delete.gif"));
jm1_jmiClear.addMouseListener(new ClearAll());
jm1_jmiPrint=new JMenuItem("打印");
jm1_jmiQuit=new JMenuItem("退出");
jm1_jmiQuit.addMouseListener(new QuitForm());
jm1.add(jm1_jmi1);
jm1.add(jm1_jmi2);
jm1.add(jm1_jmiAdd);
jm1.add(jm1_jmiClear);
jm1.add(jm1_jmiPrint);
jm1.add(jm1_jmiQuit);
jmb.add(jm1);
jm2=new JMenu("设置(S)");
jm2.setMnemonic('S');
jm2_jmi1=new JMenuItem("字体");
jm2_jmi1.setIcon(new ImageIcon("fontsize.gif"));
jm2_jmi1.addMouseListener(new FontSetting());
jm2_jmi2=new JMenuItem("背景颜色");
jm2_jmi2.setIcon(new ImageIcon("backcolor.jpg"));
jm2_jmi2.addMouseListener(new BackColorHandle());
jm2_jmi3=new JMenuItem("字体颜色");
jm2_jmi3.setIcon(new ImageIcon("fontcolor.jpg"));
jm2_jmi3.addMouseListener(new FontColorHandle());
jm2.add(jm2_jmi1);
jm2.add(jm2_jmi2);
jm2.add(jm2_jmi3);
jmb.add(jm2);
this.setJMenuBar(jmb);
//将JTextArea添加到JScrollPane中
jta=new JTextArea();
jp=new JPanel(new BorderLayout());
jsp=new JScrollPane(jp);
jp.add(jta);
this.add(jsp,BorderLayout.CENTER);
this.setSize(600, 400);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowClose());
this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-600)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-400)/2);
this.setTitle("NotePad--usharei");
this.setVisible(true);
}
//打开文件
private class OpenHandle extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
JFileChooser jfc=null;
jfc=new JFileChooser();
String str=null;
//显示JAVA源文件
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) { //设定可用的文件的后缀名
if(f.getName().endsWith(".java")||f.isDirectory()){
return true;
}
return false;
}
public String getDescription() {
return "JAVA源程序(*.java)";
}
});
jfc.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
if(f.getName().endsWith(".txt")||f.isDir
4000
ectory()){
return true;
}
return false;
}
public String getDescription(){
return "文本文件(*.txt)";
}
});
if(jfc.showOpenDialog(Notepad.this)!=0){
return ;
}
File f=jfc.getSelectedFile();
setTitle(f.getName()+"--Notepad-usharei");
str=f.getAbsolutePath();
//打开txt文件
if(f.getName().endsWith(".txt")||f.getName().endsWith(".java")){
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(str);
br=new BufferedReader(fr);
String text="",s=null;
while((s=br.readLine())!=null){
text+=s+"/r/n";
}
jta.setText(text);
}catch(Exception ee){ee.printStackTrace();}finally{try {
fr.close();br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
}
//打开二进制文件
else{
FileInputStream fis=null;
try{
fis=new FileInputStream(str);
byte buf[]=new byte[1024];
int n=0;
String ss="";
while((n=fis.read(buf))!=-1){
ss+=new String(buf,0,n);
}
jta.setText(ss.trim());
}catch(Exception ee){ee.printStackTrace();}finally{try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}}
}
//System.out.println(str);
}
}
//关闭文件
private class SaveHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
JFileChooser jfc=null;
jfc=new JFileChooser();
int i=jfc.showSaveDialog(Notepad.this);
if(i!=0){
return;
}
File f=jfc.getSelectedFile();
String str=jfc.getSelectedFile().getAbsolutePath();
setTitle(f.getName()+"--Notpad-usharei");
FileOutputStream fos=null;
byte buf[]=jta.getText().getBytes();
try{
fos=new FileOutputStream(str);
fos.write(buf);
}catch(Exception ee){ee.printStackTrace();}finally{try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
}
}
//字体的设置
private class FontSetting extends MouseAdapter{
public void mousePressed(MouseEvent e) {
//JFontChooser jfc=new JFontChooser();
//FontChooserDialog fcd=new FontChooserDialog();
//Font f=fcd.showDialog(Notepad.this, true);
Font ff=jta.getFont();
Font f=FontChooserDialog.showDialog(Notepad.this, true,ff);
//Font f=fcd.getFont();
jta.setFont(f);
//System.out.println(f.getName()+f.getSize());
}
}
//背景颜色的处理
private class BackColorHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getBackground());
jta.setBackground(c);
}
}
//字体颜色设置
private class FontColorHandle extends MouseAdapter{
public void mousePressed(MouseEvent e) {
Color c=JColorChooser.showDialog(Notepad.this, "BackColorSetting", jta.getForeground());
jta.setForeground(c);
}
}
//打开新窗体
private class AddNew extends MouseAdapter{
public void mousePressed(MouseEvent e) {
new Notepad();
}
}
//清空所有文本内容
private class ClearAll extends MouseAdapter{
public void mousePressed(MouseEvent e) {
jta.setText("");
}
}
//退出处理
private class QuitForm extends MouseAdapter{
public void mousePressed(MouseEvent e) {
System.exit(0);
}
}
//窗体退出
private class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Notepad();
}
}
相关文章推荐
- 简单记事本程序java源码项目
- 一个简单的hibernate+mysql的实例java/jsp开发
- Mac OS搭建简单的Java开发环境
- 【java开发系列】—— spring简单入门示例
- java project 项目的开发--简单的http请求, 模仿浏览器发出请求get
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax----数据序列化不可能比这更简单了!
- Ext JS MVC与java 开发简单的CMS后台管理系统
- Java实例开发05-02 简单的HTTP服务器端
- 用Java 开发 WebService Axis简单实例
- JAVA注解在SSH开发中的简单应用
- Java开发下的设计模式简单说明
- Java开发下的设计模式简单说明
- Eclipse插件开发之添加简单的GUI元素-Java基础-Java-编程开发
- java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)
- Java 反射学习(四)反射技术开发框架原理简单实现
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简单介绍
- MyEclipse开发Java WebService 简单实例
- 简单的邮件开发2--Java
- Java程序开发中的简单内存分析
- JavaWEB开发时FCKeditor类似office界面的ajax框架,加入后就能做界面类似office,能进行简单的文本编辑操作+配置手册