[收藏]一个JAVA的简单记事本
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class tj{
String title="错误消息";
int type = JOptionPane.ERROR_MESSAGE;
public tj(){
final JFrame frame = new JFrame("我的文本编辑器");
final JTextArea text = new JTextArea();
//设置框架尺寸
frame.setSize(600,500);
//关闭窗口! WindowAdapter()是窗口事件适配器类 用来监听窗口关闭事件的 当事件发生时 执行windowClosing()方法
//是强制退出的,比如做了个窗口,当人点x按纽时就调用System.exit(0);来退出
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,1));
//在panel上加上滚动条
panel.add(new JScrollPane(text));
frame.getContentPane().add(panel);
//菜单开始
JMenuBar Mbar = new JMenuBar();
frame.setJMenuBar(Mbar);
JMenu jfile = new JMenu("文件");
JMenu jedit = new JMenu("编辑");
JMenu jhelp = new JMenu("帮助");
Mbar.add(jfile);
Mbar.add(jedit);
Mbar.add(jhelp);
//菜单结束
//新建开始
JMenuItem jnew = new JMenuItem("新建");
jnew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.setText(" ");
}});
jnew.setMnemonic('N');
jnew.setAccelerator( KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,true));
//新建结束
//打开开始
JMenuItem jopen = new JMenuItem("打开");
jopen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser openfile=new JFileChooser();
openfile.setDialogTitle("打开文件");
openfile.setApproveButtonText("打开");
openfile.showOpenDialog(frame);
File file=openfile.getSelectedFile();
FileInputStream inputfile=null;
String message="文件未找到";
try{
inputfile=new FileInputStream(file);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(frame,message,title,type);
}
int readbytes;
String message1="读取文件出错误";
try{
while((readbytes=inputfile.read())!=-1)
{
text.append(String.valueOf((char)readbytes));
}
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(frame,message1,title,type);
}
String closemessage="关闭流时出错";
try{
inputfile.close();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(frame,closemessage,title,type);
}
}});
jopen.setMnemonic('O');
jopen.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
//打开结束
//保存开始
JMenuItem jsave = new JMenuItem("保存");
jsave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser savefile=new JFileChooser();
savefile.setApproveButtonText("保存");
savefile.setDialogTitle("保存文件");
savefile.showSaveDialog(frame);
File filesa=savefile.getSelectedFile();
String messagef="文件未找到";
FileOutputStream outputfile=null;
try{
outputfile=new FileOutputStream(filesa);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(frame,messagef,title,type);
}
String filecontent=text.getText();
String wrmessage="写入文件出错";
try
{
outputfile.write(filecontent.getBytes());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(frame,wrmessage,title,type);
}
String cmessage="关闭流出错";
try{
outputfile.close();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(frame,cmessage,title,type);
}
}});
jsave.setMnemonic('S');
jsave.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK,true));
//保存结束
//退出开始
JMenuItem jquite = new JMenuItem("退出");
jquite.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
jquite.setMnemonic('Q');
jquite.setAccelerator(KeyStroke.getKeyStroke('Q',java.awt.Event.CTRL_MASK,true));
//退出结束
//查找开始
JMenuItem jfind = new JMenuItem("查找");
jfind.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}});
jfind.setMnemonic('F');
jfind.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,true));
//查找结束
//剪切开始
JMenuItem jcut = new JMenuItem("剪切");
jcut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.cut();
}});
jcut.setMnemonic('C');
jcut.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,true));
//剪切结束
//复制开始
JMenuItem jcopy = new JMenuItem("复制");
jcopy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.copy();
}});
jcopy.setMnemonic('o');
jcopy.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
//复制结束
//粘贴开始
JMenuItem jpaste = new JMenuItem("粘贴");
jpaste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}});
jpaste.setMnemonic('P');
jpaste.setAccelerator(KeyStroke.getKeyStroke('P',java.awt.Event.CTRL_MASK,true));
//粘贴结束
//邮件开始
JMenuItem jiami = new JMenuItem("邮件");
jiami.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}});
//邮件结束
//关于开始
JMenuItem jabout = new JMenuItem("关于");
jabout.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int type=JOptionPane.INFORMATION_MESSAGE;
String title="关于";
String message="该软件由Faron改编";
JOptionPane.showMessageDialog(frame,message,title,type);
}});
//关于结束
jfile.add(jnew);
jfile.add(jopen);
jfile.add(jsave);
jfile.addSeparator();
jfile.add(jquite);
jedit.add(jcut);
jedit.add(jcopy);
jedit.add(jpaste);
jedit.add(jfind);
jedit.add(jiami);
jhelp.add(jabout);
frame.setVisible(true);
}
public static void main(String[] args) {
tj tj1 = new tj();
}
}
浙公网安备 33010602011771号