?? mainframe.java
字號:
}
});
//菜單——替換
itemReplace.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
replace();
}
});
//菜單——自動換行
itemAutoLine.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeLineAuto();
}
});
//菜單——字體 更改
itemFont.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeFont();
}
});
// 菜單——背景色 更改
itemBackground.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
// 菜單——前景色 更改
/*itemForeground.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
*/
// 菜單——光標色 更改
itemCaretColor.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//菜單——關于
itemAbout.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//
JOptionPane.showOptionDialog(null,
"程序名稱:\n JNotePadEditor \n" +
"程序設計: \n 陳求江\n" +
"簡介:\n 一個簡單的文字編輯器\n " +
" 可作為驗收Java的實現對象\n" +
" 歡迎網友下載研究交流\n\n" +
"http://ecolab.ruc.edu.cn/",
"JNotePadEditor By 2008.12.31",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
});
//菜單——幫助
itemHelp.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
openHelpFile();
}
});
//編輯區域鍵盤事件
areaText.addKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
//System.out.println("Sourse:" + e.getKeyModifiersText(KeyEvent.CTRL_DOWN_MASK));
//System.out.println("KeyChar byte:" + (byte)e.getKeyChar());
byte keyTypedChar = (byte)e.getKeyChar();
//
byte[] ctrlKeys = {6,18};
Arrays.sort(ctrlKeys);
//if(Arrays.binarySearch(ctrlKeys,keyTypedChar) == -1) {
// 說明CTRL+F(6) 或 CTRL+R(18) 鍵未被按下
if(keyTypedChar > 30) {
//說明CTRL類的有效組合 鍵未被按下
stateBarChanged(); //狀態欄 已修改
//updateCurrentColRows(); //更新當前光標所在的列,行
//if(areaText.getText()!="") {
String x = areaText.getText();
//x = x.replace(" |\t", "");
if(!x.equals("")) {
setItemEnabled(true); //菜單——查找等可見
//if(areaText.getCaretPosition()!=0) {
//itemSearch.setEnabled(true);
//}
}
else {
setItemEnabled(false);
}
}
}
});
//編輯區域鼠標事件
areaText.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3){ //右單擊
popUpMenu.show(menuEdit, e.getX(), e.getY());
//popUpMenu.setVisible(true);
}
}
public void mouseClicked(MouseEvent e) {
//返回當前光標位置,這是為了使查找從當前光標開始
textCurrentpos = areaText.getCaretPosition();
if(e.getButton() == MouseEvent.BUTTON1) {//左單擊
popUpMenu.setVisible(false);
//updateCurrentColRows(); //更新當前光標所在的列,行
}
}
});
areaText.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
//int dot = e.getDot();
//int mark = e.getMark();
if(!areaText.getLineWrap()) {
//格式設置為:非自動換行
Rectangle rec;
try {
int caretPos = areaText.getCaretPosition();
rec = areaText.modelToView(caretPos);
int row = (rec.y / rec.height + 1);
int col = 1;
int offset = -1;
if(rec.x != 0) {
offset = areaText.getLineStartOffset(row-1);
//System.out.println("offset:" + offset);
int lastLinePos = offset-row+1;
col = caretPos-lastLinePos;
if(offset == 0)
col +=1;
}
//System.out.println("caretPos:" + caretPos);
//System.out.println("rec:" + rec.toString());
//System.out.println("行號:" + row);
//System.out.println("列號:" + col);
updateRowCols(row,col);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//System.out.println("dot :" + dot);
//System.out.println("mark :" + mark);
}
});
}
private void createInitFile() {
//創建初始文件
//創建無標題的新建文本文件
this.setTitle("新建無標題文本文件"); //
areaText.setText(""); //清空當前JTextArea,回到一個新建的模樣
this.stateBarUnChanged();
}
private void createNewFile() {
if(isCurrentFileSaved()) { //文件是否為保存狀態
createInitFile(); //創建無標題的新建文本文件
}
else {
//顯示確認對話框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
//確認文件保存
case JOptionPane.YES_OPTION: //保存文件
saveFileAs();
createInitFile();
break;
case JOptionPane.NO_OPTION: //放棄文件保存,直接新建一個文件
createInitFile();
break;
case JOptionPane.CANCEL_OPTION: //取消當前操作
break;
}
}
}
private void openFile(){
if(isCurrentFileSaved()) { //文件是否為保存狀態
openNew();
}
else {
//顯示確認對話框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
//確認文件保存
case JOptionPane.YES_OPTION: //保存文件
saveFile();
break;
case JOptionPane.NO_OPTION: //放棄文件保存,打開一個新文件
openNew();
break;
case JOptionPane.CANCEL_OPTION: //取消當前操作
break;
}
}
//BufferedReader br = new BufferedReader(
//new FileReader(file));
}
// 打開一個指定文件路徑的文件
private void openFile(String openFileNm) {
File file = new File(openFileNm);
System.out.println("You chose to open this file: " + openFileNm);
try {
//打開選取的文件
BufferedReader buf = new BufferedReader(
new FileReader(file));
//設置文件標題
setTitle(openFileNm);
//刪除前一次的文件
areaText.setText("");
//設置狀態欄為初始狀態
this.stateBarUnChanged();
//取得系統相依的換行字符
String lineSeparator = System.getProperty("line.separator");
//讀取文件并附加至文字編輯區
String text;
while((text = buf.readLine()) != null) {
areaText.append(text);
areaText.append(lineSeparator);
}
areaText.setCaretPosition(0);
if(areaText.getText()!="") {
setItemEnabled(true);
}
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e.toString(),
"打開文件失敗", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (IOException e){
//TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e.toString(),
"打開文件失敗", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
private void openHelpFile(){
openFile("help.txt");
}
private void saveFile(){
String fileNm = this.getTitle();
//從標題欄取得文件名稱
File file = new File(fileNm);
//若指定的文件不存在
if(!file.exists()) {
//執行另存為...
saveFileAs();
}
else {
try {
BufferedWriter buf = new BufferedWriter(
new FileWriter(file));
buf.write(areaText.getText());
buf.flush();
buf.close();
//設置狀態欄為:“未修改”
this.stateBarUnChanged();
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e.toString(),
"保存文件失敗", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
//保存字體
void saveFont() {
if(fontChanged) {
//如果字體設置被改動,則需要保存改動至外存中
try {
BufferedWriter buf = new BufferedWriter(
new FileWriter(fileFontSet,false));
//buf.write(selectedFont);
//buf.write(selectedStyle);
//buf.write(selectedSize);
String fontStr = ""+selectedFont+"\t"+selectedStyle+"\t"+selectedSize;
buf.write(fontStr);
buf.flush();
buf.close();
//設置狀態欄為:“未修改”
//this.stateBarUnChanged();
} catch (IOException e) {
// TODO Auto-generated catch block
//JOptionPane.showMessageDialog(null, e.toString(),
//"保存文件失敗", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
private void saveFileAs(){
//顯示文件對話框
try{
//JFileChooser filechooser = new JFileChooser();
int option = chooser.showSaveDialog(this);
//int option = chooser.showDialog(null, null);
//如果確認選取文件
if(option == JFileChooser.APPROVE_OPTION) {
//取得選擇的文件
File file = chooser.getSelectedFile();
//在標題欄上設置文件名稱
this.setTitle(file.toString());
try {
//建立新文件
file.createNewFile();
//進行文件保存
saveFile();
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e.toString(),
"新建文件失敗", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}catch(NullPointerException e) {
System.out.println("exception : " + e.getMessage());
e.printStackTrace();
}
}
private void closeFile(){
//是否已保存文件
if(isCurrentFileSaved()) {
//釋放窗口資源, 而后關閉程序
this.dispose();
}
else {
int option = JOptionPane.showConfirmDialog(null, "文件已修改,是否保存?",
"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE, null);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -