记事本

原创 2005年05月31日 21:41:00
// java版记事本
// 实现基本的功能
// 文件,编辑,帮助

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Jishiben extends JFrame implements ActionListener{

private ObjectOutputStream output;
private ObjectInputStream input;
private String fileBody="";
boolean isCreate = false;
boolean isOpen = false;
boolean openFileSeccess = false;
boolean createFileSeccess = false;


private JMenuBar menubar;
private JMenu wenjian;
private JMenu bianji;
private JMenu bangzhu;

///////////////////////////////
private JMenuItem xinjian;
private JMenuItem dakai;
private JMenuItem baocun;
private JMenuItem tuichu;
///////////////////////////////
private JMenuItem fuzhi;
private JMenuItem zhantie;
private JMenuItem jianqie;
private JMenuItem chazhao;
private JMenuItem quanxuan;
private JMenuItem riqi;
//////////////////////////////
private JMenuItem guanyu;
private JMenuItem help;

private JTextArea area;

private JPanel mainPanel;
private JPanel areaPanel;



public Jishiben()
{
super("-记事本");

menubar = new JMenuBar();
menubar.add(wenjian = new JMenu("文件(F)"));
menubar.add(bianji = new JMenu ("编辑(E)"));
menubar.add(bangzhu = new JMenu("帮助(H)"));

wenjian.add(xinjian = new JMenuItem("新建(N)"));
wenjian.add(dakai = new JMenuItem("打开(O)"));
wenjian.add(baocun = new JMenuItem("保存(S)"));
wenjian.add(tuichu = new JMenuItem("退出(X)"));

bianji.add(fuzhi = new JMenuItem("复制"));
bianji.add(jianqie = new JMenuItem("剪切"));
bianji.add(zhantie = new JMenuItem("粘贴"));

bianji.add(chazhao = new JMenuItem("查找"));
bianji.add(quanxuan = new JMenuItem("全选"));
bianji.add(riqi = new JMenuItem("日期"));

bangzhu.add(guanyu = new JMenuItem("关于"));
bangzhu.add(help = new JMenuItem("帮助"));

wenjian.setFont(new Font("Dialog",0,12));
bianji.setFont(new Font("Dialog",0,12));
bangzhu.setFont(new Font("Dialog",0,12));
xinjian.setFont(new Font("Dialog",0,12));
dakai.setFont(new Font("Dialog",0,12));
baocun.setFont(new Font("Dialog",0,12));
tuichu.setFont(new Font("Dialog",0,12));
fuzhi.setFont(new Font("Dialog",0,12));
jianqie.setFont(new Font("Dialog",0,12));
zhantie.setFont(new Font("Dialog",0,12));
chazhao.setFont(new Font("Dialog",0,12));
quanxuan.setFont(new Font("Dialog",0,12));
riqi.setFont(new Font("Dialog",0,12));
guanyu.setFont(new Font("Dialog",0,12));
help.setFont(new Font("Dialog",0,12));

wenjian.setMnemonic(´f´);
dakai.setMnemonic(´o´);
bianji.setMnemonic(´e´);
bangzhu.setMnemonic(´h´);
xinjian.setMnemonic(´n´);
baocun.setMnemonic(´s´);
tuichu.setMnemonic(´x´);


xinjian.addActionListener(this);
dakai.addActionListener(this);
baocun.addActionListener(this);
tuichu.addActionListener(this);

fuzhi.addActionListener(this);
zhantie.addActionListener(this);
chazhao.addActionListener(this);
quanxuan.addActionListener(this);
riqi.addActionListener(this);

guanyu.addActionListener(this);
help.addActionListener(this);

area = new JTextArea();

Container container = getContentPane();
mainPanel = new JPanel();
JScrollPane areaPanel=new JScrollPane(area);
//areaPanel.setHorizontalScrollBarPolicy(2);
area.setLineWrap(true);
areaPanel.setWheelScrollingEnabled(true);

//setWrapEnable(false);//

mainPanel.setLayout(new BorderLayout());

container.add("Center",mainPanel);

mainPanel.add("North",menubar);
mainPanel.add("Center",areaPanel);

setSize(800,600);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
setVisible(true);



show();

}


public void actionPerformed( ActionEvent e)
{
if(e.getSource()==xinjian)
xinjian();

if(e.getSource()==dakai)
dakai();

if(e.getSource()==baocun)
baocun();

if(e.getSource()==tuichu)
tuichu();

if(e.getSource()==guanyu)
guanyu();

if(e.getSource()==help)
help();

if(e.getSource()==fuzhi)
area.copy();

if(e.getSource()==jianqie)
area.cut();

if(e.getSource()==zhantie)
area.paste();

}

private void xinjian()
{
////////////// create new file


createFile();
if(createFileSeccess == true)
isCreate = true;




}

/////////////////////////////////////
//打开文件 显示数据
///////////////////////////////////
private void dakai()
{
openFile();
if(openFileSeccess == true)
{
isOpen = true;

try{

fileBody = (String)input.readObject();
area.setText(fileBody);
}

catch(ClassNotFoundException cnfex)
{
JOptionPane.showMessageDialog(this,"Unable to create object","Class not Found",
JOptionPane.ERROR_MESSAGE);
}

catch(IOException io)
{
JOptionPane.showMessageDialog(this,"Error during read from file","Read Error",
JOptionPane.ERROR_MESSAGE);
}

}


}

///////////////????????????
///baocun
/////////////////////////////////////////////////////////////////////

private void baocun() //
{

//
if(isCreate==true)
{
try{
fileBody = area.getText(); //

output.writeObject(fileBody); //
output.flush();

isCreate=false;
isOpen = false; //

}

catch(IOException io) //
{
closeFile(); //
//
}

}

if(isOpen==true)
{
createFile();
if(createFileSeccess==true)
{
try{
fileBody = area.getText(); //

output.writeObject(fileBody); //
output.flush();


//

}

catch(IOException io) //
{
closeFile(); //
//
}

}

}


if(isCreate!=true&&isOpen!=true)
{ //

createFile();
if(createFileSeccess==true)
{
isCreate = true; //
try{
fileBody = area.getText();

output.writeObject(fileBody); //
output.flush();

}
//
catch(IOException io)
{
closeFile();
}
//




}//end if
//
}



}
//end baocun ///////////////////////////////////////////////////////


private void tuichu()
{
System.exit(0);


}



private void closeFile()
{
try{
output.close();
System.exit(0);
}
catch(IOException ex){
JOptionPane.showMessageDialog(this,
"Error closing file",
"Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

}

/////////////////
//open a file
private void openFile()
{
JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );


if ( result == JFileChooser.CANCEL_OPTION )
return;

File fileName = fileChooser.getSelectedFile();

if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// Open the file
try {
input = new ObjectInputStream(
new FileInputStream( fileName ) );
openFileSeccess = true;////
dakai.setEnabled( true );

}
catch ( IOException e ) {
JOptionPane.showMessageDialog( this,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}

}

////////
//create a file
private void createFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );

int result = fileChooser.showSaveDialog( this );

// clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
return;

File fileName = fileChooser.getSelectedFile();

if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// Open the file
try {
output = new ObjectOutputStream(
new FileOutputStream( fileName ) );
createFileSeccess = true;
xinjian.setEnabled( true );

}
catch ( IOException e ) {
JOptionPane.showMessageDialog( this,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}

}

////////////////
//帮助
///////////////
private void guanyu()
{
JOptionPane.showMessageDialog(this," java版记事本 "+ "/n" +
" By yellow ","关于记事本",2);
}

private void help()
{
JOptionPane.showMessageDialog(this,"复制(Ctrl+c),粘贴(Ctrl+v), 剪切(Ctrl+x) ",
"help",2);
}
/////////////////////////////////////

public static void main(String args[])
{
Jishiben app = new Jishiben();

app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

);
}

}

相关文章推荐

notepad 记事本的问题

首先这里再明确一下,所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外。版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,...

如何用java写一个简单的记事本。

初步应用swing图形界面,io输入输出,事件监听、响应器,以及类之间的相互关系。 刚刚正式学习java两个月不到,希望和大神们多交流,让彼此不断进步!...

Android课程设计,生活记事本

目录 0  绪论..............................................................................................

Java仿Windows记事本源代码

先上截图: 源代码:import java.awt.*; import javax.swing.*; import javax.swing.filechooser.FileNameExt...

Android_Note(三)——添加、更新、保存记事本功能

1、添加功能的view页面部分

android项目 之 记事本(2)------- 添加照片

继续上节,这次实现在记事本中添加照片的功能。

使用C#写出一个简单的记事本程序

上周老师下课的时候说到,写出来记事本的同学可以期末加分,于是乎,就用两个晚上的时间写出了这个简单的记事本程序。编程语言: C# 编程环境: Visual Studio 2013 运行环境: .NE...

java编写一个简单记事本

package notepad; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent;...

android记事本开发后记

在经历了对这个记事本的多次设计更改之后,我终于完成了一个尚可的记事本项目,从层次的角度来说,很多处代码毫无层次可言,对于后期的维护更改造成了很大的困难。但是即使是这样,我还是需要将这次的感受记录下来,...

用java开发的一个简单的记事本程序

代码如下:package IO; import java.io.*; import java.awt.*; import javax.swing.*;import java.awt.event.*; ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:记事本
举报原因:
原因补充:

(最多只允许输入30个字)