📄 notepad.java
字号:
/*创建一个简易记事本,文件菜单中有打开,保存等功能,编辑菜单中有撤消,拷贝,剪切等功能。
*/
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;//JFileChooser
import javax.swing.undo.*;//UndoableEditListener
import java.util.*; //Date
import javax.swing.text.*;
public class NotePad extends JFrame implements UndoableEditListener{
UndoableEdit edit;
JTextArea jta=new JTextArea("");
private JFileChooser jfile=new JFileChooser(new File("."));
ImageIcon o=new ImageIcon("image/2.gif");
ImageIcon s=new ImageIcon("image/1.gif");
private JLabel jlb=new JLabel();
public NotePad() {
//使用 Action类创建工具栏快捷按钮的监听
Action jbtoAction=new AbstractAction("打开"){
public void actionPerformed(ActionEvent e){
open();
}
};
Action jbtsAction=new AbstractAction("保存"){
public void actionPerformed(ActionEvent e){
save();
}
};
Action undoAction=new AbstractAction("撤消"){
public void actionPerformed(ActionEvent e){
edit.undo();
}
};
Action redoAction=new AbstractAction("重做"){
public void actionPerformed(ActionEvent e){
edit.redo();
}
};
JButton jbto=new JButton(jbtoAction);
JButton jbts=new JButton(jbtsAction);
JButton jbtu=new JButton(undoAction);
JButton jbtr=new JButton(redoAction);
jta.setLineWrap(true);//设定文本域自动换行功能;
// jta.setWrapStyleWord(true); //激活换行不断字功能;
jta.getDocument().addUndoableEditListener(this);
JScrollPane jsp=new JScrollPane(jta);
JMenuBar jmb=new JMenuBar();
JMenu mFile=new JMenu("File");
JMenu mEdit=new JMenu("Edit");
JMenu mColor=new JMenu("Color");
JMenu mOther=new JMenu("Other");
//文件菜单的子菜单的处理
JMenuItem mNew=new JMenuItem("New",KeyEvent.VK_N);
mFile.add(mNew);
JMenuItem mOpen=new JMenuItem("Open",KeyEvent.VK_O);
mFile.add(mOpen);
JMenuItem mSave=new JMenuItem("Save");
mFile.add(mSave);
mFile.addSeparator(); //添加分割线
JMenuItem mPrint = new JMenuItem("Print");
mFile.add(mPrint);
mFile.addSeparator(); //添加分割线
JMenuItem mExit=new JMenuItem("Exit");
mFile.add(mExit);
mFile.setMnemonic(KeyEvent.VK_F);
//编辑菜单的子菜单的处理
JMenuItem jmiundo=new JMenuItem("Undo");
mEdit.add(jmiundo);
JMenuItem jmiredo=new JMenuItem("Redo");
mEdit.add(jmiredo);
mEdit.addSeparator(); //添加分割线
JMenuItem jmiclear=new JMenuItem("Clear");
mEdit.add(jmiclear);
JMenuItem jmico=new JMenuItem("Copy");
mEdit.add(jmico);
JMenuItem jmicu=new JMenuItem("Cut");
mEdit.add(jmicu);
JMenuItem jmip=new JMenuItem("Paste");
mEdit.add(jmip);
mEdit.addSeparator(); //添加分割线
JMenuItem jmifind=new JMenuItem("find");
mEdit.add(jmifind);
JMenuItem jmireplace=new JMenuItem("Replace");
mEdit.add(jmireplace);
JMenuItem jmifindandreplace=new JMenuItem("Find&&Replace");
mEdit.add(jmifindandreplace);
//颜色菜单的子菜单的处理
JMenuItem jmiforeground=new JMenuItem("foreground");
JMenuItem jmibackground=new JMenuItem("background");
mColor.add(jmiforeground);
mColor.add(jmibackground);
//其它菜单的子菜单的处理
JMenuItem jmifont=new JMenuItem("font");
mOther.add(jmifont);
JMenuItem jmid=new JMenuItem("Date/Time");
mOther.add(jmid);
jmb.add(mFile);
jmb.add(mEdit);
jmb.add(mColor);
jmb.add(mOther);
JToolBar jToolBar1=new JToolBar(JToolBar.HORIZONTAL);
jToolBar1.setBorder(BorderFactory.createLineBorder(Color.red));
jToolBar1.add(jbto);
jToolBar1.add(jbts);
jToolBar1.add(jbtu);
jToolBar1.add(jbtr);
this.setJMenuBar(jmb);
getContentPane().add(jToolBar1,BorderLayout.NORTH);
getContentPane().add(jsp,BorderLayout.CENTER);
this.setSize(400,400);
this.setVisible(true);
this.setTitle("记事本");
//菜单打开文件的监听
mOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
open();
}
});
//菜单保存文件的监听
mSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
save();
}
});
//退出记事本的监听
mExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//编辑菜单撤消的监听
jmiundo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
edit.undo();
}
});
//编辑菜单重做的监听
jmiredo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
edit.redo();
}
});
//编辑菜单清除文件的监听
jmiclear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.setText(null);
}
});
//编辑菜单拷贝的监听
jmico.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.copy();
}
});
//编辑菜单剪切的监听
jmicu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.cut();
}
});
//编辑菜单粘贴的监听
jmip.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.paste();
}
});
//编辑菜单查找的监听
jmifind.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String content=JOptionPane.showInputDialog("enter your the find content:");
TextField findField=new TextField();
findField.setText(content);
findNext(findField.getText());
}
});
//编辑菜单替换的监听
jmireplace.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String change=JOptionPane.showInputDialog("enter your the replace content:");
TextField changeField=new TextField();
changeField.setText(change);
replace(changeField.getText());
}
});
//编辑菜单查找替换的监听
jmifindandreplace.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String content=JOptionPane.showInputDialog("enter your the find content:");
TextField findField=new TextField();
findField.setText(content);
findNext(findField.getText());
String change=JOptionPane.showInputDialog("enter your the replace content:");
TextField changeField=new TextField();
changeField.setText(change);
replace(changeField.getText());
}
});
//其它菜单添加日期的监听
jmid.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Date d=new Date();
jta.append(d.toString());
}
});
//前景色的监听
jmiforeground.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color selectedColor=JColorChooser.showDialog(null,"choose foregound color",
jta.getForeground());
if(selectedColor!=null)
jta.setForeground(selectedColor);
}
});
//背景色的监听
jmibackground.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color selectedColor=JColorChooser.showDialog(null,"choose backgound color",
jta.getForeground());
if(selectedColor!=null)
jta.setBackground(selectedColor);
}
});
//字体的监听
jmifont.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String fontNames[]={"隶书","华文行楷","黑体","宋体"};
// String fontNames[]=getToolkit().getFontList();
Object fonts=JOptionPane.showInputDialog(null,"select the fonts content:","your love",
JOptionPane.QUESTION_MESSAGE,null,fontNames,null);
String font=fonts.toString();
// Object[] stylelist={"Font.PLAIN","Font.BOLD","Font.ITALIC","Font.BOLDITALIC"};
// Object styles=JOptionPane.showInputDialog(null,"select the style content:","your love",
// JOptionPane.QUESTION_MESSAGE,null,stylelist,null);
// int s=((Integer)styles).intValue();
Object[] sizeNames={new Integer(2),new Integer(4),new Integer(6),new Integer(8),new Integer(10),
new Integer(12),new Integer(14),new Integer(16),new Integer(18),new Integer(20),
new Integer(25),new Integer(36)};
Object sizes=JOptionPane.showInputDialog(null,"select the size content:","your love",
JOptionPane.QUESTION_MESSAGE,null,sizeNames,null);
int size=((Integer)sizes).intValue();
Font font1=new Font(font,Font.BOLD,size);
//Font.BOLD,size);
jta.setFont(font1);
}
});
}
public void open(){
if(jfile.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
open(jfile.getSelectedFile());
}
public void open( File file){
try{
//打开指定文件;
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
byte[] b=new byte[in.available()];
in.read(b,0,b.length);
jta.append(new String(b,0,b.length));
in.close();
jlb.setText(file.getName()+"opened");
}
catch(IOException ex){
jlb.setText("error opening"+file.getName());
}
}
public void save(){
if(jfile.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
save(jfile.getSelectedFile());
}
}
public void save(File file) {
try{
//保存指定文件;
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
byte[] b=(jta.getText()).getBytes();
out.write(b,0,b.length);
out.close();
jlb.setText(file.getName()+"saveed");
}
catch(IOException ex){
jlb.setText("error saveing"+file.getName());
}
}
public void findNext(String f){
String edit=jta.getText();
int start=jta.getSelectionStart();
int end=jta.getSelectionEnd();
int index=-1;
index=edit.indexOf(f,start+1);
if(index==-1&&start!=0){
index=edit.indexOf(f);
}
if(index!=-1){
jta.setSelectionStart(index);
jta.setSelectionEnd(index+f.length());
}
}
public void replace(String f){
int start=jta.getSelectionStart();
int end=jta.getSelectionEnd();
if(start!=end)
jta.replaceRange(f,start,end);
}
public void undoableEditHappened(UndoableEditEvent e)
{
StringBuffer buf=new StringBuffer(200);
edit=e.getEdit();
buf.append(edit.getPresentationName());
}
public static void main(String args[])
{
new NotePad();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -