import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class Notebook extends JFrame {
Container contianer;
JFileChooser fc=new JFileChooser();
File file=null;
JTextArea jta;
private MyFindDialog fd = new MyFindDialog(this, jta);
public Notebook()
{
this.setTitle("记事本");
contianer=this.getContentPane();
contianer.setLayout(new BorderLayout());
jta=new JTextArea();
contianer.add(jta);
JMenuBar mb=new JMenuBar();
buildMainMenu(mb);
this.setJMenuBar(mb);
this.setVisible(true);
this.setSize(650, 450);
}
void buildMainMenu(JMenuBar mb) {
JMenu fileMenu=new JMenu("文件(F)");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem newMenuItem=new JMenuItem("新建");
fileMenu.add(newMenuItem);
JMenuItem openMenuItem=new JMenuItem("打开");
openMenuItem.setMnemonic(KeyEvent.VK_O);
openMenuItem.addActionListener(new OpenActionListener());
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem=new JMenuItem("保存");
saveMenuItem.setMnemonic(KeyEvent.VK_S);
saveMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveMenuItem);
JMenuItem saveasMenuItem=new JMenuItem("另存为");
saveasMenuItem.setMnemonic(KeyEvent.VK_A);
saveasMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveasMenuItem);
JMenuItem exitMenuItem=new JMenuItem("退出");
exitMenuItem.setMnemonic(KeyEvent.VK_E);
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E ,ActionEvent.CTRL_MASK));
exitMenuItem.addActionListener(new ExitActionListener());
fileMenu.add(exitMenuItem);
mb.add(fileMenu);
JMenu editMenu=new JMenu("编辑(E)");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem cutMenuItem=new JMenuItem("剪切");
cutMenuItem.setMnemonic(KeyEvent.VK_T);
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
cutMenuItem.addActionListener(new CutActionListener());
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem=new JMenuItem("复制");
copyMenuItem.setMnemonic(KeyEvent.VK_C);
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
copyMenuItem.addActionListener(new CopyActionListener());
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem=new JMenuItem("粘贴");
pasteMenuItem.setMnemonic(KeyEvent.VK_P);
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
pasteMenuItem.addActionListener(new PasteActionListener());
editMenu.add(pasteMenuItem);
JMenuItem sallMenuItem=new JMenuItem("全选");
sallMenuItem.setMnemonic(KeyEvent.VK_A);
sallMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
sallMenuItem.addActionListener(new SallActionListener());
editMenu.add(sallMenuItem);
JMenuItem searchMenuItem=new JMenuItem("查找");
searchMenuItem.setMnemonic(KeyEvent.VK_S);
searchMenuItem.addActionListener(new SearchActionListener());
editMenu.add(searchMenuItem);
JMenuItem chanMenuItem=new JMenuItem("替换");
chanMenuItem.setMnemonic(KeyEvent.VK_G);
chanMenuItem.addActionListener(new ChanActionListener());
editMenu.add(chanMenuItem);
mb.add(editMenu);
JMenu helpMenu=new JMenu("帮助(H)");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem abouthMenuItem=new JMenuItem("关于记事本");
abouthMenuItem.setMnemonic(KeyEvent.VK_A);
abouthMenuItem.addActionListener(new AbouthActionListener());
helpMenu.add(abouthMenuItem);
mb.add(helpMenu);
}
class MyFindDialog extends JDialog implements ActionListener {
private Label l1 = new Label("查找字符串");
private Label l2 = new Label("替换字符串");
private TextField tf1 = new TextField(10);
private TextField tf2 = new TextField(10);
private Button b1 = new Button("向下查找");
private Button b2 = new Button("替换");
public MyFindDialog(Frame owner, JTextArea jta) {
super(owner, "查找", false);
setLayout(null);
l1.setBounds(10, 30, 80, 20);
l2.setBounds(10, 70, 80, 20);
tf1.setBounds(90, 30, 90, 20);
tf2.setBounds(90, 70, 80, 20);
b1.setBounds(190, 30, 80, 20);
b2.setBounds(190, 70, 80, 20);
add(l1);
add(l2);
add(tf1);
add(tf2);
add(b1);
add(b2);
setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void showFind() {
setTitle("查找");
setSize(280, 300);
setVisible(true);
}
public void showReplace() {
setTitle("查找替换");
setSize(280,300);
setVisible(true);
}
private void find() {
String content = jta.getText();
String str = tf1.getText();
int pp=jta.getCaretPosition();
int end=content.length();
String text=content.substring(pp,end);
if(str!=null&&!str.equals(""))
{
int start=text.indexOf(str);
if(start>=0)
{start=start+pp;
jta.setSelectionStart(start);
jta.setSelectionEnd(start+str.length());
}
else
{
javax.swing.JOptionPane.showMessageDialog(this,"成功完成向下查找!","查找完毕",JOptionPane.OK_OPTION | JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void replace() {
String str2 = tf2.getText();
find();
jta.replaceRange(str2, jta.getSelectionStart(),jta.getSelectionEnd());
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
find();
else if (e.getSource() == b2)
replace();
}
}
class SaveActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(file!=null) fc.setSelectedFile(file);
int returnVal=fc.showSaveDialog(Notebook.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
saving();}
}
void saving(){
try{
FileWriter Writef=new FileWriter(file);
Writef.write(jta.getText());
Writef.close();
}
catch(Exception e){e.printStackTrace();}
}
}
class OpenActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(file !=null)fc.setSelectedFile(file);
int returnVal=fc.showOpenDialog(Notebook.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
unfold();}}
void unfold(){
try{
FileReader Readf=new FileReader(file);
int len=(int)file.length();
char []buffer=new char[len];
Readf.read(buffer,0,len);
Readf.close();
jta.setText(new String(buffer));
}catch(Exception e){e.printStackTrace();}
}
}
class ExitActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
}
class CutActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.cut();
}
}
class CopyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.copy();
}
}
class PasteActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.paste();
}
}
class SallActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.setSelectionStart(0);
jta.setSelectionEnd(jta.getText().length());
}
}
class SearchActionListen