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;
推荐阅读
- 龙港市委托代理记账平台,龙港代理记账公司
- 学校怎么创建微信视频号,学校视频号如何吸引
- linux日志命令,linux日志操作
- 怎么更改硬盘的mbr格式,如何更改硬盘格式mbr
- PHP能不能读串口数据 php串口通信编程问题
- 父亲的游戏推荐安卓版,父亲的游戏推荐安卓版
- 打猎模拟游戏,打猎模拟游戏有哪些
- linux命令rpm linux命令行大全
- 路由器被网络攻击了怎么办,家庭路由器被攻击会有什么影响