📄 myfileguitest.java
字号:
package com.last;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyFileGUITest extends Frame implements ActionListener{
/**
* @param args
*/
TextArea ta = new TextArea();
MenuBar mb;
Menu menu,edit;
MenuItem open,save,find;
MyFile mf = new MyFile(this);
MyFindDialog mfd = new MyFindDialog(this,ta);
public MyFileGUITest(){
this.mb = new MenuBar();
this.menu = new Menu("file");
this.edit = new Menu("edit");
this.open = new MenuItem("open");
this.open.addActionListener(this);
this.menu.add(open);
this.save = new MenuItem("save");
this.save.addActionListener(this);
this.menu.add(save);
this.find = new MenuItem("find");
this.find.addActionListener(this);
this.edit.add(find);
this.mb.add(this.menu);
this.mb.add(this.edit);
this.setMenuBar(mb);
//this.ta = new TextArea();
this.add(this.ta);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setBounds(300,300,500,400);
this.setVisible(true);
}
public static void main(String[] args) {
new MyFileGUITest();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == open){
this.ta.setText(this.mf.getData());
}
else if(e.getSource() == save){
this.mf.setData(this.ta.getText());
}
else{
this.mfd.showDialog();
}
}
}
class MyFile{
FileDialog fd;
public MyFile(Frame parent){
this.fd = new FileDialog(parent,"open",FileDialog.LOAD);
}
public String getPath(){
return this.fd.getDirectory() +"\\"+ this.fd.getFile();
}
public String getData(){
this.fd.setMode(FileDialog.LOAD);
//this.fd.setBounds(400,400,300,300);
this.fd.setVisible(true);
try {
BufferedReader br = new BufferedReader(new FileReader(this.getPath()));
String line = br.readLine();
StringBuffer st = new StringBuffer();
while(line != null){
st.append(line +"\n");
line = br.readLine();
}
br.close();
return st.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public void setData(String data){
this.fd.setMode(FileDialog.SAVE);
this.fd.setVisible(true);
try {
PrintWriter pw = new PrintWriter(new FileWriter(this.getPath()));
pw.write(data);
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyFindDialog extends Dialog implements ActionListener{
Label lFind;
TextField find;
Button bFind;
TextArea ta;
public MyFindDialog(Frame owner, TextArea ta){
super(owner,"find",false);
this.ta = ta;
this.setLayout(new FlowLayout());
this.lFind = new Label("查找内容: ");
this.find = new TextField(20);
this.bFind = new Button("查找");
this.bFind.addActionListener(this);
this.add(this.lFind);
this.add(this.find);
this.add(this.bFind);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.setVisible(false);
}
});
}
public void showDialog(){
this.setBounds(400,400,400,150);
this.setVisible(true);
}
public void find(){
String text = this.ta.getText();
String str = this.find.getText();
int end = text.length();
int len = str.length();
int start = this.ta.getSelectionEnd();
if(start == end){
start = 0;
}
for( ; start<=end-len; start++){
if(text.substring(start,start+len).equals(str)){
this.ta.setSelectionStart(start);
this.ta.setSelectionEnd(start+len);
return;
}
}
this.ta.setSelectionStart(end);
this.ta.setSelectionEnd(end);
}
public void actionPerformed(ActionEvent e) {
this.find();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -