public void CreatFile()
{
pad.jta.setText("");
}
public void OpenFile()
{
if(jfc.APPROVE_OPTION==jfc.showOpenDialog(null))
{
file=jfc.getSelectedFile();
path=file.getPath();
pad.setTitle(file.getName());
try
{
fr=new FileReader(file);
int length=(int) file.length();
char array[]=new char[length];
fr.read(array, 0, length);
pad.jta.setText(new String(array));
fr.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
public void SaveFile()
{
file=new File(path);
if( !file.exists())
{
SaveasFile();
}else
{
try{
String text=pad.jta.getText();
fw=new FileWriter(file);
fw.write(text);
fw.close();
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void SaveasFile()
{
if(jfc.APPROVE_OPTION==jfc.showSaveDialog(null))
{
path=jfc.getSelectedFile().getAbsolutePath();
filename=jfc.getSelectedFile().getName();
if(filename.indexOf('.')==-1)
{
path+=".txt";
}
String title=filename+"-记事本";
file=new File(path);
try
{
if(!file.exists())
{
fw=new FileWriter(file);
String text=pad.jta.getText();
fw.write(text);
fw.close();
}else
{
}
} catch ( Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pad.setTitle(title);
}
}
public void print()
{
String str=pad.jta.getText();
if(str.length()>0&&str!=null)
{
try {
pad.jta.print();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{
JOptionPane.showConfirmDialog
(null, "对不起,文本为空,打印取消!", "Empty",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
}
}
public void exit()
{
System.exit(0);
}
public void CopyFile()
{
pad.jta.copy();
}
public void CutFile()
{
pad.jta.cut();
}
public void PasteFile()
{
pad.jta.paste();
}
public void Delete()
{
String str=pad.jta.getSelectedText();
if(str!=null&&str.length()>0)
{
String text=pad.jta.getText();
text=text.replace(str, " ");
pad.jta.setText(text);
}
}
public void SelectAll()
{
pad.jta.selectAll();
}
public void ShowTime()
{
int year=date.getYear()+1900;
int month=date.getMonth()+1;
int day=date.getDate();
int hour=date.getHours();
int minute=date.getMinutes();
String time;
if(minute==0)
{
time=hour+":"+minute+"0 "+year+"/"+month+"/"+day+" ";
}else if(minute>0&&minute<9)
{
time=hour+":0"+minute+" "+year+"/"+month+"/"+day+" ";
}else
{
time=hour+":"+minute+" "+year+"/"+month+"/"+day+" ";
}
font=new Font("宋体",Font.BOLD,18);
pad.jta.setFont(font);
pad.jta.append(time);
}
public void Clip()
{
jf=new JFrame();
JPanel jp1=new JPanel();
MyPanel mp=new MyPanel();
jtf=new JTextField(10);
jl=new JLabel("行号:");
jb1=new JButton("转到");
jb1.setActionCommand("clip");
jb1.addActionListener(mp);
jb2=new JButton("取消");
jb2.setActionCommand("cancel");
jb2.addActionListener(mp);
jp1.add(jl);
jp1.add(jtf);
mp.add(jb1);
mp.add(jb2);
jf.setTitle("转到");
jf.setLayout(new GridLayout(2,1));
jf.add(jp1);
jf.add(mp);
jf.setSize(200,100);
jf.setLocation(450,300);
jf.setVisible(true);
jf.setResizable(false);
}
public void StateFile()
{
if(pad.state.getState())
{
MyTime mt=new MyTime();
mt.setPreferredSize(new Dimension(400, 20));
Thread thread=new Thread(mt);
pad.stateJToolBar=new JToolBar();
pad.stateJToolBar.add(mt);
pad.add(pad.stateJToolBar, BorderLayout.SOUTH);
pad.stateJToolBar.setVisible(true);
thread.start();
}else
{
pad.stateJToolBar.setVisible(false);
}
}
public void AboutFile()
{
String message="记事本作者:钱夫利"
+"\n编写语言:java!"
+"\n编写日期:2013年4月10日"
;
JOptionPane.showConfirmDialog(null, message,"关于记事本...",JOptionPane.PLAIN_MESSAGE);
}
public void HelpFile()
{
String message="1.记事本软件界面很简洁,使用方法简单,但是也仅拥有着基本文字编辑的功能。\n不能对文字进行排版和样式编辑。"
+"\n2.软件最上方的是标题栏,下面的是菜单栏(点击不同按钮会出现不同的下拉菜单),中间白色空\n白区域为编辑区域,下方的是状态栏,右边是滚动条。"
+ "\n3.下拉菜单说明:"
+ "\n(1)文件"
+ "\n新建:新建一个空白的文档。[如果正在编辑中的文档被修改过,会提醒是否保存。]"
+ "\n打开:打开一个已知的文本文档。[如果正在编辑中的文档被修改过,会提醒是否保存。]"
+ "\n保存:保存现在编辑中的文档。[如果没有保存过会提示保存的位置,如果保存过则会自动覆写上已经保存的文件。]"
+ "\n另存为:将现在编辑中的文件存到别的地方。[如果文件保存过则需要保存到别的地方或者改名保\n存到同一位置,如果文件没有保存过则功能同 保存。]"
+ "\n打印:将现在编辑中的文件输出到打印终端进行打印。"
+ "\n退出:退出记事本软件"
+ "\n(2)编辑"
+ "\n撤销:撤销本次操作,恢复上一次的操作"
+ "\n剪切:将选中的文字剪贴到剪贴板中"
+ "\n复制:将选中的文字复制到剪贴板中"
+ "\n粘贴:将剪贴板中的文字粘贴到记事本的光标处"
+ "\n删除:删除选中的文字。撤销:撤销上一步操作"
+ "\n查找:在文本中查找给定的字符"
+ "\n查找下一个:在文本中依次查找给定的字符"
+ "\n替换:在文本中选取字符并用给定字符替换"
+ "\n转到:跳转到指定的文本行"
+ "\n全选:选定文本中的所有字符"
+ "\n时间/日期:在文本中插入当前日期"
+ "\n(3)格式"
+ "\n自动换行:当文字到达最右边的时候,自动切换到下一行"
+ "\n字体:文字的显示设置,可设置 字体、字形、大小,并提供预览"
+ "\n(4)查看"
+ "\n状态栏:当前光标的所在的行数列数,显示时间以及文字总数,需要重点击状态栏才显示最新消息"
+ "\n(5)帮助"
+ "\n查看帮助:给出本记事本的所有菜单的功能及使用方法"
+ "\n关于记事本:记事本的作者和完成日期及编写语言"
;
JOptionPane.showMessageDialog(null,message,"查看帮助",JOptionPane.PLAIN_MESSAGE);
}
public int getlineNumber()
{
int totalLine=pad.jta.getLineCount();
int[] lineNumber=new int[totalLine + 1];
int pos=0, t=0, num=0, i=0;
String s=pad.jta.getText();
while (true)
{
pos=s.indexOf('\12', pos); // 返回 \n 所在的位置
if (pos==-1)
break;
lineNumber[t++]=pos++;
}
if (pad.jta.getCaretPosition() <=lineNumber[0])
num=1;
else
{
if (pad.jta.getCaretPosition() > lineNumber[pad.jta.getLineCount() - 1])
num=pad.jta.getLineCount();
for (i=0; i < totalLine + 1; i++)
{
if (pad.jta.getCaretPosition() <=lineNumber[i])
{
num=i + 1;
break;
} else
continue;
}
}
return num;
}
public int getColumnNumber()
{
int num=0;
int line=getlineNumber();
//得到光标当前位置
int a=pad.jta.getCaretPosition();
int b=0;
try {
b=pad.jta.getLineStartOffset(getlineNumber()-1);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
num=a-b+1;
return num;
}
public void Fonts()
{
MyFont myFont=new MyFont(pad);
myFont.setVisible(true);
if(pad.jta.getFont()!=myFont.getFont()){
pad.jta.setFont(myFont.getFont());
}
}
public void Search()
{
this.mSearch=new MySearch();
Thread thread=new Thread(new MyPanel());
thread.start();
}
public void Replace()
{
this.mReplace=new MyReplace();
Thread thread=new Thread(new MyPane());
thread.start();
}

- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
