记下先!
/* 编写一个记事本工具(可模仿WINDOWs附件中记事本)。
* 说明:必须完成的功能:简单编辑功能、菜单(打开、新建、保存、退出);
* */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
public class NotePad extends JFrame {
private static final long serialVersionUID = 1L; //Eclipse自动生成序列号
String name = "无标题";
JTextArea text = new JTextArea(); //文本编辑区
JScrollPane jsp = new JScrollPane(text); //可滚动编辑区
JMenuBar mnbMain = new JMenuBar();
JMenu mnServer = new JMenu("文件(F)");
JMenuItem[] mniServers = new JMenuItem[]{
new JMenuItem("新建(N)"),
new JMenuItem("保存(S)"),
new JMenuItem("打开(O)"),
new JMenuItem("退出(X)"),
};
{
mnbMain.add(mnServer);
for (int i = 0; i < mniServers.length; i++) {
mnServer.add(mniServers[i]);
}
mniServers[0].addActionListener(new ActionListener() { //定义"新建"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
new NotePad(getLocation().x+15,getLocation().y+5);
}
});
mniServers[1].addActionListener(new ActionListener() { //定义"保存"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
while(name.equals("无标题")){
String input = JOptionPane.showInputDialog(mniServers[0],"输入文件名(后缀为txt、c或java)").toLowerCase();
if(input.endsWith(".txt")||input.endsWith(".c")
|| input.endsWith(".java"))
{
name = input;
}
}
write(text.getText(),name);
NotePad.this.setTitle( name +" - 记事本");
JOptionPane.showConfirmDialog(NotePad.this, "文件已经存入C:/Documents and Settings/Administrator/桌面 下");
}
});
mniServers[2].addActionListener(new ActionListener() { //定义"打开"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
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("未选择文件");
}
name = chooser.getSelectedFile().getName();
NotePad.this.setTitle( name +" - 记事本");
read(chooser.getSelectedFile());
}
});
mniServers[3].addActionListener(new ActionListener() { //定义"退出"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
public NotePad() {
this(200,200);
}
public NotePad(int x,int y) {
this.setTitle( name +" - 记事本");
this.setBounds(x, y, 600, 400);
this.setLayout(null);
this.add(mnbMain);
mnbMain.setBounds(5, 0, 50, 30);
this.add(jsp);
jsp.setBounds(5, 30, getWidth()-10, getHeight()-50);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
/*********************************************MAIN**************************************************/
public static void main(String[] args) {
new NotePad();
}
private void read(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();
}
}
private void write(String txt,String fileName){
FileWriter fw;
try {
fw = new FileWriter("C:/Documents and Settings/Administrator/桌面/"+fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txt);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
空间有图片效果
http://hi.baidu.com/shy2850/blog/item/3d15b409797d34dc63d98662.html参考资料:http://hi.baidu.com/shy2850/blog/item/3d15b409797d34dc63d98662.html