Exception in thread "AWT-EventQueue-0" java.lang.Error: 无法解析的编译问题:

错误源代码

package 实验;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

class gFileFilter extends FileFilter {
	public String getDescription() {
		return "*.jpg";
	}
 
	public boolean accept(File file) {
		String name = file.getName();
		return name.toLowerCase().endsWith(".jpg");
	}
}

public class FileFilterDemo{
	static JFrame jf;
	static JButton jb;

	
	public static void main(String[] args) {
		jf=new JFrame();
		jf.setSize(500,500);
		jf.setLocation(200, 300);
		jf.setLayout(null);
		
		jb=new JButton("打开文件");
		jb.setBounds(200, 200, 100, 40);
		jf.add(jb);
		
		jb.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				JFileChooser jfc=new JFileChooser();
				gFileFilter jff=new gFileFilter();
				jfc.addChoosableFileFilter(jff);
				jfc.setFileFilter(jff);
				jfc.showOpenDialog(null);
			}
			
		});
		
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
	
}

错误

Exception in thread "AWT-EventQueue-0" java.lang.Error: 无法解析的编译问题:
    类型 JFileChooser 中的方法 addChoosableFileFilter(FileFilter)对于参数(gFileFilter)不适用
    类型 JFileChooser 中的方法 setFileFilter(FileFilter)对于参数(gFileFilter)不适用

    at 实验.FileFilterDemo$1.actionPerformed(FileFilterDemo.java:45)
    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$500(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.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)

错误提示

类型 FileFilter 不能是 gFileFilter 的超类;超类必须是类

Exception in thread

类型 JFileChooser 中的方法 addChoosableFileFilter(FileFilter)对于参数(gFileFilter)不适用

Exception in thread

解决

仔细查找原因,原来导入的FileFilter是来自io中的

import java.io.FileFilter;

而我们要用的是来自filechooser中的FileFilter

import javax.swing.filechooser.FileFilter;

所以才会发生这样的错误。

成功源代码

解决成功的源代码是:

package 实验;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.filechooser.FileFilter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

class gFileFilter extends FileFilter {
	public String getDescription() {
		return "*.jpg";
	}
 
	public boolean accept(File file) {
		String name = file.getName();
		return name.toLowerCase().endsWith(".jpg");
	}
}

public class FileFilterDemo{
	static JFrame jf;
	static JButton jb;

	
	public static void main(String[] args) {
		jf=new JFrame();
		jf.setSize(500,500);
		jf.setLocation(200, 300);
		jf.setLayout(null);
		
		jb=new JButton("打开文件");
		jb.setBounds(200, 200, 100, 40);
		jf.add(jb);
		
		jb.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				JFileChooser jfc=new JFileChooser();
				gFileFilter jff=new gFileFilter();
				jfc.addChoosableFileFilter(jff);
				jfc.setFileFilter(jff);
				jfc.showOpenDialog(null);
			}
			
		});
		
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
	
}

 

你可能感兴趣的:(异常处理)