求一个用JAVA写的简单的记事本源代码程序

供稿:hz-xin.com     日期:2024-05-04
求一个用JAVA写的简单的记事本源代码程序 要有运行结果的截图和源代码,在线等

这个是我以前写着玩的一个例子,可以运行起来看看,有保存,撤销,复制,剪切功能,自己研究研究


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();
}
}

importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.InputEvent;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.BorderFactory;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPopupMenu;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.KeyStroke;importjavax.swing.ScrollPaneConstants;importjavax.swing.SwingConstants;publicclassJNotePadUIextendsJFrame{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;privateJFileChooserfileChooser;privateJPopupMenupopUpMenu;publicJNotePadUI(){super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);}privatevoidsetUpUIComponent(){setSize(640,480);//菜单栏JMenuBarmenuBar=newJMenuBar();//设置「文件」菜单JMenufileMenu=newJMenu("文件");menuOpen=newJMenuItem("打开");//快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem("保存");menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem("另存为");menuClose=newJMenuItem("关闭");menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔线fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔线fileMenu.add(menuClose);//设置「编辑」菜单JMenueditMenu=newJMenu("编辑");menuCut=newJMenuItem("剪切");menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem("复制");menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem("粘贴");menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);//设置「关于」菜单JMenuaboutMenu=newJMenu("关于");menuAbout=newJMenuItem("关于JNotePad");aboutMenu.add(menuAbout);menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//文字编辑区域textArea=newJTextArea();textArea.setFont(newFont("宋体",Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//状态栏stateBar=newJLabel("未修改");stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();fileChooser=newJFileChooser();}privatevoidsetUpEventListener(){//按下窗口关闭钮事件处理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜单-打开menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜单-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜单-另存为menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜单-关闭文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜单-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜单-复制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){copy();}});//菜单-粘贴menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//菜单-关于menuAbout.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//显示对话框JOptionPane.showOptionDialog(null,"程序名称:
JNotePad
"+"程序设计:

"+"简介:
一个简单的文字编辑器
"+"可作为验收Java的实现对象
"+"欢迎网友下载研究交流

"+"/","关于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);}});//编辑区键盘事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//编辑区鼠标事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否为保存状态open();//打开}else{//显示对话框intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//确认文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放弃文件保存caseJOptionPane.NO_OPTION:open();break;}}}privatebooleanisCurrentFileSaved(){if(stateBar.getText().equals("未修改")){returnfalse;}else{returntrue;}}privatevoidopen(){//fileChooser是JFileChooser的实例//显示文件选取的对话框intoption=fileChooser.showDialog(null,null);//使用者按下确认键if(option==JFileChooser.APPROVE_OPTION){try{//开启选取的文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//设定文件标题setTitle(fileChooser.getSelectedFile().toString());//清除前一次文件textArea.setText("");//设定状态栏stateBar.setText("未修改");//取得系统相依的换行字符StringlineSeparator=System.getProperty("line.separator");//读取文件并附加至文字编辑区Stringtext;while((text=buf.readLine())!=null){textArea.append(text);textArea.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"开启文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile(){//从标题栏取得文件名称Filefile=newFile(getTitle());//若指定的文件不存在if(!file.exists()){//执行另存为saveFileAs();}else{try{//开启指定的文件BufferedWriterbuf=newBufferedWriter(newFileWriter(file));//将文字编辑区的文字写入文件buf.write(textArea.getText());buf.close();//设定状态栏为未修改stateBar.setText("未修改");}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFileAs(){//显示文件对话框intoption=fileChooser.showSaveDialog(null);//如果确认选取文件if(option==JFileChooser.APPROVE_OPTION){//取得选择的文件Filefile=fileChooser.getSelectedFile();//在标题栏上设定文件名称setTitle(file.toString());try{//建立文件file.createNewFile();//进行文件保存saveFile();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE);}}}privatevoidcloseFile(){//是否已保存文件if(isCurrentFileSaved()){//释放窗口资源,而后关闭程序dispose();}else{intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){caseJOptionPane.YES_OPTION:saveFile();break;caseJOptionPane.NO_OPTION:dispose();}}}privatevoidcut(){textArea.cut();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidcopy(){textArea.copy();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;

class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String [] menus){
for(int i=0;i<menus.length;i++)
add(new Menu(menus[i]));
}
public void addMenuItems(int menuNumber,String[] items){
for(int i=0;i<items.length;i++){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;i<getMenuCount();i++)
for(int j=0;j<getMenu(i).getItemCount();j++)
getMenu(i).getItem(j).addActionListener(al);
}
}

class MyFile{
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory()+"\\"+fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline+'\n');
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}

class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String) content.getTransferData(DataFlavor.stringFlavor);
//DataFlavor.stringFlavor会将剪贴板中的字符串转换成Unicode码形式的String对象。
//DataFlavor类是与存储在剪贴板上的数据的形式有关的类。
}catch(Exception ue){}
return null;
}
}

class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("查找字符串");
private Label lReplace=new Label("替换字符串");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替换");
private TextArea ta;
public MyFindDialog(Frame owner,TextArea ta){
super(owner,"查找",false);
this.ta=ta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.dispose();
}
});
}//构造函数结束
public void showFind(){
setTitle("查找");
setSize(280,60);
setVisible(true);
}
public void showReplace(){
setTitle("查找替换");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=ta.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
int start=ta.getSelectionEnd();
if(start==end) start=0;
for(;start<=end-len;start++){
if(text.substring(start,start+len).equals(str)){
ta.setSelectionStart(start);
ta.setSelectionEnd(start+len);
return;
}
}
//若找不到待查字符串,则将光标置于末尾
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}

public Button getBFind() {
return bFind;
}
private void replace(){
String str=tReplace.getText();
if(ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());
else find();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==bFind)
find();
else if(e.getSource()==bReplace)
replace();
}
}

public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea(); //可编辑的TextArea
private MyFile mf=new MyFile(this);//MyFile对象
private MyClipboard cb=new MyClipboard();
private MyFindDialog findDlg=new MyFindDialog(this,editor);

public MyMemo(String title){ //构造函数
super(title);
MyMenuBar mb=new MyMenuBar(this);
//添加需要的菜单及菜单项
mb.addMenus(new String[]{"文件","编辑","查找","帮助"});
mb.addMenuItems(0,new String[]{"新建","打开","保存",null,"全选"});
mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除",null,"全选"});
mb.addMenuItems(2,new String[]{"查找",null,"查找替换"});
mb.addMenuItems(3,new String[]{"我的记事本信息"});

add(editor); //为菜单项注册动作时间监听器
mb.addActionListener(this);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
}); //分号不能忘了
} //构造函数完

public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand(); //获取菜单项标题
if(selected.equals("新建"))
editor.setText("");
else if(selected.equals("打开")){
try{
editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("保存")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("退出")){
dispose();
}
else if(selected.equals("剪贴")){
//将选中的字符串复制到剪贴板中并清除字符串
cb.setData(editor.getSelectedText());
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("复制")){
cb.setData(editor.getSelectedText());
}
else if(selected.equals("粘贴")){
String str=cb.getData();
editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd());
//粘贴在光标位置
}
else if(selected.equals("清除")){
editor.replaceRange("",editor.getSelectionStart(),editor.getSelectionEnd());
}
else if(selected.equals("全选")){
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
}
else if(selected.equals("查找")){
findDlg.showFind();
}
else if(selected.equals("查找替换")){
findDlg.showReplace();
}
}

public static void main(String[] args){
MyMemo memo=new MyMemo("记事本");
memo.setSize(650,450);
memo.setVisible(true);
}
}

我曾今给人回答过这样的问题
http://zhidao.baidu.com/question/99637291.html
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose;

private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;

private JMenuItem menuAbout;

private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;

private JPopupMenu popUpMenu;

public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
}

private void setUpUIComponent() {
setSize(640, 480);

// 菜单栏
JMenuBar menuBar = new JMenuBar();

// 设置「文件」菜单
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打开");
// 快捷键设置
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为");

menuClose = new JMenuItem("关闭");
menuClose.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_Q,
InputEvent.CTRL_MASK));

fileMenu.add(menuOpen);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuClose);

// 设置「编辑」菜单
JMenu editMenu = new JMenu("编辑");
menuCut = new JMenuItem("剪切");
menuCut.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy = new JMenuItem("复制");
menuCopy.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste = new JMenuItem("粘贴");
menuPaste.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
editMenu.add(menuCut);
editMenu.add(menuCopy);
editMenu.add(menuPaste);

// 设置「关于」菜单
JMenu aboutMenu = new JMenu("关于");
menuAbout = new JMenuItem("关于JNotePad");
aboutMenu.add(menuAbout);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);

setJMenuBar(menuBar);

// 文字编辑区域
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 16));
textArea.setLineWrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.CENTER);

// 状态栏
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(
BorderFactory.createEtchedBorder());
contentPane.add(stateBar, BorderLayout.SOUTH);

popUpMenu = editMenu.getPopupMenu();
fileChooser = new JFileChooser();
}

private void setUpEventListener() {
// 按下窗口关闭钮事件处理
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeFile();
}
}
);

// 菜单 - 打开
menuOpen.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);

// 菜单 - 保存
menuSave.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
);

// 菜单 - 另存为
menuSaveAs.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
}
);

// 菜单 - 关闭文件
menuClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFile();
}
}
);

// 菜单 - 剪切
menuCut.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
}
);

// 菜单 - 复制
menuCopy.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
}
);

// 菜单 - 粘贴
menuPaste.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
}
);

// 菜单 - 关于
menuAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 显示对话框
JOptionPane.showOptionDialog(null,
"程序名称:\n JNotePad \n" +
"程序设计:\n \n" +
"简介:\n 一个简单的文字编辑器\n" +
" 可作为验收Java的实现对象\n" +
" 欢迎网友下载研究交流\n\n" +
" /",
"关于JNotePad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
}
);

// 编辑区键盘事件
textArea.addKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
processTextArea();
}
}
);

// 编辑区鼠标事件
textArea.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(editMenu, e.getX(), e.getY());
}

public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
}
);
}

private void openFile() {
if(isCurrentFileSaved()) { // 文件是否为保存状态
open(); // 打开
}
else {
// 显示对话框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
// 确认文件保存
case JOptionPane.YES_OPTION:
saveFile(); // 保存文件
break;
// 放弃文件保存
case JOptionPane.NO_OPTION:
open();
break;
}
}
}
private boolean isCurrentFileSaved() {
if(stateBar.getText().equals("未修改")) {
return false;
}
else {
return true;
}
}
private void open() {
// fileChooser 是 JFileChooser 的实例
// 显示文件选取的对话框
int option = fileChooser.showDialog(null, null);

// 使用者按下确认键
if(option == JFileChooser.APPROVE_OPTION) {
try {
// 开启选取的文件
BufferedReader buf =
new BufferedReader(
new FileReader(
fileChooser.getSelectedFile()));

// 设定文件标题
setTitle(fileChooser.getSelectedFile().toString());
// 清除前一次文件
textArea.setText("");
// 设定状态栏
stateBar.setText("未修改");
// 取得系统相依的换行字符
String lineSeparator = System.getProperty("line.separator");
// 读取文件并附加至文字编辑区
String text;
while((text = buf.readLine()) != null) {
textArea.append(text);
textArea.append(lineSeparator);
}

buf.close();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"开启文件失败", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
// 从标题栏取得文件名称
File file = new File(getTitle());

// 若指定的文件不存在
if(!file.exists()) {
// 执行另存为
saveFileAs();
}
else {
try {
// 开启指定的文件
BufferedWriter buf =
new BufferedWriter(
new FileWriter(file));
// 将文字编辑区的文字写入文件
buf.write(textArea.getText());
buf.close();
// 设定状态栏为未修改
stateBar.setText("未修改");
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"写入文件失败", JOptionPane.ERROR_MESSAGE);
}
}
}

private void saveFileAs() {
// 显示文件对话框
int option = fileChooser.showSaveDialog(null);

// 如果确认选取文件
if(option == JFileChooser.APPROVE_OPTION) {
// 取得选择的文件
File file = fileChooser.getSelectedFile();

// 在标题栏上设定文件名称
setTitle(file.toString());

try {
// 建立文件
file.createNewFile();
// 进行文件保存
saveFile();
}
catch(IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"无法建立新文件", JOptionPane.ERROR_MESSAGE);
}
}
}

private void closeFile() {
// 是否已保存文件
if(isCurrentFileSaved()) {
// 释放窗口资源,而后关闭程序
dispose();
}
else {
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);

switch(option) {
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
dispose();
}
}
}

private void cut() {
textArea.cut();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}

private void copy() {
textArea.copy();
popUpMenu.setVisible(false);
}

private void paste() {
textArea.paste();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}

private void processTextArea() {
stateBar.setText("已修改");
}

public static void main(String[] args) {
new JNotePadUI();
}
}

class Test{
public static void main(String[] args){
System.out.println("Hello!");
}
}

记事本的程序太多了,搜一下就出来了

用JAVA编写一个记事本~只要实现以下功能:插入~删除~查找~保存~另存为...
答:import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import ...

用JAVA编个简单的记事本程序
答:ce1.add("华文行楷");ce1.add("华文中宋");ce1.add("华文新魏");ce1.add("华文细黑");ce1.add("宋体");ce1.add("方正姚体"); ce1.add("幼圆");ce1.add("隶书");ce1.add("楷体-GB2312");ce1.add("华文...

求一个用java写的记事本程序,用awt组件
答:import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import ...

如何用记事本编写java程序?
答:1、首先新建一个记事本,然后在其中加入如下的java内容,如下图所示 2、接下来将文件的后缀名修改为java,如下图所示 3、然后打开cmd命令行运用javac命令编译一下java文件,如下图所示 4、最后我们在运用java命令执行一下j...

如何用JAVA程序编写一个记事本
答:JMenuItem about = new JMenuItem("关于记事本(A)"); JFrame th = this; String name; String openedPath = null; boolean opened = false; boolean reworked = false; UndoManager undoManager = new UndoManager(); // 初...

如何用java一步步编出一个记事本程序
答:一下是一个可以运行的,你看看package mySwing;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.awt.color.*;import java.awt.font.*;import javax....

谁帮我用JAVA写一个记事本?
答:mb.addMenuItems(1,new String[]{"剪贴","复制","粘贴","清除","null","全选" }); mb.addMenuItems(2,new String[]{"查找","null","查找替换"}); mb.addMenuItems(3,new String[]{"我的记事本信息"}); add(...

用java编写记事本(输入,打开,保存功能就行)或者计算器(实现加减乘除就...
答:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Calculator extends JFrame implements ActionListener { private JTextField text;StringBuffer sb1 = new StringBuffer(); //参数一 String...

java程序,写一个记事本,有查找和替换的功能
答:import java.io.*;import java.awt.event.*;import javax.swing.*;public class MainClass extends JFrame implements ActionListener{ int width = 500,height = 400;JPanel panel;JMenuBar bar;JMenu fileMenu,editMenu,...

java的一个简单记事本程序设计
答:java的字体Font类、GraphicsEnvironment类编程问题:利用Font类、画图环境类,把系统字体全部抽出来,包括颜色、字体。意思即是:设计一个java程序,通过按钮“字体”、“颜色”设置,可以把里面的文本格式改变,相当一个“记事本”吧!注意:不用...