java做的简单记事本 下载本文

内容发布更新时间 : 2024/12/22 16:18:08星期一 下面是文章的全部内容请认真阅读。

}

if(strCmd.equals(\全选\ {

text.selectAll(); }

if(strCmd.equals(\清空\ {

text.setText(\ }

if(strCmd.equals(\复制\ {

str=text.getSelectedText(); }

if(strCmd.equals(\粘贴\ {

text.insert(str,text.getCaretPosition()); }

if(strCmd.equals(\剪切\ {

str=text.getSelectedText();

text.replaceRange(\;

}

if(strCmd.equals(\字体\ {

d.setVisible(true); }

if(strCmd.equals(\颜色选择\ {

Color c=JColorChooser.showDialog(this,\颜色选择\

text.setForeground(c); Red=c.getRed();

Green=c.getGreen(); Blue=c.getBlue(); }

if(e.getSource()==tFind||e.getSource()==bFind) {

find(); return; }

11

if(e.getSource()==tReplace||e.getSource()==bReplace) {

text.requestFocus();

String s1=tReplace.getText(); if

(text.getSelectedText().equals(tFind.getText())) {

int start=text.getSelectionStart(); text.replaceRange(s1, text.getSelectionStart(), text.getSelectionEnd());

text.select(start,start+s1.length()); } else

find(); return; }

if(e.getSource()==bReplaceAll) {

text.requestFocus();

String s1=tReplace.getText(); str = text.getText();

String strFind = tFind.getText(); int start = 0;

int end = str.length();

int len = strFind.length(); int count=0;

if (start<=end-len) {

for (;start <= end - len; start++) { if (str.substring(start, start + len).equals(strFind)) {

count++;

text.replaceRange(s1, start,start + len);

text.select(start,start+s1.length()); } } }

if(count==0)

JOptionPane.showConfirmDialog(findD,\没有

12

找到!\替换提示\ else

JOptionPane.showConfirmDialog(findD,\共替换字符\个.\替换提示\ return; }

if(strCmd.equals(\查找\ { findD.setVisible(true); }

if(strCmd.equals(\替换\ {

findD.setVisible(true); }

if(strCmd.equals(\插入时间\ {

text.setText(text.getText()+date); }

if(strCmd.equals(\确定\ {

String s1=\ int s;

s1=c1.getSelectedItem(); Str1=s1;

System.out.print(s1); s2=c2.getSelectedItem(); if(s2.equals(\粗体\ s=1;

else if(s2.equals(\斜体\ s=2;

else if(s2.equals(\粗体与斜体\ s=3; else

s=0; Str2=s2;

s3=c3.getSelectedItem();

text.setFont(new Font(s1,s,Integer.parseInt(s3))); Str3=s3;

d.setVisible(false); }

if(strCmd.equals(\取消\ {

d.setVisible(false); }

13

if(strCmd.equals(\关于\ {

About.setVisible(true); }

if(strCmd.equals(\自动换行\ {

text.setLineWrap(true); } }

public void openFile() throws IOException {

String str1=\ int n=0; Color c;

FileDialog fd=new FileDialog(this,\打开对话框\

fd.setVisible(true);

File file=new File(fd.getDirectory()+fd.getFile());

str1=(fd.getFile()).substring(0,(fd.getFile()).length()-4); File fStyle=new File(fd.getDirectory()+str1+\ FileReader fileReader=new FileReader(fStyle);

BufferedReader br=new BufferedReader(fileReader,4096); while((str2=br.readLine())!=null) {

string+=str2;

}

br.close();

System.out.println(string);

StringTokenizer str3=new StringTokenizer(string,\ String []str4=new String[6]; while(str3.hasMoreTokens()) {

str4[n]=str3.nextToken(); n++; }

System.out.print(str4[0]); Str1=str4[0]; Str2=str4[1]; Str3=str4[2];

Red=Integer.parseInt(str4[3]); Green=Integer.parseInt(str4[4]); Blue=Integer.parseInt(str4[5]); c=new Color(Red,Green,Blue);

14

text.setForeground(c); if(Str2.equals(\粗体\ {

temp=1; }

if(Str2.equals(\斜体\ {

temp=2; }

if(Str2.equals(\粗体与斜体\ {

temp=3; }

if(Str2.equals(\普通\ {

temp=0; }

FileReader fr=new FileReader(file);

BufferedReader bufr=new BufferedReader(fr,4096); text.setText(\ String texts=\

while((s=bufr.readLine())!=null) {

text.setFont(new Font(Str1,temp,Integer.parseInt(Str3))); texts+=s+\ }

text.setText(texts); bufr.close(); }

public void saveFile() throws IOException {

FileDialog fd=new FileDialog(this,\保存对话框\

fd.setVisible(true);

File file=new File(fd.getDirectory()+fd.getFile()+\ File fileStyle=new File(fd.getDirectory()+fd.getFile()+\ fileStyle.createNewFile();

FileWriter fw =new FileWriter(file);

BufferedWriter bufw=new BufferedWriter(fw); FileWriter fstyle=new FileWriter(fileStyle);

BufferedWriter bufw2=new BufferedWriter(fstyle); String

temp=\

15