记事本

// 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);
}
}

);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值