?? dreamtimesnotepad.java
字號(hào):
case JOptionPane.NO_OPTION :
openFile();
break;
case JOptionPane.YES_OPTION :
saveNewFile();
openFile();
break;
default:
break;
}
}
else {
switch(option) {
case JOptionPane.NO_OPTION :
openFile();
break;
case JOptionPane.YES_OPTION :
saveFile();
openFile();
break;
default:
break;
}
}
}
else {
openFile();
}
}
void saveNewFile() { //保存新文件
OutputStreamWriter osw;
int pos = 0, t = 0;
int rv = fc.showSaveDialog(DreamTimesNotePad.this);//保存文件對(duì)話框
if (rv == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
fns = file.getName();
if(file != null) {
try {
osw = new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(file)));
String str = text.getText();
while(true){
pos = str.indexOf('\12', pos);
if(pos == -1) break;
str = str.substring(0, pos) + '\15' + str.substring(pos);
pos = pos + 2;
}
osw.write(str, 0, str.length());
osw.close();
} catch(IOException e) { }
}
f.setTitle("夢(mèng)想年華記事本 - [" + file.getName() + "]");
undo.discardAllEdits();
Undo.setEnabled(false);
statusFile.setText("已保存!");
}
}
void saveFile() { //保存已打開的文件
OutputStreamWriter osw;
int pos = 0, t = 0;
if(file != null) {
try {
osw = new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(file)));
String str = text.getText();
while(true){
pos = str.indexOf('\12', pos);
if(pos == -1) break;
str = str.substring(0, pos) + '\15' + str.substring(pos);
pos = pos + 2;
}
osw.write(str, 0, str.length());
osw.close();
} catch(IOException e) { }
}
f.setTitle("夢(mèng)想年華記事本 - [" + file.getName() + "]");
fns = file.getName();
undo.discardAllEdits();
Undo.setEnabled(false);
statusFile.setText("已保存!");
}
void exit(){
if(statusFile.getText().endsWith(" * ")) {
int option = JOptionPane.showConfirmDialog(null,
"文件 "+fns+" 文字已經(jīng)被修改! \n 你是否要保存該文件?", "警告!",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
switch(option) {
case JOptionPane.NO_OPTION :
System.exit(0);
break;
case JOptionPane.YES_OPTION :
if(fns == "新文件") {
saveNewFile();
System.exit(0);
}
else {
saveFile();
System.exit(0);
}
break;
default:
break;
}
}
else {
System.exit(0);
}
}
void print(){ //打印
//獲取打印服務(wù)對(duì)象
PrintJob pjob = tk.getPrintJob(DreamTimesNotePad.this,
file.getName(),null,null);
Graphics p = pjob.getGraphics(); //獲取打印內(nèi)容
text.printAll(p); //調(diào)用printAll()方法實(shí)現(xiàn)打印
p.dispose(); //關(guān)閉打印窗口
pjob.end(); //打印結(jié)束
}
void setundo(){ //撤消
if(undo.canUndo()) {
try {
undo.undo();
} catch(CannotUndoException cue) {
System.out.println("Unable to undo: " + cue);
cue.printStackTrace();
}
if(!undo.canUndo())
Undo.setEnabled(false);
}
}
void delete(){ //刪除
//用空格代替選定的文本實(shí)現(xiàn)刪除
text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd());
}
void find(){
JDialog ds = new JDialog(this, "查找", true);
ds.getContentPane().setLayout(new FlowLayout());
ds.setResizable(false);
final JLabel dsMessage1 = new JLabel(" 總數(shù): ");
final JLabel dsMessage2 = new JLabel(" 0");
final Checkbox dsLoop = new Checkbox("循環(huán) ");
dsLoop.setState(findingLoop);
final Checkbox dsMatchCase = new Checkbox("大小寫匹配 ");
final TextField tfs = new TextField(15);
ds.getContentPane().add(tfs);
tfs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = text.getText(); //得到文本域內(nèi)容
str2 = str1.toLowerCase(); //轉(zhuǎn)化為小寫
str3 = tfs.getText(); //得到查找文本框的內(nèi)容
str4 = str3.toLowerCase(); //轉(zhuǎn)化為小寫
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos); //得到起始位置
if(a > -1) {
text.setCaretPosition(a);
b = tfs.getText().length(); //得到查找字符串長(zhǎng)度
text.select(a, a + b); //選擇查找到的字符串
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "已到文件結(jié)尾!",
"Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "已到文件結(jié)尾!",
"Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bs = new Button(" 查找 ");
bs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = text.getText(); //得到文本域內(nèi)容
str2 = str1.toLowerCase(); //轉(zhuǎn)化為小寫
str3 = tfs.getText(); //得到查找文本框的內(nèi)容
str4 = str3.toLowerCase(); //轉(zhuǎn)化為小寫
if(dsMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos); //得到起始位置
if(a > -1) {
text.setCaretPosition(a);
b = tfs.getText().length(); //得到查找字符串長(zhǎng)度
text.select(a, a + b); //選擇查找到的字符串
FindStartPos = a + b;
foundCount++;
dsMessage2.setText(foundCount + "");
}
else {
if(dsLoop.getState()) {
JOptionPane.showMessageDialog(null, "已到文件結(jié)尾!",
"Find result", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else {
JOptionPane.showMessageDialog(null, "已到文件結(jié)尾!",
"Find result", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bsc = new Button("Cancel");
bsc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
foundCount = 0;
}
});
ds.getContentPane().add(bs);
ds.getContentPane().add(bsc);
ds.getContentPane().add(dsLoop);
ds.getContentPane().add(dsMatchCase);
ds.getContentPane().add(dsMessage1);
ds.getContentPane().add(dsMessage2);
ds.setLocation(120, 120);
ds.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
ds.setSize(300,110);
Dimension dsSize = ds.getSize(); //使窗體居中
int x3 = (sSize.width - dsSize.width)/2;
int y3 = (sSize.height - dsSize.height)/2;
ds.setLocation(x3, y3);
ds.setVisible(true);
}
void fontcolor() {
//使用顏色對(duì)話框來設(shè)置前景色(字體顏色)
//用text.getForeground()得到當(dāng)前文本域中使用的前景色
fcolor = JColorChooser.showDialog(this,"設(shè)置字體顏色",text.getForeground());
if (fcolor != null) {
text.setForeground(fcolor); //設(shè)置文本顏色為選定的顏色
}
this.repaint(); //重繪界面(該語句可無)
}
void backcolor() {
//使用顏色對(duì)話框來設(shè)置背景顏色
//用text.getBackground()得到當(dāng)前文本域中使用的背景色
bcolor = JColorChooser.showDialog(this,"設(shè)置背景顏色",text.getBackground());
if (bcolor != null) {
text.setBackground(bcolor); //設(shè)置背景顏色為選定的顏色
}
this.repaint(); //重繪界面(該語句可無)
}
void about() { //關(guān)于對(duì)話框
//創(chuàng)建關(guān)于對(duì)話框并設(shè)定標(biāo)題
JDialog about = new JDialog(this, "歡迎您使用夢(mèng)想年華記事本", true);
//設(shè)定對(duì)話框的布局
about.getContentPane().setLayout(new BorderLayout());
//創(chuàng)建一個(gè)按鈕用來關(guān)閉對(duì)話框
JButton Ok = new JButton(new ImageIcon("images/ok.gif"));
Ok.setMnemonic(KeyEvent.VK_F4); //設(shè)置按鈕快捷鍵
Ok.addActionListener(new ActionListener() { //關(guān)閉窗口
public void actionPerformed(ActionEvent Oke) {
dispose();
}
});
//創(chuàng)建圖片標(biāo)簽,顯示在關(guān)于對(duì)話框中
JLabel top1 = new JLabel(Icontop1);
//在窗口中加入標(biāo)簽和按鈕并指定布局
about.getContentPane().add("North",top1);
about.getContentPane().add("South",Ok);
Ok.addActionListener(this);
about.addWindowListener(new WindowAdapter() { //關(guān)閉窗口
public void windowClosing(WindowEvent dle) {
dispose();
}
});
about.setSize(400, 400); //設(shè)置窗體大小
//以下語句使窗體居中顯示
Dimension aboutSize = about.getSize();
int x2 = (sSize.width - aboutSize.width)/2;
int y2 = (sSize.height - aboutSize.height)/2;
about.setLocation(x2, y2);
about.setResizable(false); //設(shè)置窗體大小不可改變
about.setVisible(true); //顯示窗體
}
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee) {
undo.addEdit(uee.getEdit());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -