📄 notepad.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.undo.UndoManager;
public class NotePad extends JFrame implements ActionListener{
//JTextArea ta =new JTextArea();
JTextArea[] taa =new JTextArea[10];
JToolBar tb=new JToolBar();
JScrollPane[] sp=new JScrollPane[10];
JFileChooser filechooser = new JFileChooser(); //文件选择器
JTabbedPane tabbedPane=new JTabbedPane();
UndoManager[] um=new UndoManager[10];
Document[] doc = new Document[10] ;
int tabCounter = 0;
int index = 0;//正在使用的选项卡或对象等资源的标志
public NotePad(){
JFrame f=new JFrame("文本编辑器---李锋!");
f.setLayout(new BorderLayout());
f.setSize(500,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JMenuBar mb=new JMenuBar();
JMenu m1=new JMenu("文件");
JMenuItem mi1=new JMenuItem("新建");
mi1.addActionListener(this);
JMenuItem mi2=new JMenuItem("打开");
mi2.addActionListener(this);
JMenuItem mi3=new JMenuItem("关闭");
mi3.addActionListener(this);
JMenuItem mi4=new JMenuItem("保存");
mi4.addActionListener(this);
JMenuItem mi5=new JMenuItem("退出");
mi5.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m1.add(mi5);
mb.add(m1);
JMenu m2=new JMenu("编辑");
JMenuItem m21=new JMenuItem("剪切");
m21.addActionListener(this);
JMenuItem m22=new JMenuItem("复制");
m22.addActionListener(this);
JMenuItem m23=new JMenuItem("粘贴");
m23.addActionListener(this);
JMenuItem m24=new JMenuItem("撤销");
m24.addActionListener(this);
JMenuItem m25=new JMenuItem("重做");
m25.addActionListener(this);
m2.add(m21);
m2.add(m22);
m2.add(m23);
m2.add(m24);
m2.add(m25);
mb.add(m2);
JMenu m4=new JMenu("搜索和替换");
JMenuItem m41=new JMenuItem("搜索和替换");
m41.addActionListener(this);
m4.add(m41);
mb.add(m4);
JMenu m3=new JMenu("帮助");
JMenuItem m31=new JMenuItem("关于");
m31.addActionListener(this);
m3.add(m31);
mb.add(m3);
JButton b1=new JButton("新建");
b1.addActionListener(this);
tb.add(b1);
JButton b2=new JButton("打开");
b2.addActionListener(this);
tb.add(b2);
JButton b3=new JButton("保存");
b3.addActionListener(this);
tb.add(b3);
JButton b8=new JButton("关闭");
b8.addActionListener(this);
tb.add(b8);
JButton b4=new JButton("退出");
b4.addActionListener(this);
tb.add(b4);
JButton b5=new JButton("复制");
b5.addActionListener(this);
tb.add(b5);
JButton b6=new JButton("粘贴");
b6.addActionListener(this);
tb.add(b6);
JButton b7=new JButton("剪切");
b7.addActionListener(this);
tb.add(b7);
JButton b9=new JButton("撤销");
b9.addActionListener(this);
tb.add(b9);
JButton b10=new JButton("重做");
b10.addActionListener(this);
tb.add(b10);
//JTextArea ta =new JTextArea();
sp[0]=new JScrollPane();
taa[0]=new JTextArea();
um[0]=new UndoManager();
doc[0]=taa[0].getDocument();
doc[0].addUndoableEditListener(um[0]);
sp[0].getViewport().add(taa[0], null);
tabbedPane.add(sp[0]);
//tabs1.add(ta);
//sp.getViewport().add(tabs1, null);
f.setJMenuBar(mb);
f.add(tb,BorderLayout.NORTH);
//f.add(sp,BorderLayout.CENTER); //增加
f.add(tabbedPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
String com=e.getActionCommand();
if(com.equals("退出"))
System.exit(0);
else
if(com.equals("新建")){
final JPanel content = new JPanel();
JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
content.setLayout(new GridLayout(1,1));
taa[++index]=new JTextArea();
doc[index]=taa[index].getDocument();
um[index]=new UndoManager();
doc[index].addUndoableEditListener(um[index]);
sp[index]=new JScrollPane();
sp[index].getViewport().add(taa[index], null);
content.add(sp[index]);
//index++;
tabbedPane.addTab(null,content);
}
else
if(com.equals("打开")){
int i = filechooser.showOpenDialog(NotePad.this); //显示打开文件对话框
if (i == JFileChooser.APPROVE_OPTION){
//点击对话框中打开选项
File f = filechooser.getSelectedFile(); //得到选择的文件
try {
Reader is = new FileReader(f); //得到文件输入流
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(),filechooser.getName(f));
taa[tabbedPane.getSelectedIndex()].read(is, "d"); //读入文件到文本窗格
doc[tabbedPane.getSelectedIndex()]=taa[index].getDocument();
um[tabbedPane.getSelectedIndex()]=new UndoManager();
doc[tabbedPane.getSelectedIndex()].addUndoableEditListener(um[tabbedPane.getSelectedIndex()]);
}
catch (Exception ex) {
ex.printStackTrace(); //输出出错信息
}
}
}
else
if(com.equals("保存")){
int i = filechooser.showSaveDialog(NotePad.this); //显示保存文件对话框
if (i == JFileChooser.APPROVE_OPTION) { //点击对话框中保存按钮
File f = filechooser.getSelectedFile(); //得到选择的文件
try {
FileOutputStream out = new FileOutputStream(f); //得到文件输出流
out.write(taa[tabbedPane.getSelectedIndex()].getText().getBytes()); //写出文件
}
catch (Exception ex) {
ex.printStackTrace(); //输出出错信息
}
}
}
else
if(com.equals("关闭")){
tabbedPane.remove(tabbedPane.getSelectedIndex());
}
else
if(com.equals("剪切")){
taa[tabbedPane.getSelectedIndex()].cut(); //调用文本窗格的剪切命令
}
else
if(com.equals("复制")){
taa[tabbedPane.getSelectedIndex()].copy(); //调用文本窗格的拷贝命令
}
else
if(com.equals("粘贴")){
taa[tabbedPane.getSelectedIndex()].paste(); //调用文本窗格的粘贴命令
}
else
if(com.equals("关于")){
JOptionPane.showMessageDialog(NotePad.this, "文本编辑器---李锋"); //显示软件信息
}
else
if(com.equals("搜索和替换")){
String s,s1;
int x;
s = JOptionPane.showInputDialog("输入需要查找的:");
String a = taa[tabbedPane.getSelectedIndex()].getText();
if ((x=a.indexOf(s))!=-1){
taa[tabbedPane.getSelectedIndex()].setCaretPosition(x);
taa[tabbedPane.getSelectedIndex()].setSelectionStart(x);
taa[tabbedPane.getSelectedIndex()].setSelectionEnd(x+s.length());
s1 = JOptionPane.showInputDialog("输入需要替换的:");
taa[tabbedPane.getSelectedIndex()].replaceSelection(s1);
}
else
JOptionPane.showMessageDialog(null, new String("没有找到") + "");
}
else
if(com.equals("撤销")){
if(um[tabbedPane.getSelectedIndex()].canUndo()){
um[tabbedPane.getSelectedIndex()].undo();
}
}
else
if(com.equals("重做")){
if(um[tabbedPane.getSelectedIndex()].canRedo()){
um[tabbedPane.getSelectedIndex()].redo();
}
}
}
public static void main(String args[]){
new NotePad();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -