| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 613 人关注过本帖
标题:[讨论]记事本程序调试
只看楼主 加入收藏
fengfeihuang
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-9-11
收藏
 问题点数:0 回复次数:3 
[讨论]记事本程序调试

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

1yOaUVZT.txt (10.3 KB) [讨论]记事本程序调试


搜索更多相关主题的帖子: 记事本 调试 
2006-11-24 13:20
fengfeihuang
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-9-11
收藏
得分:0 
该程序在" this.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}});//适配器的使用
"报错
此处匿名类定义是出错了吗?
请求帮助
2006-11-24 13:23
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
该程序在" this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}});//适配器的使用
"报错
此处匿名类定义是出错了吗?
请求帮助

这样才叫用适配器,你刚才是实现接口,因为WindowListener里面有很多方面,你只实现一个当然会报错,
用适配置就可以了,因为它帮你默认实现了那些方法,就等着你来重写你自己的方法了

可惜不是你,陪我到最后
2006-11-24 13:41
fengfeihuang
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-9-11
收藏
得分:0 
千里冰封,thank you very much~!
2006-11-25 22:10
快速回复:[讨论]记事本程序调试
数据加载中...
 
   



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

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