记事本项目java代码 java记事本程序代码( 二 )


JMenu mnServer = new JMenu("文件(F)");
JMenu mnEdit= new JMenu("编辑(E)");
JMenuItem[] mniServers = new JMenuItem[]{
new JMenuItem("新建(N)"),
new JMenuItem("保存(S)"),
new JMenuItem("打开(O)"),
new JMenuItem("退出(X)"),
};
{
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
mnbMain.add(mnServer);
menuPanel.add(mnbMain);
mnbMain.setBounds(5, 0, 50, 30);
for (int i = 0; imniServers.length; i++) {
mnServer.add(mniServers[i]);
}
mniServers[0].addActionListener(new ActionListener() { //定义"新建"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
new Demo(getLocation().x+15,getLocation().y+5);
}
});
mniServers[1].addActionListener(new ActionListener() { //定义"保存"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToSave();
}
});
mniServers[2].addActionListener(new ActionListener() { //定义"打开"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToOpen();
}
});
mniServers[3].addActionListener(new ActionListener() { //定义"退出"组件操作
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
text.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
}
});
}
public Demo(int x,int y) {
this.setTitle( name +" - 记事本");
this.setBounds(x, y, 600, 400);
this.setLayout(new BorderLayout());
this.add(menuPanel, BorderLayout.NORTH);
this.add(jsp);
jsp.setBounds(5, 30, getWidth()-10, getHeight()-50);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public Demo() {
this(200,200);
}
protected void chooseToSave() {
File file = chooseFile();
if(null==file)return;
if(file.exists()){
int cho = JOptionPane.showConfirmDialog(this, "文件已存在,是否覆盖?");
System.out.println(cho);
if(cho==JOptionPane.OK_OPTION)save(file);
else return;
}
else save(file);
}
private void save(File file) {
name = file.getName();
write(text.getText(),file.getPath());
this.setTitle( name +" - 记事本");
}
protected void chooseToOpen() {
File file = chooseFile();
if(null==file||!file.exists())return;
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);

推荐阅读