📄 notepad.java
字号:
{
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("",text.getSelectionStart(),text.getSelectionEnd());
}
if(strCmd.equals("字体"))
{
d.setVisible(true);
}
if(strCmd.equals("颜色选择"))
{
Color c=JColorChooser.showDialog(this,"颜色选择",Color.black);
text.setForeground(c);
Red=c.getRed();
Green=c.getGreen();
Blue=c.getBlue();
}
if(e.getSource()==tFind||e.getSource()==bFind)
{
find();
return;
}
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,"没有找到!","替换提示",JOptionPane.YES_NO_OPTION);
else
JOptionPane.showConfirmDialog(findD,"共替换字符"+count+"个.","替换提示",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
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="",s2="",s3="";
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);
}
if(strCmd.equals("关于"))
{
About.setVisible(true);
}
if(strCmd.equals("自动换行"))
{
text.setLineWrap(true);
}
}
public void openFile() throws IOException
{
String str1="",str2="",string="";
int n=0;
Color c;
FileDialog fd=new FileDialog(this,"打开对话框",FileDialog.LOAD);
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+".css");
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);
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="",s;
while((s=bufr.readLine())!=null)
{
text.setFont(new Font(Str1,temp,Integer.parseInt(Str3)));
texts+=s+"\n";
}
text.setText(texts);
bufr.close();
}
public void saveFile() throws IOException
{
FileDialog fd=new FileDialog(this,"保存对话框",FileDialog.SAVE);
fd.setVisible(true);
File file=new File(fd.getDirectory()+fd.getFile()+".txt");
File fileStyle=new File(fd.getDirectory()+fd.getFile()+".css");
fileStyle.createNewFile();
FileWriter fw =new FileWriter(file);
BufferedWriter bufw=new BufferedWriter(fw);
FileWriter fstyle=new FileWriter(fileStyle);
BufferedWriter bufw2=new BufferedWriter(fstyle);
String temp="/"+Str1+"/"+Str2+"/"+Str3+"/"+Red+"/"+Green+"/"+Blue+"/";
bufw2.write(temp,0,temp.length());
bufw2.flush();
bufw2.close();
String s=text.getText();
bufw.write(s,0,s.length());
bufw.flush();
bufw.close();
}
public void find()
{
text.requestFocus();
str = text.getText();
String strFind = tFind.getText();
int end = str.length();
int len = strFind.length();
int start = text.getSelectionEnd();
if (start == end)
start = 0;
for (;start <= end - len; start++) {
if (str.substring(start, start + len).equals(strFind)) {
text.select(start,start+len);
return;
}
}
JOptionPane.showConfirmDialog(findD,"没有找到!!","提示",JOptionPane.YES_NO_OPTION);
text.select(start,start+len);
}
}
public class NotePad
{
public static void main(String [] args)
{
MyTextPad np=new MyTextPad("计事本");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -