name = file.getName();
Demo.this.setTitle( name +" - 记事本");
read(text,file);
}
/*********************************************MAIN**************************************************/
public static void main(String[] args) {
new Demo();
}
private File chooseFile(){
JFileChooser chooser = new JFileChooser(); //构建文件选择器
chooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "文本文件";
}
@Override
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isDirectory() || name.endsWith(".txt")
||name.endsWith(".c") || name.endsWith(".java")
||name.endsWith(".cpp");//可识别文件
}
});
int result = chooser.showDialog(null, "确定");
if (result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
} else {
System.out.println("未选择文件");
}
return chooser.getSelectedFile();
}
public static void read(JTextArea text,File file){//定义读取文件操作
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String string = null;
while((string = br.readLine()) != null){
text.append(string+"\n");
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String txt,String fileName){
FileWriter fw;
try {
fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txt);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TextAreaMenu extends JTextArea implements MouseListener {
private static final long serialVersionUID = -2308615404205560110L;
private JPopupMenu pop = null; // 弹出菜单
private JMenuItem selectAll = null,copy = null, paste = null, cut = null, cancel=null; // 功能菜单
public TextAreaMenu() {
super();
init();
}
private void init() {
this.addMouseListener(this);
this.setSelectedTextColor(Color.red);
pop = new JPopupMenu();
pop.add(selectAll = new JMenuItem("全选"));
pop.add(copy = new JMenuItem("复制"));
pop.add(paste = new JMenuItem("粘贴"));
pop.add(cut = new JMenuItem("剪切"));
pop.add(cancel = new JMenuItem("撤销"));
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK));
copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
cancel.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK));
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
this.add(pop);
}
/**
* 菜单动作
* @param e
*/
public void action(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals(selectAll.getText())) { // 全选
this.selectAll();
}
else if (str.equals(copy.getText())) { // 复制
this.copy();
} else if (str.equals(paste.getText())) { // 粘贴
this.paste();
} else if (str.equals(cut.getText())) { // 剪切
推荐阅读
- 龙港市委托代理记账平台,龙港代理记账公司
- 学校怎么创建微信视频号,学校视频号如何吸引
- linux日志命令,linux日志操作
- 怎么更改硬盘的mbr格式,如何更改硬盘格式mbr
- PHP能不能读串口数据 php串口通信编程问题
- 父亲的游戏推荐安卓版,父亲的游戏推荐安卓版
- 打猎模拟游戏,打猎模拟游戏有哪些
- linux命令rpm linux命令行大全
- 路由器被网络攻击了怎么办,家庭路由器被攻击会有什么影响