日期:2014-05-20  浏览次数:20881 次

JAVA初学者问题,那位大侠帮帮忙
小弟是JAVA初学者,按着教科书上的例子写了个记事本程序(书上的原文例子),可是运行后功实现不了,那位大侠帮忙改进下,不盛感激。
我的程序是用的监听类分开监听模式。
MouseHandler.java

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

class MouseHandler extends MouseAdapter{
public void mouseReleased(MouseEvent e){
if (e.getButton()==MouseEvent.BUTTON3&&e.getSource()==txtContent){
popupMenu.show(txtContent,e.getX(),e.getY());
}
super.mouseReleased(e);
}
}

keyHandler.java
 
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

class keyHandler extends KeyAdapter{
public void keyTyped(KeyEvent e){
if(e.getSource()==txtContent)
lblStatus.setText("已修改");
super.keyTyped(e);
}
}

ConfirmHandler.java 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

class ConfirmHandler implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(!(e.getSource() instanceof JMenuItem))
return;

JMenuItem i = (JMenuItem)e.getSource();

if(i==newItem)
iNew();
else if (i==openItem)
iOpen();
else if (i==saveAsItem)
iSave();
else if (i==cutItem||i==cutPitem)
iCut();
else if (i==copyItem||i==copyPitem)
iCopy();
else if(i==pasteItem||i==pastePitem)
iPaste();
else if (i==aboutItem)
iAbout();

}

private void iAbout() {
// TODO Auto-generated method stub
String mes="";
JOptionPane.showMessageDialog(null, mes);
}

private void iPaste() {
// TODO Auto-generated method stub
txtContent.paste();
lblStatus.setText("已修改");
}

private void iCopy() {
// TODO Auto-generated method stub
txtContent.copy();
pasteItem.setEnabled(true);
pastePitem.setEnabled(true);
}

private void iCut() {
// TODO Auto-generated method stub
txtContent.cut();
pasteItem.setEnabled(true);
pastePitem.setEnabled(true);
lblStatus.setText("已修改");
}

private void iSave() {
// TODO Auto-generated method stub
JFileChooser chooser = new JFileChooser();
PrintWriter w = null;
File file = null;

if(lblStatus.getText().equals("已修改")){
int s =chooser.showSaveDialog(null);
if(s==JFileChooser.CANCEL_OPTION)
return;
file = chooser.getSelectedFile();
try{
w = new PrintWriter(new BufferedWriter(new FileWriter(file)));

String ss[]=txtContent.getText().split("\n");
for(String st:ss)
w.println(st);

}catch (FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
w.close();
}
}
lblStatus.setText("未修改");
}

private void iOpen() {
// TODO Auto-generated method stub
iNew();

JFileChooser chooser = new JFileChooser();
int s = chooser.showOpenDialog(null);
String str = "";

if(s==JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
BufferedReader r= null;
try{
r = new BufferedReader(new FileReader(file));
while(true){
str = r.readLine();