java记事本开发源代码 java简单记事本源代码( 三 )


import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L; //Eclipse自动生成序列号
String name = "无标题";
JPanel menuPanel = new JPanel();
JTextArea text = new TextAreaMenu();//文本编辑区
JScrollPane jsp = new JScrollPane(text);//可滚动编辑区
JMenuBar mnbMain = new JMenuBar();
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, "文件已存在java记事本开发源代码,是否覆盖java记事本开发源代码?");
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;

推荐阅读