java基礎教程案例:[54]java記事本代碼3?

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();

}

java基礎教程案例 (共54篇) 上一篇:java記事本代碼2 下一篇:java記事本代碼4

代碼, 案例, 基礎教程, 記事本,
相關問題答案