package another;
//引入包
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;import javax.swing.*;import java.io.*;public class TextFilter extends JFrame implements ActionListener{
//声明成员变量
private File file; //文件对象,用来表示当前文件
private JTextArea text; //文本域
private JFileChooser fchooser; //选择文件对话框
private boolean b = false; //用来标记原来是不是已经有了文本
private JComboBox checkFont, checkNumber; //这是字体和字号的复选框
private JCheckBox checkbold,checkstyle;//粗体斜体选择框//声明构造方法
public TextFilter()
{
super("文本文件编辑器");
this.setBounds(400, 400, 500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text = new JTextArea(); //实例化文本域
this.getContentPane().add(new JScrollPane(this.text)); //对文本域添加滚动条,并且将滚动条对象添加到文本文件编辑器JMenuBar menubar = new JMenuBar(); //创建菜单栏对象
this.setJMenuBar(menubar); //为文本编辑器窗口添加菜单栏String menustr[] = {"文件(F)","编辑(E)","插入(C)","格式(O)","工具","帮助(H)"}; //这是菜单栏上的菜单选项
JMenu menu[] = new JMenu[menustr.length]; //菜单选项
for(int i = 0 ; i < menu.length ; i++)
{
menu[i] = new JMenu(menustr[i]); //实例化菜单选项
menubar.add(menu[i]); //将菜单选项添加入菜单栏
}String menuitemstr[] = {"新建(N)","打开(O)","保存(S)","另存为(Q)","退出(X)"};
JMenuItem menuitem[] = new JMenuItem[menuitemstr.length];
for(int i = 0 ; i < menuitem.length ; i++ )
{
menuitem[i] = new JMenuItem(menuitemstr[i]);
menu[0].add(menuitem[i]);
menuitem[i].addActionListener(this);
if(i == 3)
menu[0].addSeparator();
}//添加快捷键
menuitem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
menuitem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
menuitem[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
menuitem[3].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
menuitem[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.ALT_MASK));
String menuitemstr1[] = {"复制(C)","粘贴(V)","剪切(X)"};
JMenuItem menuitem1[] = new JMenuItem[menuitemstr1.length];
for(int i = 0 ; i < menuitem1.length ; i++)
{
menuitem1[i] = new JMenuItem(menuitemstr1[i]);
menu[1].add(menuitem1[i]);
menuitem1[i].addActionListener(this);
}//添加快捷键
menuitem1[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
menuitem1[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
menuitem1[2].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//添加颜色功能
String color = "颜色";
JMenuItem menuitem2 = new JMenuItem(color);
menu[3].add(menuitem2);
menuitem2.addActionListener(this);String insertstr[] = {"图片","文件"};
JMenuItem insert[] = new JMenuItem[insertstr.length];
for(int i = 0;i<insert.length;i++)
{
insert[i] = new JMenuItem(insertstr[i]);
menu[2].add(insert[i]);
}String helpstr[] = {"查看帮助","有关记事本"};
JMenuItem help[] = new JMenuItem[helpstr.length];
help[0] = new JMenuItem(helpstr[0]);
help[1] = new JMenuItem(helpstr[1]);
menu[5].add(help[0]);
menu[5].addSeparator();
menu[5].add(help[1]);//工具栏
JToolBar toolbar = new JToolBar();
this.getContentPane().add(toolbar,"North"); 
JButton bnew = new JButton("新建");
JButton bopen = new JButton("打开");
JButton bsave = new JButton("保存");
bnew.addActionListener(this);
bopen.addActionListener(this); //注册监听器
bsave.addActionListener(this);//工具栏里面的字体栏
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String charac[] = ge.getAvailableFontFamilyNames();
checkFont = new JComboBox(charac);
checkFont.addActionListener(this);
checkFont.setEditable(false);//工具栏里面的字号
String size[] = {"10","20","30","40","50"};
checkNumber = new JComboBox(size);
checkNumber.addActionListener(this);
checkNumber.setEditable(false);//添加选择框
checkbold = new JCheckBox("粗体");
checkstyle = new JCheckBox("斜体");
checkbold.addActionListener(this);
checkstyle.addActionListener(this);toolbar.add(bnew);
toolbar.add(bopen);
toolbar.add(bsave);
toolbar.add(checkFont);
toolbar.add(checkNumber);
toolbar.add(checkbold);
toolbar.add(checkstyle);this.setVisible(true);this.file = null; //文件对象为空
this.fchooser = new JFileChooser(new File(".","")); // File对象是当前目录下,文件对话框打开后就是默认目录
//声明FileExtetionFilter对象,由两部分组成,一部分是文件表述,另一部分是该描述下筛选文件的文件扩展名
this.fchooser.setFileFilter(new FileExtentionFilter("文本文件(*.txt)","txt"));
}public TextFilter(File file)
{this();
//用文件对象作为参数
this.file = file;
this.text.setText(this.readFromFile());
this.setTitle(this.file.getName());}public TextFilter(String filename)
{
//用文件名作为参数的构造方法
this(new File(filename));
}public void writeToFile(String lines)
{
//对文件进行写入操作的方法
try
{
FileWriter fout = new FileWriter(this.file); //对当前文件建立输出流
fout.write(lines+"\n\r");
fout.close();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(this, "存在IO错误,写入"+file.getName()+"不成功");
}
}@SuppressWarnings("finally")
public String readFromFile()
{
char lines[] = null;
try
{
FileReader fin = new FileReader(this.file);
lines = new char[(int)this.file.length()];
fin.read(lines);
fin.close();
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(this, "\""+this.file.getName()+"\"文件不存在");
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(this, "IO错误,读取"+file.getName()+"文件不成功");
}
finally
{
return new String(lines);
}
}public void actionPerformed(ActionEvent a)
{
if(a.getActionCommand() == "新建" || a.getActionCommand() == "新建(N)")
{
if(JOptionPane.showConfirmDialog(this, "确定新建文件吗") == 0)
{
this.file = null;
this.setTitle("未命名");
this.text.setText("");
b = false;
}
}
else if((a.getActionCommand() == "打开" || a.getActionCommand() == "打开(O)") && fchooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{this.file = fchooser.getSelectedFile();
this.setTitle(this.file.getName());
this.text.setText(this.readFromFile());
b = true;
}
else if((a.getActionCommand() == "保存" || a.getActionCommand() == "保存(S)") && this.file != null)
{
//需要保存的文件不为空
this.writeToFile(this.text.getText());
b = true;
}
else if(((a.getActionCommand() == "保存" || a.getActionCommand() == "保存(S)") && this.file == null) && fchooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
{
this.file = fchooser.getSelectedFile();
if(!file.getName().endsWith(".txt"))
this.file = new File(file.getAbsolutePath()+".txt");
this.writeToFile(this.text.getText());
this.setTitle(this.file.getName());
b = true;}
else if((a.getActionCommand() == "另存为" || a.getActionCommand() == "另存为(Q)") && fchooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
{
this.file = fchooser.getSelectedFile();
if(!file.getName().endsWith(".txt"))
this.file = new File(file.getAbsolutePath()+".txt");
this.writeToFile(this.text.getText());
this.setTitle(this.file.getName());
b = true;
}
else if(a.getActionCommand() == "退出" || a.getActionCommand() == "退出(X)")
{
if(b == false)
{
System.exit(0);
}
else
{
if(JOptionPane.showConfirmDialog(this, "文档还没有保存,确定退出吗") == 0)
System.exit(0);
}
}
else if(a.getActionCommand() == "复制" || a.getActionCommand() == "复制(C)")
{
this.text.copy();
}
else if(a.getActionCommand() == "粘贴" || a.getActionCommand() == "粘贴(V)")
{
this.text.paste();
}
else if(a.getActionCommand() == "剪切" || a.getActionCommand() == "剪切(X)")
{
this.text.cut();
}
else if(a.getSource() instanceof JComboBox || a.getSource() instanceof JCheckBox)//这一套是用来设置一套字体的
{
int fontsize = 0;
try
{
String fontname = (String)checkFont.getSelectedItem(); //获得选中的字体
fontsize = Integer.parseInt((String)checkNumber.getSelectedItem()); //获得选中的字体大小
if(fontsize < 4 || fontsize > 120)
throw new Exception("SizeException");
java.awt.Font font = text.getFont(); //获得当前文本对象
int style = font.getStyle(); //获得当前文本的字形
if(a.getSource() == checkbold)
style = style ^ 1; //变成粗体
if(a.getSource() == checkstyle)
style = style ^ 2;
text.setFont(new Font(fontname,style,fontsize)); //当前字形不变
}
catch(Exception e)
{
if(e.getMessage() == "SizeException")
JOptionPane.showMessageDialog(this, "字号超出范围");
}
}
else if(a.getActionCommand() == "颜色")
{
Color fontcolor;
new JColorChooser();
fontcolor = JColorChooser.showDialog(this, "颜色选择器", new Color(255,255,255));
text.setForeground(fontcolor);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TextFilter();
}}自己写的一个文本编辑器,同样的程序在我的myeclipse下就可以运行,但是在eclipse下运行提示错误错误发到下一楼

解决方案 »

  1.   

    Exception in thread "main" java.lang.NullPointerException
    at sun.awt.shell.Win32ShellFolder2$7.call(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2$7.call(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2$ComInvoker.invoke(Unknown Source)
    at sun.awt.shell.ShellFolder.invoke(Unknown Source)
    at sun.awt.shell.ShellFolder.invoke(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2.getFileSystemPath(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2.access$300(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2$2.call(Unknown Source)
    at sun.awt.shell.Win32ShellFolder2$2.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2$ComInvoker$3.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at another.TextFilter.actionPerformed(TextFilter.java:214)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)也就是说,错误很明显是空指针,但是不明白为什么在eclipse中空指针异常
      

  2.   

    this.fchooser = new JFileChooser(new File(".","")); // File对象是当前目录下,文件对话框打开后就是默认目录
    //声明FileExtetionFilter对象,由两部分组成,一部分是文件表述,另一部分是该描述下筛选文件的文件扩展名
    this.fchooser.setFileFilter(new FileExtentionFilter("文本文件(*.txt)","txt"));
    }
    这个类哪来的
      

  3.   


    我忘记发上来了……是另外一个类package another;
    //这个是文件过滤器类
    import java.io.File;public class FileExtentionFilter extends javax.swing.filechooser.FileFilter{private String des , exten; //声明两个成员变量,一个是文件描述,一个是扩展名 //声明构造方法,用来实例化成员变量
    public FileExtentionFilter(String des, String exten)
    {
    this.des = des;
    this.exten = exten;
    }//由于继承FileFilter类,所以要重载其中的accept方法
    public boolean accept(File f)
    {
    return f.getName().toLowerCase().endsWith(this.exten); 
    //f.getname()用来返回f文件的文件名
    //.tolowecase()用来将所得到的文件名字符串全部转化为小写
    //endswith(this.exten)用来比较字符串是否以成员变量exten结尾,也就是筛选文件类型 
    }public String getDescription() //getDescription()是类方法中的一个抽象方法,必须实现
    {
    return this.des;
    }}
      

  4.   

    在eclipse4.2中运行没有任何问题的,不可能一个java文件在myeclipse下能运行在eclipse下就不能运行的。
      

  5.   

    会不会是jdk的问题呀?另外,MyEclipse用的好好的,干嘛换掉呢?
      

  6.   


    因为实验室用的是eclipse,所以后来也开始用了
      

  7.   

    这种情况很大可能是环境, jar之类的问题, 代码本身没错
      

  8.   


    因为实验室用的是eclipse,所以后来也开始用了在window-->preferences 里搜compiler,看看jdk版本是不是有问题。
      

  9.   

    at another.TextFilter.actionPerformed(TextFilter.java:214)
    看看除了什么问题。看看是那个方法报错了还是缺少什么东西