求一个用JAVA写的简单的记事本源代码程序 要有运行结果的截图和源代码,在线等

 我来答
汪雄辉拉
2010-12-31 · TA获得超过2966个赞
知道大有可为答主
回答量:1430
采纳率:0%
帮助的人:1627万
展开全部
这个是我以前写着玩的一个例子,可以运行起来看看,有保存,撤销,复制,剪切功能,自己研究研究

package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
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;

public class MyNote {
private JFrame frame = new JFrame("记事本");
private JTextArea text = new JTextArea();
private static boolean flag = false; // 判断是否保存

public MyNote() {
JMenuBar bar = new JMenuBar();
JMenu edit = new JMenu("check");
JMenu check = new JMenu("edit");
JMenu help = new JMenu("help");
JMenuItem term = new JMenuItem("copy");
JMenuItem term1 = new JMenuItem("paste");
JMenuItem term2 = new JMenuItem("cut");
JMenuItem term3 = new JMenuItem("backout");
JMenuItem term4 = new JMenuItem("import");
JMenuItem term5 = new JMenuItem("open");
JMenuItem term6 = new JMenuItem("exit");
JMenuItem term7 = new JMenuItem("save to");
JMenuItem term8 = new JMenuItem("about");
JMenuItem term9 = new JMenuItem("save");
JMenuItem term10 = new JMenuItem("new");
Font font = new Font(null, JFrame.ABORT, 24);
text.setFont(font);
JScrollPane scroll = new JScrollPane(text);
frame.setJMenuBar(bar);
bar.add(edit);
bar.add(check);
bar.add(help);
edit.add(term7);
edit.add(term4);
edit.add(term5);
edit.add(term6);
check.add(term);
check.add(term1);
check.add(term2);
check.add(term3);
help.add(term8);
check.add(term9);
edit.add(term10);

frame.add(scroll);
text.setVisible(false);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLocation(500, 500);
// 事件注册
MenuListener m = new MenuListener();
term5.addActionListener(m);
NewListener n = new NewListener();
term4.addActionListener(n);
SaveListener s = new SaveListener();
term7.addActionListener(s);
CopyListener c = new CopyListener();
term.addActionListener(c);
PasteListener p = new PasteListener();
term1.addActionListener(p);
CutListener c1 = new CutListener();
term2.addActionListener(c1);
HelpListener h = new HelpListener();
term8.addActionListener(h);
ExitListener e = new ExitListener();
term6.addActionListener(e);
CloseListener c2 = new CloseListener();
frame.addWindowListener(c2);
BackOutListener b = new BackOutListener();
term3.addActionListener(b);
SaveActionListener s1 = new SaveActionListener();
term9.addActionListener(s1);
NewFileListener n1 = new NewFileListener();
term10.addActionListener(n1);
}

private class MenuListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
text.setVisible(true);
}

}

// 打开新文件
private class NewListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
text.setText("");
JFileChooser fileChooser = new JFileChooser();
int r = fileChooser.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
FileReader fr = new FileReader(file);
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.append(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

// 另存为
private class SaveListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
JFileChooser filechoose = new JFileChooser();
int r = filechoose.showSaveDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

// 复制
private class CopyListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
text.copy();
}
}

// 粘贴
private class PasteListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
text.paste();
}
}

// 剪切
private class CutListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
text.cut();
}
}

// 关于主题
private class HelpListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "汪雄辉的无敌记事本");
}
}

private class ExitListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "谢谢!");
try {
Thread.sleep(2000);
System.exit(0);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}

private class CloseListener implements WindowListener {

public void windowActivated(WindowEvent e) {

}

public void windowClosed(WindowEvent e) {
}

// 关闭窗口
public void windowClosing(WindowEvent e) {
StringBuffer sb = new StringBuffer();
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
fr.close();
} catch (Exception e1) {
e1.getStackTrace();
}
String s = sb.toString();
if (flag == false || !(text.getText().equals(s))) {
int r = JOptionPane.showConfirmDialog(frame, "你还没有保存,要保存吗?");
if (r == JOptionPane.OK_OPTION) {
JFileChooser filechoose = new JFileChooser();
int r1 = filechoose.showSaveDialog(frame);
if (r1 == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
System.exit(0);
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
}
} else if (r == JOptionPane.NO_OPTION) {
System.exit(0);
} else {
}
} else
System.exit(0);

}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}
}

// 撤销
private class BackOutListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
if (flag == false)
text.setText("");
else {
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.setText(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.getStackTrace();
}
}
}
}

// 保存文件
private class SaveActionListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
flag = true;
FileWriter writer;
try {
writer = new FileWriter("未命名文件.txt");
writer.write((String) text.getText());
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private class NewFileListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
new MyNote();
}
}

public static void main(String[] args) {
new MyNote();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
aimilin6688
2011-01-14 · TA获得超过1266个赞
知道小有建树答主
回答量:809
采纳率:0%
帮助的人:592万
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
shy2850
2010-12-30 · TA获得超过6773个赞
知道大有可为答主
回答量:2505
采纳率:55%
帮助的人:1567万
展开全部

参考资料: http://hi.baidu.com/shy2850/blog/item/3d15b409797d34dc63d98662.html

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
easysoft_ln
2010-12-30 · TA获得超过1208个赞
知道大有可为答主
回答量:1.3万
采纳率:41%
帮助的人:4662万
展开全部
可以借助百度Hi告诉我
有时间可以解决你的问题
具体的要求也可以告诉我

ES:\\D8428D82B5FD04C6D12A8D3678A20DD8
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式