1个回答
展开全部
是个swing的。你用吧
package com.note;
import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class MyNote extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenu menuFile;
JMenu menuEdit ;
JMenu menuAbout;
JPopupMenu popUpMenu;
JTextArea txt=null; //编辑区
JLabel lab=new JLabel("未修改");
JFileChooser fileChooser = new JFileChooser();
File saveFileName = null, fileName = null;
public MyNote(String s)
{
super(s);//标题栏
createMenuBar(); //菜单栏
createContentPane(); //文本编辑区组件
setBounds(200,200,600,450);//窗口大小及位置
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
closeFile();
}
});
setVisible(true);
}
public void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//菜单栏
menuFile = new JMenu("文件");
menuEdit = new JMenu("编辑");
menuAbout= new JMenu("关于");
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuAbout);
//文件菜单选项
JMenuItem itemOpen = new JMenuItem("打开");
menuFile.add(itemOpen);
itemOpen.addActionListener(this);
menuFile.addSeparator();
JMenuItem itemSave = new JMenuItem("保存");
menuFile.add(itemSave);
itemSave.addActionListener(this);
JMenuItem itemSaveas=new JMenuItem("另存为");
menuFile.add(itemSaveas);
itemSaveas.addActionListener(this);
menuFile.addSeparator();
JMenuItem itemQuit = new JMenuItem("关闭");
menuFile.add(itemQuit);
itemQuit.addActionListener(this);
//编辑菜单选项
JMenuItem itemCut = new JMenuItem("剪切");
menuEdit.add(itemCut);
itemCut.addActionListener(this);
JMenuItem itemCopy = new JMenuItem("复制");
menuEdit.add(itemCopy);
itemCopy.addActionListener(this);
JMenuItem itemPaste= new JMenuItem("粘贴");
menuEdit.add(itemPaste);
itemPaste.addActionListener(this);
//关于菜单选项
JMenuItem itemAboutnote = new JMenuItem("关于NotePad");
menuAbout.add(itemAboutnote);
itemAboutnote.addActionListener(this);
//
JMenuItem Cutitem = new JMenuItem("剪切");
menuEdit.add(Cutitem);
Cutitem.addActionListener(this);
JMenuItem Copyitem = new JMenuItem("复制");
menuEdit.add(Copyitem);
Copyitem.addActionListener(this);
JMenuItem Pasteitem= new JMenuItem("粘贴");
menuEdit.add(Pasteitem);
Pasteitem.addActionListener(this);
popUpMenu= new JPopupMenu();
popUpMenu.add(Cutitem);
popUpMenu.add(Copyitem);
popUpMenu.add(Pasteitem);
//快捷键
itemOpen.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));
itemSave.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));
itemQuit.setAccelerator(KeyStroke.getKeyStroke('Q',Event.CTRL_MASK,false));
itemCut.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK,false));
itemCopy.setAccelerator(KeyStroke.getKeyStroke('C',Event.CTRL_MASK,false));
itemPaste.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK,false));
setJMenuBar(menuBar);
}
private boolean isChanged()
{
if(lab.getText().equals("未修改"))
return false;//内容是否修改,返回值
else return true;
}
public void actionPerformed(ActionEvent e)
{
int option=-1;
if(e.getActionCommand().equals("打开"))
option=0;
if(e.getActionCommand().equals("保存"))
option=1;
if(e.getActionCommand().equals("另存为"))
option=2;
if(e.getActionCommand().equals("关闭"))
option=3;
if(e.getActionCommand().equals("剪切"))
option=4;
if(e.getActionCommand().equals("复制"))
option=5;
if(e.getActionCommand().equals("粘贴"))
option=6;
if(e.getActionCommand().equals("关于NotePad"))
option=7;
switch(option)//判断命令名称
{
case 0:{
if(!isChanged())
{
open();
}
else
{
int option2=JOptionPane.showConfirmDialog
(this,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
switch(option2)
{
case JOptionPane.YES_OPTION:
saveFile();//保存文件
break;
case JOptionPane.NO_OPTION:
open();
break;
}
}
break;}
case 1:
saveFile();break;
case 2:
saveFileAs();break;
case 3:
closeFile();break;
case 4:
Cut();break;
case 5:
Copy();break;
case 6:
Paste();break;
case 7:
about();break;
default:
break;
}
}
public void createContentPane() //文本编辑区组件
{
JScrollPane scrollPane; //滑动条
txt = new JTextArea(10,30);
txt.setEditable(true);
txt.setFont(new Font("宋体",Font.PLAIN,18));
txt.setLineWrap(true);
scrollPane = new JScrollPane(txt);
add(scrollPane, BorderLayout.CENTER);
add(lab,BorderLayout.SOUTH);
txt.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e)
{
lab.setText("已修改");//触发KEYEVENT,修改状态栏
}
});
txt.addMouseListener(new MouseAdapter(){//右键判断
public void mouseReleased(MouseEvent e)
{
if(e.getButton()==MouseEvent.BUTTON3)
popUpMenu.show(e.getComponent(),e.getX(),e.getY());
}
});
}
private void open()
{
int option=fileChooser.showOpenDialog(null);
if(option==JFileChooser.APPROVE_OPTION)
{
try
{
//打开所选文件
BufferedReader buf=new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
//设置文件标题
setTitle(fileChooser.getSelectedFile().toString());
//删除前一次文件
txt.setText("");
//设置状态栏
lab.setText("未修改");
//取得系统相依的换行符
String lineSeparator=System.getProperty("line.separator");
//读取文件并附加至文字编辑区
String text;
while((text=buf.readLine())!=null)
{
txt.append(text);//将文件内容添加到文本编辑区
txt.append(lineSeparator);
}
buf.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(
this,e.toString(),"打开文件失败",JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile()
{
//从标题栏取得文件名称
File file=new File(getTitle());
//若指定文件不存在
if(!(file.exists()))
{
saveFileAs();
}
else
{
try
{
//打开指定文件
BufferedWriter buf=new BufferedWriter(new FileWriter(file));
//将文字编辑区的文字写入文件
buf.write(txt.getText());
buf.close();
//设置状态栏为未修改
lab.setText("未修改");
}
catch(IOException e)
{
JOptionPane.showMessageDialog
(this, e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs()
{
//显示对话框
int option3=fileChooser.showSaveDialog(null);
//如果确认选择文件
if(option3==JFileChooser.APPROVE_OPTION)
{
//取得选择的文件
File file=fileChooser.getSelectedFile();
//在标题栏上设置文件名称
setTitle(file.toString());
try
{
//建立文件
file.createNewFile();
//进行文件保护
saveFile();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,e.toString(),"无法建立文件",JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile()
{
//是否已保存文件
if(!isChanged()) dispose();
else
{
int option= JOptionPane.showConfirmDialog
(this,"文件已修改,是否保存?","保存文件?",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
switch(option)
{
case JOptionPane.YES_NO_OPTION:
saveFile();
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void Cut()
{
txt.cut();
lab.setText("已修改");
popUpMenu.setVisible(false);
}
private void Copy()
{
txt.copy();
popUpMenu.setVisible(false);
}
private void Paste()
{
txt.paste();
lab.setText("已修改");
popUpMenu.setVisible(false);
}
private void about()
{
//显示对话框
JOptionPane.showOptionDialog(this,"程序名称\n"
+" JNotePad\n"+
"简介:\n"+" 一个简单的文字编辑器\n",
"关于JNotePad",JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,null,null,null);
}
public static void main(String args[]) {
new MyNote("记事本");
}
}
package com.note;
import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class MyNote extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JMenu menuFile;
JMenu menuEdit ;
JMenu menuAbout;
JPopupMenu popUpMenu;
JTextArea txt=null; //编辑区
JLabel lab=new JLabel("未修改");
JFileChooser fileChooser = new JFileChooser();
File saveFileName = null, fileName = null;
public MyNote(String s)
{
super(s);//标题栏
createMenuBar(); //菜单栏
createContentPane(); //文本编辑区组件
setBounds(200,200,600,450);//窗口大小及位置
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
closeFile();
}
});
setVisible(true);
}
public void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//菜单栏
menuFile = new JMenu("文件");
menuEdit = new JMenu("编辑");
menuAbout= new JMenu("关于");
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuAbout);
//文件菜单选项
JMenuItem itemOpen = new JMenuItem("打开");
menuFile.add(itemOpen);
itemOpen.addActionListener(this);
menuFile.addSeparator();
JMenuItem itemSave = new JMenuItem("保存");
menuFile.add(itemSave);
itemSave.addActionListener(this);
JMenuItem itemSaveas=new JMenuItem("另存为");
menuFile.add(itemSaveas);
itemSaveas.addActionListener(this);
menuFile.addSeparator();
JMenuItem itemQuit = new JMenuItem("关闭");
menuFile.add(itemQuit);
itemQuit.addActionListener(this);
//编辑菜单选项
JMenuItem itemCut = new JMenuItem("剪切");
menuEdit.add(itemCut);
itemCut.addActionListener(this);
JMenuItem itemCopy = new JMenuItem("复制");
menuEdit.add(itemCopy);
itemCopy.addActionListener(this);
JMenuItem itemPaste= new JMenuItem("粘贴");
menuEdit.add(itemPaste);
itemPaste.addActionListener(this);
//关于菜单选项
JMenuItem itemAboutnote = new JMenuItem("关于NotePad");
menuAbout.add(itemAboutnote);
itemAboutnote.addActionListener(this);
//
JMenuItem Cutitem = new JMenuItem("剪切");
menuEdit.add(Cutitem);
Cutitem.addActionListener(this);
JMenuItem Copyitem = new JMenuItem("复制");
menuEdit.add(Copyitem);
Copyitem.addActionListener(this);
JMenuItem Pasteitem= new JMenuItem("粘贴");
menuEdit.add(Pasteitem);
Pasteitem.addActionListener(this);
popUpMenu= new JPopupMenu();
popUpMenu.add(Cutitem);
popUpMenu.add(Copyitem);
popUpMenu.add(Pasteitem);
//快捷键
itemOpen.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));
itemSave.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));
itemQuit.setAccelerator(KeyStroke.getKeyStroke('Q',Event.CTRL_MASK,false));
itemCut.setAccelerator(KeyStroke.getKeyStroke('X',Event.CTRL_MASK,false));
itemCopy.setAccelerator(KeyStroke.getKeyStroke('C',Event.CTRL_MASK,false));
itemPaste.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK,false));
setJMenuBar(menuBar);
}
private boolean isChanged()
{
if(lab.getText().equals("未修改"))
return false;//内容是否修改,返回值
else return true;
}
public void actionPerformed(ActionEvent e)
{
int option=-1;
if(e.getActionCommand().equals("打开"))
option=0;
if(e.getActionCommand().equals("保存"))
option=1;
if(e.getActionCommand().equals("另存为"))
option=2;
if(e.getActionCommand().equals("关闭"))
option=3;
if(e.getActionCommand().equals("剪切"))
option=4;
if(e.getActionCommand().equals("复制"))
option=5;
if(e.getActionCommand().equals("粘贴"))
option=6;
if(e.getActionCommand().equals("关于NotePad"))
option=7;
switch(option)//判断命令名称
{
case 0:{
if(!isChanged())
{
open();
}
else
{
int option2=JOptionPane.showConfirmDialog
(this,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
switch(option2)
{
case JOptionPane.YES_OPTION:
saveFile();//保存文件
break;
case JOptionPane.NO_OPTION:
open();
break;
}
}
break;}
case 1:
saveFile();break;
case 2:
saveFileAs();break;
case 3:
closeFile();break;
case 4:
Cut();break;
case 5:
Copy();break;
case 6:
Paste();break;
case 7:
about();break;
default:
break;
}
}
public void createContentPane() //文本编辑区组件
{
JScrollPane scrollPane; //滑动条
txt = new JTextArea(10,30);
txt.setEditable(true);
txt.setFont(new Font("宋体",Font.PLAIN,18));
txt.setLineWrap(true);
scrollPane = new JScrollPane(txt);
add(scrollPane, BorderLayout.CENTER);
add(lab,BorderLayout.SOUTH);
txt.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e)
{
lab.setText("已修改");//触发KEYEVENT,修改状态栏
}
});
txt.addMouseListener(new MouseAdapter(){//右键判断
public void mouseReleased(MouseEvent e)
{
if(e.getButton()==MouseEvent.BUTTON3)
popUpMenu.show(e.getComponent(),e.getX(),e.getY());
}
});
}
private void open()
{
int option=fileChooser.showOpenDialog(null);
if(option==JFileChooser.APPROVE_OPTION)
{
try
{
//打开所选文件
BufferedReader buf=new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
//设置文件标题
setTitle(fileChooser.getSelectedFile().toString());
//删除前一次文件
txt.setText("");
//设置状态栏
lab.setText("未修改");
//取得系统相依的换行符
String lineSeparator=System.getProperty("line.separator");
//读取文件并附加至文字编辑区
String text;
while((text=buf.readLine())!=null)
{
txt.append(text);//将文件内容添加到文本编辑区
txt.append(lineSeparator);
}
buf.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(
this,e.toString(),"打开文件失败",JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile()
{
//从标题栏取得文件名称
File file=new File(getTitle());
//若指定文件不存在
if(!(file.exists()))
{
saveFileAs();
}
else
{
try
{
//打开指定文件
BufferedWriter buf=new BufferedWriter(new FileWriter(file));
//将文字编辑区的文字写入文件
buf.write(txt.getText());
buf.close();
//设置状态栏为未修改
lab.setText("未修改");
}
catch(IOException e)
{
JOptionPane.showMessageDialog
(this, e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs()
{
//显示对话框
int option3=fileChooser.showSaveDialog(null);
//如果确认选择文件
if(option3==JFileChooser.APPROVE_OPTION)
{
//取得选择的文件
File file=fileChooser.getSelectedFile();
//在标题栏上设置文件名称
setTitle(file.toString());
try
{
//建立文件
file.createNewFile();
//进行文件保护
saveFile();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this,e.toString(),"无法建立文件",JOptionPane.ERROR_MESSAGE);
}
}
}
private void closeFile()
{
//是否已保存文件
if(!isChanged()) dispose();
else
{
int option= JOptionPane.showConfirmDialog
(this,"文件已修改,是否保存?","保存文件?",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);
switch(option)
{
case JOptionPane.YES_NO_OPTION:
saveFile();
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void Cut()
{
txt.cut();
lab.setText("已修改");
popUpMenu.setVisible(false);
}
private void Copy()
{
txt.copy();
popUpMenu.setVisible(false);
}
private void Paste()
{
txt.paste();
lab.setText("已修改");
popUpMenu.setVisible(false);
}
private void about()
{
//显示对话框
JOptionPane.showOptionDialog(this,"程序名称\n"
+" JNotePad\n"+
"简介:\n"+" 一个简单的文字编辑器\n",
"关于JNotePad",JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,null,null,null);
}
public static void main(String args[]) {
new MyNote("记事本");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询