import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class TestTxt extends JFrame{
File file = null;
JTextPane text = new JTextPane();
static JMenuBar menubar = new JMenuBar();
JFileChooser filechooser = new JFileChooser();
void inTextPane() {
getContentPane().add(new JScrollPane(text));
}
JMenu[] menus = new JMenu[] {
new JMenu("文件"),new JMenu("编辑"),new JMenu("帮助")
};
JMenuItem menuitems[][] = new JMenuItem[][] {
{
new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")
},
{
new JMenuItem("复制"),new JMenuItem("剪切"),new JMenuItem("粘贴")
},
{
new JMenuItem("关于")
}
};
void inMenu() {
for (int i = 0;i < menus.length; i ++) {
menubar.add(menus[i]);
for(int j = 0; j < menuitems[i].length; j ++) {
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener(action);
}
}
//this.setJMenuBar(menubar);
}
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuItem mi = (JMenuItem)e.getSource();
String id = mi.getText();
if(id.equals("新建")) {
text.setText("sorry!此功能尚不支持!");
file = null;
}else if(id.equals("打开")) {
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
openFile();
}
}else if(id.equals("保存")) {
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
saveFile();
}
}else if(id.equals("退出")) {
TestTxt txt = new TestTxt();
int s = JOptionPane.showConfirmDialog(txt, "你真的要退出吗?");
if(s ==JOptionPane.YES_OPTION)
System.exit(0);
}else if(id.equals("剪切")) {
text.cut();
}else if(id.equals("复制")) {
text.copy();
}else if(id.equals("粘贴")) {
text.paste();
}else if(id.equals("关于")) {
TestTxt txt = new TestTxt();
JDialog dialog = new JDialog(txt,"help");
dialog.getContentPane().add(new JLabel(" 测试版本(by_xgc)"));
dialog.setSize(200,150);
dialog.setVisible(true);
}
}
private void saveFile() {
// TODO 自动生成方法存根
try {
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
}catch(Exception e) {
e.printStackTrace();
}
}
private void openFile() {
try {
FileReader fr = new FileReader(file);
int len = (int)file.length();
char[] mu = new char[len];
fr.read(mu, 0, len);
fr.close();
text.setText(new String(mu));
}catch(Exception e) {
e.printStackTrace();
}
}
};
TestTxt() {
inTextPane();
inMenu();
}
public static void main(String[] args) {
TestTxt txt = new TestTxt();
//txt.setLayout(new FlowLayout());
txt.setVisible(true);
txt.setSize(800,600);
txt.setTitle("记事本");
txt.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestTxt txt = new TestTxt();
int s = JOptionPane.showConfirmDialog(txt, "你真的要结束吗?");
if (s == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);
txt.setJMenuBar(menubar);
txt.show();
}
}
java简单记事本
需积分: 0 38 浏览量
2008-05-03
15:33:53
上传
评论 1
收藏 97KB RAR 举报
dibayinfu
- 粉丝: 0
- 资源: 22
最新资源
- 基于matlab实现字符识别的参考程序,用所用的字体识别率挺高,部分手写的也能识别,但是还不能识别扭曲和倾斜的字符 .rar
- 基于matlab实现指纹识别系统 包括数据库导入、图像选择识别、帮助功能、识别图像可视、gabor滤波可视,实现界面操作.rar
- 基于matlab实现指纹识别,利用gabor滤波器,提取指纹特征,只能呢读取bmp图像.rar
- 基于matlab实现于数字图像相关方法中散斑质量评价
- 基于matlab实现遗传算法和佳点集遗传算法的基本思想和不同点,用遗传算法测试一标准函数 佳点集算法测试一标准函数
- tensorflow-gpu-2.5.1-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-gpu-2.5.0-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-gpu-2.5.2-cp39-cp39-manylinux2010-x86-64.whl
- 内含方正小标宋简体、仿宋-Gb2312、黑体、楷体、宋体,五个公文常用字体
- 记忆卡牌游戏源码及可运行文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈