| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2895 人关注过本帖
标题:《求助》怎样用JAVA做记事本
只看楼主 加入收藏
qwe
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-6-2
收藏
 问题点数:0 回复次数:11 
《求助》怎样用JAVA做记事本
怎样用JAVA做记事本,急用。。
搜索更多相关主题的帖子: JAVA 记事本 
2006-12-18 20:53
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
本论坛有例子
你找找看

可惜不是你,陪我到最后
2006-12-18 20:59
01yin
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-12-18
收藏
得分:0 
看一天的书应该就可以编好。,再说到处都有这方面的例子
2006-12-18 21:31
qwe
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-6-2
收藏
得分:0 

就是不知道怎样添加格式、字体、颜色、查看等菜单啊
给我一个例子吧。。。多谢!!

2006-12-18 21:59
wyb19850616
Rank: 6Rank: 6
来 自:大连
等 级:贵宾
威 望:29
帖 子:3172
专家分:126
注 册:2006-10-3
收藏
得分:0 

import java.awt.datatransfer.*;//与clipboard有关
import javax.swing.*;//JOptionPane,Swing 用它们来提供自己的可插入外观功能
//在Javax.swing包中,一共有三个菜单子类:JMenuBar,JMenu和JMenuItem类
import javax.swing.event.*;

class NotePad extends JFrame{

private JPanel contentPane;//内容板
private JMenuBar menuBar=new JMenuBar();//菜单栏
private JMenu menuFile=new JMenu();//file菜单
private JMenuItem mItemNew=new JMenuItem();
private JMenuItem mItemOpen=new JMenuItem();
private JMenuItem mItemSave=new JMenuItem();
private JMenuItem mItemAssave=new JMenuItem();
private JMenuItem mItemQuit=new JMenuItem();
private JMenu menuEdit=new JMenu();//编辑菜单
private JMenuItem mItemCut=new JMenuItem();
private JMenuItem mItemCopy=new JMenuItem();
private JMenuItem mItemPaste=new JMenuItem();
private JMenuItem mItemLook=new JMenuItem();
private JMenuItem mItemInstead=new JMenuItem();
private JMenu menuHelp=new JMenu();//帮助菜单
private JMenuItem mItemHelpAbout=new JMenuItem();
private JTextArea textArea=new JTextArea();//文本编辑区
private JScrollPane scroller=new JScrollPane();//滚动条
private Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();

public static final String[] KEYWORDS={//关键字的定义
"abstract","boolean","break","byte", "case", "catch","char",
"class","const", "continue","default", "do", "else", "extends",
"final", "finally", "float", "for", "goto", "if",
"implements", "import", "instanceof", "int", "interface",
"long", "new", "package", "private", "protected","public","return",
"short", "static","super","switch","synchronized",
"this","throw", "throws","try", "void", "volatile","while","true","false"
};

NotePad(){//构造函数
initContainer();//初始化容器
initComponent();//初始化组件
initListener();//添加事件监听器
}

private void initContainer(){//初始化容器
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(textArea,BorderLayout.CENTER);
contentPane.add("Center",scroller);

this.setBounds(100,100,500,500);
this.setFont(new Font("宋体",Font.PLAIN,8));
this.setTitle("无标题-记事本");
this.setMenuBar(menuBar);
}

private void initComponent(){//初始化组件
buildMenuBar();
buildTextArea();
}
private void buildMenuBar(){
initMenuBar();//set MenuBar
initMenuFile();//set MenuFile
initMenuEdit();//set MenuEdit
initMenuHelp();//set MenuHelp
}
private void initMenuBar(){//set MenuBar
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuHelp);
}
private void initMenuFile(){//set MenuFile
menuFile.setText("文件");
menuFile.add(mItemNew);
menuFile.add(mItemOpen);
menuFile.add(mItemSave);
menuFile.addSeparator();
menuFile.add(mItemQuit);

mItemNew.setText("新建N");//set menuFileNew
mItemOpen.setText("打开O");//set menuFileOpen
mItemSave.setText("保存S");//set menuFileSave
mItemAssave.setText("另存为A");
mItemQuit.setText("退出");//set menuFileQuit
}
private void initMenuEdit(){//set MenuEdit
menuEdit.setText("编辑");
menuEdit.add(mItemCut);
menuEdit.add(mItemCopy);
menuEdit.add(mItemPaste);

mItemCut.setText("剪切T");//set mItemCut
mItemCopy.setText("复制C");//set mItemCopy
mItemPaste.setText("粘贴P");//set mItemPaste
mItemLook.setText("查找");
mItemInstead.setText("替换");
}
private void initMenuHelp(){//set MenuHelp
menuHelp.setText("帮助");
menuHelp.add(mItemHelpAbout);
mItemHelpAbout.setText("关于记事本");
}
private void buildTextArea(){//构建文本区
textArea.setRows(20);
textArea.setColumns(20);
textArea.setDoubleBuffered(false);
textArea.setToolTipText("这是一个简单的记事本");
textArea.setText("");
textArea.setVerifyInputWhenFocusTarget(true);

scroller.getViewport().add(textArea);
}

private void initListener(){// 事件监听器
ActListener actlistener=new ActListener();
//Actlistener用来监听处理所有菜单项的事件源事件
mItemNew.addActionListener(actlistener);
mItemOpen.addActionListener(actlistener);
mItemSave.addActionListener(actlistener);
mItemAssave.addActionListener(actlistener);
mItemQuit.addActionListener(actlistener);

mItemCut.addActionListener(actlistener);
mItemCopy.addActionListener(actlistener);
mItemPaste.addActionListener(actlistener);
mItemLook.addActionListener(actlistener);
mItemInstead.addActionListener(actlistener);

mItemHelpAbout.addActionListener(actlistener);

//textArea.addMouseListener(new MouseListener());
//WindowListener处理容器关闭触发相关事件
this.addWindowListener(new WindowListener());
}

class ActListener implements ActionListener{//构建事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==mItemNew){
fileNew();}
else if(e.getSource()==mItemOpen){
fileOpen();}
else if(e.getSource()==mItemSave){
fileSave();}
else if(e.getSource()==mItemAssave){
fileAssave();}
else if(e.getSource()==mItemQuit){
fileQuit();}
else if(e.getSource()==mItemCut){
fileCut();}
else if(e.getSource()==mItemCopy){
fileCopy();}
else if(e.getSource()==mItemPaste){
filePaste();}
else if(e.getSource()==mItemLook){
fileLook();}
else if(e.getSource()==mItemInstead){
fileInstead();}
else if(e.getSource()==mItemHelpAbout){
about();}
}
}
String fileName=null;//处理文件时的文件名
boolean isSaved=false;//处理文件时的是否保存标志
private void fileNew(){//新建文件
if(isSaved){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");}
else {
int result=JOptionPane.showConfirmDialog(this,"do you want to save the file?","NotePad",JOptionPane.YES_NO_CANCEL_OPTION);//Swing offer JOptionPane inserting surface
if(JOptionPane.OK_OPTION==result){
fileAssave();}
else if(JOptionPane.NO_OPTION==result){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");}
else{}
}
}
private void fileOpen(){
//打开文件
String openFileName="";
JFileChooser jFileChooser=new JFileChooser();//?JFileChooser
if(isSaved){
try{
if(JFileChooser.APPROVE_OPTION==jFileChooser.showOpenDialog(this)){
openFileName=jFileChooser.getSelectedFile().getPath();
File file=new File(openFileName);
int flength=(int)file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[] data=new char[flength];
while(fReader.ready()){
num+=fReader.read(data,num,flength-num);}
fReader.close();
textArea.setText(new String (data,0,num));
fileName=openFileName;
this.setTitle(fileName.substring(fileName.lastIndexOf("\\")+1));
this.repaint();
isSaved=false;
}
}catch(Exception e){JOptionPane.showMessageDialog(this,"error in OPen file ","error",JOptionPane.ERROR_MESSAGE);}
}
else{
int result=JOptionPane.showConfirmDialog(this,"Do you want to save the file ?","NotePad",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.YES_OPTION==result){
fileSave();
fileOpen();}
else if(JOptionPane.NO_OPTION==result){
isSaved=true;
fileOpen();}
else{}
}
}
private void fileSave(){
//保存文件
if(fileName==null){
fileAssave();}
else{
if(!isSaved){
if(fileName.length()!=0){
try{
File saveFile=new File(fileName);
FileWriter fw=new FileWriter(saveFile);
fw.write(textArea.getText());//has not actived
fw.close();
isSaved=true;
this.setTitle(fileName.substring(fileName.lastIndexOf("\\")+1));
this.repaint();//this function ?
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in file saveing !","error",JOptionPane.ERROR_MESSAGE);}
}
else{fileAssave();}
}
}
}
private void fileAssave(){
//assave
JFileChooser jFileChooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this)){
fileName=jFileChooser.getSelectedFile().getPath();
fileSave();}
}
private void fileQuit(){
//quit
dispose();
System.exit(0);
}
private void fileCut(){
//cut
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);//cb is Chipboard
this.textArea.replaceRange("",this.textArea.getSelectionStart(),this.textArea.getSelectionEnd());
isSaved=false;}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in cutting !","error",JOptionPane.ERROR_MESSAGE);}
}
private void fileCopy(){
//Copy
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in copying ! ","error",JOptionPane.ERROR_MESSAGE);}
}
private void filePaste(){
//Paste
try{
Transferable tr=cb.getContents(this);
if(tr!=null){
String s="";
textArea.replaceRange(s,textArea.getSelectionStart(),textArea.getSelectionEnd());}
else{
isSaved=false;}
}catch(Exception err){//what different is between err and e
JOptionPane.showMessageDialog(this,"error in pasting !","error",JOptionPane.ERROR_MESSAGE); }
}
private void fileLook(){
//Look
}
private void fileInstead(){
//Instead
}
private void about(){
JOptionPane.showMessageDialog(this,"这是一个简单的记事本","记事本",JOptionPane.INFORMATION_MESSAGE);
}

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}});//适配器的使用



public void showNote(){//显示方法
this.setVisible(true);
this.validate();
}

public static void main(String args[]){//main方法运行测试程序
NotePad Note=new NotePad();
Note.showNote();
}
}


多年以后我就会很老了  腰间那柄玄铁剑也换成了木剑 我拖着它浪迹天涯    我一生打败了无数江湖豪客    然而   却打不败逝者如斯的时光和对你无尽的思念
2006-12-18 22:02
WestNet
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2006-10-31
收藏
得分:0 

楼上的 我怎么编译不了你这个NotePad?有两个错误


不灰心等待,痛苦也忍耐。
2006-12-18 23:12
qwe
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-6-2
收藏
得分:0 

是啊,有两个错误啊!由于我是JAVA新手,所以不懂得改啊。
有没有这个程序的详细文档啊?要注明用法的啊。。。我想看看啊。。。。

2006-12-19 09:26
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

JAVA自带的DEMO中有

你可以研究一下


可惜不是你,陪我到最后
2006-12-19 09:29
白展堂
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-1-2
收藏
得分:0 
究竟是哪错了呀!


2007-01-18 21:10
禹_二
Rank: 1
等 级:新手上路
帖 子:661
专家分:0
注 册:2006-9-13
收藏
得分:0 

package p1;
import java.awt.datatransfer.*;//与clipboard有关
import javax.swing.*;//JOptionPane,Swing 用它们来提供自己的可插入外观功能
//在Javax.swing包中,一共有三个菜单子类:JMenuBar,JMenu和JMenuItem类
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

class NotePad extends JFrame {

private JPanel contentPane;//内容板
private JMenuBar menuBar=new JMenuBar();//菜单栏
private JMenu menuFile=new JMenu();//file菜单
private JMenuItem mItemNew=new JMenuItem();
private JMenuItem mItemOpen=new JMenuItem();
private JMenuItem mItemSave=new JMenuItem();
private JMenuItem mItemAssave=new JMenuItem();
private JMenuItem mItemQuit=new JMenuItem();
private JMenu menuEdit=new JMenu();//编辑菜单
private JMenuItem mItemCut=new JMenuItem();
private JMenuItem mItemCopy=new JMenuItem();
private JMenuItem mItemPaste=new JMenuItem();
private JMenuItem mItemLook=new JMenuItem();
private JMenuItem mItemInstead=new JMenuItem();
private JMenu menuHelp=new JMenu();//帮助菜单
private JMenuItem mItemHelpAbout=new JMenuItem();
private JTextArea textArea=new JTextArea();//文本编辑区
private JScrollPane scroller=new JScrollPane();//滚动条
private Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();

public static final String[] KEYWORDS={//关键字的定义
"abstract","boolean","break","byte", "case", "catch","char",
"class","const", "continue","default", "do", "else", "extends",
"final", "finally", "float", "for", "goto", "if",
"implements", "import", "instanceof", "int", "interface",
"long", "new", "package", "private", "protected","public","return",
"short", "static","super","switch","synchronized",
"this","throw", "throws","try", "void", "volatile","while","true","false"
};

NotePad(){//构造函数
initContainer();//初始化容器
initComponent();//初始化组件
initListener();//添加事件监听器
}

private void initContainer(){//初始化容器
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(textArea,BorderLayout.CENTER);
contentPane.add("Center",scroller);

this.setBounds(100,100,500,500);
this.setFont(new Font("宋体",Font.PLAIN,8));
this.setTitle("无标题-记事本");
this.setJMenuBar(menuBar);
}

private void initComponent(){//初始化组件
buildMenuBar();
buildTextArea();
}
private void buildMenuBar(){
initMenuBar();//set MenuBar
initMenuFile();//set MenuFile
initMenuEdit();//set MenuEdit
initMenuHelp();//set MenuHelp
}
private void initMenuBar(){//set MenuBar
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuHelp);
}
private void initMenuFile(){//set MenuFile
menuFile.setText("文件");
menuFile.add(mItemNew);
menuFile.add(mItemOpen);
menuFile.add(mItemSave);
menuFile.addSeparator();
menuFile.add(mItemQuit);

mItemNew.setText("新建N");//set menuFileNew
mItemOpen.setText("打开O");//set menuFileOpen
mItemSave.setText("保存S");//set menuFileSave
mItemAssave.setText("另存为A");
mItemQuit.setText("退出");//set menuFileQuit
}
private void initMenuEdit(){//set MenuEdit
menuEdit.setText("编辑");
menuEdit.add(mItemCut);
menuEdit.add(mItemCopy);
menuEdit.add(mItemPaste);

mItemCut.setText("剪切T");//set mItemCut
mItemCopy.setText("复制C");//set mItemCopy
mItemPaste.setText("粘贴P");//set mItemPaste
mItemLook.setText("查找");
mItemInstead.setText("替换");
}
private void initMenuHelp(){//set MenuHelp
menuHelp.setText("帮助");
menuHelp.add(mItemHelpAbout);
mItemHelpAbout.setText("关于记事本");
}
private void buildTextArea(){//构建文本区
textArea.setRows(20);
textArea.setColumns(20);
textArea.setDoubleBuffered(false);
textArea.setToolTipText("这是一个简单的记事本");
textArea.setText("");
textArea.setVerifyInputWhenFocusTarget(true);

scroller.getViewport().add(textArea);
}

private void initListener(){// 事件监听器
ActListener actlistener=new ActListener();
//Actlistener用来监听处理所有菜单项的事件源事件
mItemNew.addActionListener(actlistener);
mItemOpen.addActionListener(actlistener);
mItemSave.addActionListener(actlistener);
mItemAssave.addActionListener(actlistener);
mItemQuit.addActionListener(actlistener);

mItemCut.addActionListener(actlistener);
mItemCopy.addActionListener(actlistener);
mItemPaste.addActionListener(actlistener);
mItemLook.addActionListener(actlistener);
mItemInstead.addActionListener(actlistener);

mItemHelpAbout.addActionListener(actlistener);

//textArea.addMouseListener(new MouseListener());
//WindowListener处理容器关闭触发相关事件
// this.addWindowListener(new WindowListener());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}});
}

class ActListener implements ActionListener{//构建事件监听器
public void actionPerformed(ActionEvent e){
if(e.getSource()==mItemNew){
fileNew();}
else if(e.getSource()==mItemOpen){
fileOpen();}
else if(e.getSource()==mItemSave){
fileSave();}
else if(e.getSource()==mItemAssave){
fileAssave();}
else if(e.getSource()==mItemQuit){
fileQuit();}
else if(e.getSource()==mItemCut){
fileCut();}
else if(e.getSource()==mItemCopy){
fileCopy();}
else if(e.getSource()==mItemPaste){
filePaste();}
else if(e.getSource()==mItemLook){
fileLook();}
else if(e.getSource()==mItemInstead){
fileInstead();}
else if(e.getSource()==mItemHelpAbout){
about();}
}
}
String fileName=null;//处理文件时的文件名
boolean isSaved=false;//处理文件时的是否保存标志
private void fileNew(){//新建文件
if(isSaved){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");}
else {
int result=JOptionPane.showConfirmDialog(this,"do you want to save the file?","NotePad",JOptionPane.YES_NO_CANCEL_OPTION);//Swing offer JOptionPane inserting surface
if(JOptionPane.OK_OPTION==result){
fileAssave();}
else if(JOptionPane.NO_OPTION==result){
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("无标题-记事本");}
else{}
}
}
private void fileOpen(){
//打开文件
String openFileName="";
JFileChooser jFileChooser=new JFileChooser();//?JFileChooser
if(isSaved){
try{
if(JFileChooser.APPROVE_OPTION==jFileChooser.showOpenDialog(this)){
openFileName=jFileChooser.getSelectedFile().getPath();
File file=new File(openFileName);
int flength=(int)file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[] data=new char[flength];
while(fReader.ready()){
num+=fReader.read(data,num,flength-num);}
fReader.close();
textArea.setText(new String (data,0,num));
fileName=openFileName;
this.setTitle(fileName.substring(fileName.lastIndexOf("\\")+1));
this.repaint();
isSaved=false;
}
}catch(Exception e){JOptionPane.showMessageDialog(this,"error in OPen file ","error",JOptionPane.ERROR_MESSAGE);}
}
else{
int result=JOptionPane.showConfirmDialog(this,"Do you want to save the file ?","NotePad",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.YES_OPTION==result){
fileSave();
fileOpen();}
else if(JOptionPane.NO_OPTION==result){
isSaved=true;
fileOpen();}
else{}
}
}
private void fileSave(){
//保存文件
if(fileName==null){
fileAssave();}
else{
if(!isSaved){
if(fileName.length()!=0){
try{
File saveFile=new File(fileName);
FileWriter fw=new FileWriter(saveFile);
fw.write(textArea.getText());//has not actived
fw.close();
isSaved=true;
this.setTitle(fileName.substring(fileName.lastIndexOf("\\")+1));
this.repaint();//this function ?
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in file saveing !","error",JOptionPane.ERROR_MESSAGE);}
}
else{fileAssave();}
}
}
}
private void fileAssave(){
//assave
JFileChooser jFileChooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this)){
fileName=jFileChooser.getSelectedFile().getPath();
fileSave();}
}
private void fileQuit(){
//quit
dispose();
System.exit(0);
}
private void fileCut(){
//cut
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);//cb is Chipboard
this.textArea.replaceRange("",this.textArea.getSelectionStart(),this.textArea.getSelectionEnd());
isSaved=false;}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in cutting !","error",JOptionPane.ERROR_MESSAGE);}
}
private void fileCopy(){
//Copy
try{
String str=this.textArea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
cb.setContents(s,s);}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"error in copying ! ","error",JOptionPane.ERROR_MESSAGE);}
}
private void filePaste(){
//Paste
try{
Transferable tr=cb.getContents(this);
if(tr!=null){
String s="";
textArea.replaceRange(s,textArea.getSelectionStart(),textArea.getSelectionEnd());}
else{
isSaved=false;}
}catch(Exception err){//what different is between err and e
JOptionPane.showMessageDialog(this,"error in pasting !","error",JOptionPane.ERROR_MESSAGE); }
}
private void fileLook(){
//Look
}
private void fileInstead(){
//Instead
}
private void about(){
JOptionPane.showMessageDialog(this,"这是一个简单的记事本","记事本",JOptionPane.INFORMATION_MESSAGE);
}

//适配器的使用



public void showNote(){//显示方法
this.setVisible(true);
this.validate();
}


public static void main(String args[]){//main方法运行测试程序
NotePad Note=new NotePad();
Note.showNote();
}
}

改好了,应该没什么错了,只是功能好像很差!


2007-01-19 09:32
快速回复:《求助》怎样用JAVA做记事本
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023620 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved