点击查看代码
package notepad;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
public class notepadMain extends JFrame {
// 建立一个窗体
JFrame mainWindow;
// 建立菜单栏
JMenuBar menuBar;
/*
* file 文件 edit 编辑 help 帮助 version 查看 format 格式
*/
JMenu file, edit, help, version, foramt, sendTo;
JMenuItem openFile, cut, save, saveAs, copy, stick, sendEmail, find, about, peizhi,replace,all;
JTextArea input;
JPanel textPanel;
ArrayList<String> gettedList;
// 你所要查找的文本的位置
int position;
//----------------------------------------------------------------------
// 下面是创建配置邮箱软件打开路径窗口的组件
JFrame pzfm;
JPanel pzpanel1, pzpanel2, pzpanel3;
JTextField pzjtf;
JButton pzbut;
JLabel pzlabel;
// 存储邮箱路径
String address;
File pzfile;
FileWriter pzfwriter;
// ------------------------------
// 获取邮箱地址路径
String getedAddress;
File getfile;
FileReader getfread;
// -------------------------------
// 替换
JLabel label1;
JTextField fiel, fiel2;
JButton but1;
int index;
// ---------------------------------
//构造方法
notepadMain() {
mainWindow = new JFrame("记事本");
mainWindow.setBounds(0, 0, 900, 700);
mainWindow.setLocationRelativeTo(null);
getEmailAddress();
setMenuBar();
setPanel();
listener();
// getEmailAddress();
mainWindow.setVisible(true);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 设置menuBar
public void setMenuBar() {
menuBar = new JMenuBar();
file = new JMenu("文件");
edit = new JMenu("编辑");
help = new JMenu("帮助");
sendTo = new JMenu("发送");
version = new JMenu("查看");
foramt = new JMenu("配置");
openFile = new JMenuItem("打开");
cut = new JMenuItem("剪切");
save = new JMenuItem("保存");
saveAs = new JMenuItem("另存为");
stick = new JMenuItem("粘贴");
copy = new JMenuItem("复制");
peizhi = new JMenuItem("配置邮件路径");
sendEmail = new JMenuItem("复制全文并打开邮箱");
find = new JMenuItem("查找");
replace=new JMenuItem("替换");
about = new JMenuItem("关于");
all=new JMenuItem("全选");
menuBar.add(file);
menuBar.add(edit);
menuBar.add(foramt);
menuBar.add(version);
menuBar.add(sendTo);
menuBar.add(help);
file.add(openFile);
file.add(save);
file.add(saveAs);
edit.add(cut);
edit.add(copy);
edit.add(stick);
edit.add(all);
foramt.add(peizhi);
sendTo.add(sendEmail);
version.add(find);
version.add(replace);
help.add(about);
// 添加menuItem在此
mainWindow.setJMenuBar(menuBar);
}
//设置menu的监听
void listener() {
openFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
openFile();
}
});
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
saveFile();
}
});
saveAs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
saveOther();
}
});
cut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
input.cut();
}
});
copy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
input.copy();
}
});
stick.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
input.paste();
}
});
all.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
input.selectAll();
}
});
find.addActionListener(new ActionListener() {
/*
* 实现查找 将文本呢域中的内容通过Tihuan类的构造方法传入Tihuan类中,
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
// 获取列数
// 获取文本域内输入的内容
// String youInput=input.getText();
// gettedList=new ArrayList<String>();
// Tihuan find=new Tihuan(youInput); t
// --------------------------------------------------
// setFrame();
// addListen(youInput);
findT();
}
});
replace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
replaceT();
}
});
sendEmail.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
input.selectAll();
input.copy();
Runtime rt = Runtime.getRuntime();
try {
rt.exec(getedAddress);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
peizhi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
setPzWindow();
}
});
about.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
JOptionPane.showMessageDialog(null, "20202417111韩佳成");
}
});
}
//创建一个窗体,专门用于配置记事本打开邮箱的路径
void setPzWindow() {
pzfm = new JFrame();
pzfm.setTitle("配置邮箱路径");
pzpanel1 = new JPanel();
pzpanel2 = new JPanel();
pzpanel3 = new JPanel();
pzbut = new JButton("确定");
pzjtf = new JTextField(30);
pzlabel = new JLabel("邮箱路径:");
pzfm.setSize(450, 120);
pzfm.setLocation(1000, 400);
pzfm.setResizable(false);
pzpanel1.add(pzlabel);
pzpanel1.add(pzjtf);
pzpanel1.add(pzbut);
pzfm.add(pzpanel1);
// pzfm.add(pzpanel2);
pzfm.setVisible(true);
// 绑定配置窗口中的确定按钮
pzbut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
// pzfile.delete();
setEmailFileAddress();
// 点击确定后关闭窗口
pzfm.setVisible(false);
}
});
}
//创建一个文件,,文件内存储需要打开的软件的路径
public void setEmailFileAddress() {
address = pzjtf.getText();
pzfile = new File("d://Emailaddress.txt");
try {
pzfile.createNewFile();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
JOptionPane.showMessageDialog(null, "创建文件失败");
e1.printStackTrace();
}
try {
pzfwriter = new FileWriter(pzfile, true);
pzfwriter.write("");
pzfwriter.flush();
} catch (IOException e) {
// TODO 自动生成的 catch 块
JOptionPane.showMessageDialog(null, "创建失败");
e.printStackTrace();
}
try {
// pzfwriter.write(address);
BufferedWriter out = new BufferedWriter(pzfwriter);
out.write(address);
out.flush();
out.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
JOptionPane.showMessageDialog(null, "写入异常");
e.printStackTrace();
}
}
//读取邮件地址
public void getEmailAddress() {
int n;
byte[] a = new byte[1000];
getfile = new File("d://Emailaddress.txt");
try {
FileInputStream in = new FileInputStream(getfile);
while ((n = in.read(a, 0, 1000)) != -1) {
String s = new String(a, 0, n);
getedAddress = s;
System.out.println(getedAddress);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
setPzWindow();
// e.printStackTrace();
}
}
// 设置输入的面板
public void setPanel() {
textPanel = new JPanel();
input = new JTextArea();
input.setFont(new Font("微软雅黑", Font.BOLD, 30));
input.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(input);
mainWindow.add(scrollPane);
}
//打开功能的实现
void openFile() {
FileDialog fd = new FileDialog(notepadMain.this, "打开", FileDialog.LOAD);
// 在这里我想限制打开的文件只能是txt
fd.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO 自动生成的方法存根
name = ".txt";
return false;
}
});
fd.setVisible(true);
File file = new File(fd.getDirectory() + "\\" + fd.getFile());
FileReader fr = null;
try {
fr = new FileReader(file);
char[] chs = new char[1024];
int len;
input.setText("");
while ((len = fr.read(chs)) != -1) {
input.append(new String(chs, 0, len));
}
setTitle("记事本----" + file.getName());
} catch (IOException e2) {
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
//保存功能的实现
void saveFile() {
// 设置一个文本过滤器,使文件保存类型为txt
FileDialog fd = new FileDialog(notepadMain.this, "保存", FileDialog.SAVE);
// 在这里我想限制保存的文件只能是txt
fd.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO 自动生成的方法存根
name = input.getText() + ".txt"; // 设置保存的默认名称为输入内容+.txt
return false;
}
});
// 显示对话框
fd.setVisible(true);
// 获取用户选择的文件的完整名称
File file = new File(fd.getDirectory() + "\\" + fd.getFile());
FileWriter fw = null;
try {
fw = new FileWriter(file);
// 写文本区的数据到文件
fw.write(input.getText());
setTitle("记事本----" + file.getName());
} catch (IOException e1) {
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
// 另存为
public void saveOther() {
JFileChooser chooser = new JFileChooser();// 创建文件选择器
int result = chooser.showSaveDialog(null);// 弹出一个save file文件选择器对话框
if (result == JFileChooser.APPROVE_OPTION) {// JFileChooser.APPROVE_OPTION是个整数常量,代表0
try {
// 向磁盘中写文件
FileWriter fout = new FileWriter(chooser.getSelectedFile(), true);// 创建文件输出流对象
// 写数据
String string = input.getText();
fout.write(input.getText() + "\r\n");
fout.close();// 关闭流,释放IO流所占的系统资源
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//创建替换窗口----------------------------------未来要实现替换,此窗口里有替换要的ui
public void setFrame() {
JFrame frame = new JFrame();
frame.setSize(350, 160);
frame.setLocation(1000, 400);
frame.setResizable(false);
frame.setTitle("查找");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel jpanel = new JPanel();
label1 = new JLabel("查找内容(N)");
fiel = new JTextField(10);
but1 = new JButton("查找 下一 个(F)");// 对按钮添加事件
// but1.addActionListener(this);
JLabel label2 = new JLabel("替 换 为(P)");
fiel2 = new JTextField(10);
JButton but2 = new JButton("替 换 ( R )");
// but2.addActionListener(this);
JButton but3 = new JButton("取消");
JCheckBox box = new JCheckBox("区分大小写(C)");
jpanel.add(label1);
jpanel.add(fiel);
jpanel.add(but1);
// 这里---------------------------------
jpanel.add(label2);
jpanel.add(fiel2);
jpanel.add(but2);
jpanel.add(box);
jpanel.add(but3);
// 到这里的组件如果功能没有实现就注释掉--------
frame.add(jpanel);
frame.setVisible(true);
}
//替换窗口事件绑定
// 将查找按钮绑定事件
public void addListen(String list) {
but1.addActionListener(new ActionListener() {
// 查找的按钮绑定
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int column, line;// column is 列,line is 行
index = list.indexOf(fiel.getText());
// line=index/row+1;
// column=index%row;
if (index == -1) {
JOptionPane.showMessageDialog(null, "没有找到目标字符,请重新确认(查找为大小写敏感)");
} else {
// 新弹出一个对话框显示你要查找的字符串再我文档的第几个
JOptionPane.showMessageDialog(null, index + 1);
input.setCaretPosition(index);
}
}
});
}
//-------------------------------------------------------------------------------
// ----------------------------------------------------
public void findT() {
final JDialog findDialog = new JDialog(this, "查找", false);// false时允许其他窗口同时处于激活状态(即无模式)
Container con = findDialog.getContentPane();// 返回此对话框的contentPane对象
con.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel findContentLabel = new JLabel("查找内容");
final JTextField findText = new JTextField(15);
JButton findNextButton = new JButton("查找下一个");
final JCheckBox matchCheckBox = new JCheckBox("区分大小写");
ButtonGroup bGroup = new ButtonGroup();
final JRadioButton upButton = new JRadioButton("向前");
final JRadioButton downButton = new JRadioButton("向后");
downButton.setSelected(true);
bGroup.add(upButton);
bGroup.add(downButton);
JButton cancel = new JButton("取消");
// 创建"查找"对话框的界面
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel directionPanel = new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
directionPanel.add(upButton);
directionPanel.add(downButton);
panel1.setLayout(new GridLayout(2, 1));
panel1.add(findNextButton);
panel1.add(cancel);
panel2.add(findContentLabel);
panel2.add(findText);
panel2.add(panel1);
panel3.add(matchCheckBox);
panel3.add(directionPanel);
con.add(panel2);
con.add(panel3);
findDialog.setSize(410, 180);
findDialog.setResizable(false);// 不可调整大小
findDialog.setLocation(230, 280);
findDialog.setVisible(true);
// 取消按钮事件处理
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
findDialog.dispose();
}
});
// "查找下一个"按钮监听
findNextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { // "区分大小写(C)"的JCheckBox是否被选中
int k = 0, m = 0;
final String str1, str2, str3, str4, strA, strB;
str1 = input.getText();
str2 = findText.getText();
//toUpperCase()可以将字符串小写转化为大写
str3 = str1.toUpperCase();
str4 = str2.toUpperCase();
if (matchCheckBox.isSelected())// 区分大小写
{
//strA获取用户输入的文本域的内容
//strB获取用户希望查找的内容
strA = str1;
strB = str2;
} else// 不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找
{
strA = str3;
strB = str4;
}
if (upButton.isSelected()) { // k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if (input.getSelectedText() == null)
k = strA.lastIndexOf(strB, input.getCaretPosition() - 1);
else
k = strA.lastIndexOf(strB, input.getCaretPosition() - findText.getText().length() - 1);
if (k > -1) { // String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k, k + strB.length());
} else {
JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "查找", JOptionPane.INFORMATION_MESSAGE);
}
} else if (downButton.isSelected()) {
if (input.getSelectedText() == null)
k = strA.indexOf(strB, input.getCaretPosition() + 1);
else
k = strA.indexOf(strB, input.getCaretPosition() - findText.getText().length() + 1);
if (k > -1) { // String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k, k + strB.length());
} else {
JOptionPane.showMessageDialog(null, "找不到您查找的内容!", "查找", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});// "查找下一个"按钮监听结束
}// 查找方法结束
// -----------------------------------------------------
// 替换方法
public void replaceT()
{ final JDialog replaceDialog=new JDialog(this,"替换",false);//false时允许其他窗口同时处于激活状态(即无模式)
Container con=replaceDialog.getContentPane();//返回此对话框的contentPane对象
con.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel findContentLabel=new JLabel("查找内容:");
final JTextField findText=new JTextField(15);
JButton findNextButton=new JButton("查找下一个:");
JLabel replaceLabel=new JLabel("替换为:");
final JTextField replaceText=new JTextField(15);
JButton replaceButton=new JButton("替换");
JButton replaceAllButton=new JButton("全部替换");
JButton cancel=new JButton("取消");
cancel.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ replaceDialog.dispose();
}
});
final JCheckBox matchCheckBox=new JCheckBox("区分大小写");
ButtonGroup bGroup=new ButtonGroup();
final JRadioButton upButton=new JRadioButton("向前");
final JRadioButton downButton=new JRadioButton("向后");
downButton.setSelected(true);
bGroup.add(upButton);
bGroup.add(downButton);
findNextButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ //"区分大小写(C)"的JCheckBox是否被选中
int k=0,m=0;
final String str1,str2,str3,str4,strA,strB;
str1=input.getText();
str2=findText.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//区分大小写
{ strA=str1;
strB=str2;
}
else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找
{ strA=str3;
strB=str4;
}
if(upButton.isSelected())
{ //k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if(input.getSelectedText()==null)
k=strA.lastIndexOf(strB,input.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB, input.getCaretPosition()-findText.getText().length()-1);
if(k>-1)
{ //String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k,k+strB.length());
}
else
{ JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(downButton.isSelected())
{ if(input.getSelectedText()==null)
k=strA.indexOf(strB,input.getCaretPosition()+1);
else
k=strA.indexOf(strB, input.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{ //String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k,k+strB.length());
}
else
{ JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
}
});//"查找下一个"按钮监听结束
//"替换"按钮监听
replaceButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ if(replaceText.getText().length()==0 && input.getSelectedText()!=null)
input.replaceSelection("");
if(replaceText.getText().length()>0 && input.getSelectedText()!=null)
input.replaceSelection(replaceText.getText());
}
});//"替换"按钮监听结束
//"全部替换"按钮监听
replaceAllButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ input.setCaretPosition(0); //将光标放到编辑区开头
int k=0,m=0,replaceCount=0;
if(findText.getText().length()==0)
{ JOptionPane.showMessageDialog(replaceDialog,"请填写查找内容!","提示",JOptionPane.WARNING_MESSAGE);
findText.requestFocus(true);
return;
}
while(k>-1)//当文本中有内容被选中时(k>-1被选中)进行替换,否则不进行while循环
{ //"区分大小写(C)"的JCheckBox是否被选中
final String str1,str2,str3,str4,strA,strB;
str1=input.getText();
str2=findText.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//区分大小写
{ strA=str1;
strB=str2;
}
else//不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找
{ strA=str3;
strB=str4;
}
if(upButton.isSelected())
{ //k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if(input.getSelectedText()==null)
k=strA.lastIndexOf(strB,input.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB, input.getCaretPosition()-findText.getText().length()-1);
if(k>-1)
{ //String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k,k+strB.length());
}
else
{ if(replaceCount==0)
{ JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{ JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(downButton.isSelected())
{ if(input.getSelectedText()==null)
k=strA.indexOf(strB,input.getCaretPosition()+1);
else
k=strA.indexOf(strB, input.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{ //String strData=strA.subString(k,strB.getText().length()+1);
input.setCaretPosition(k);
input.select(k,k+strB.length());
}
else
{ if(replaceCount==0)
{ JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{ JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(replaceText.getText().length()==0 && input.getSelectedText()!= null)
{ input.replaceSelection("");
replaceCount++;
}
if(replaceText.getText().length()>0 && input.getSelectedText()!= null)
{ input.replaceSelection(replaceText.getText());
replaceCount++;
}
}//while循环结束
}
});//"替换全部"方法结束
//创建"替换"对话框的界面
JPanel directionPanel=new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
directionPanel.add(upButton);
directionPanel.add(downButton);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(2,1));
panel1.add(findContentLabel);
panel1.add(findText);
panel1.add(findNextButton);
panel4.add(replaceButton);
panel4.add(replaceAllButton);
panel2.add(replaceLabel);
panel2.add(replaceText);
panel2.add(panel4);
panel3.add(matchCheckBox);
panel3.add(directionPanel);
panel3.add(cancel);
con.add(panel1);
con.add(panel2);
con.add(panel3);
replaceDialog.setSize(420,220);
replaceDialog.setResizable(false);//不可调整大小
replaceDialog.setLocation(230,280);
replaceDialog.setVisible(true);
}//"全部替换"按钮监听结束
//--------------------------------------------------------------
public static void main(String[] args) {
new notepadMain();
}
}