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 的超类;超类必须是类

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

解决

仔细查找原因,原来导入的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);
	}
	
}

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自己做的TEST网络聊天 package com.tsing.chat.frame; import java.awt.*; import java.awt.event.*; import java.util.EventListener; import java.io.*; import java.net.*; import javax.swing.JOptionPane; public class ClientFrame extends Frame implements ActionListener { //窗体应该有发送按钮、文本框用来输入东西、List列表用来显示信息 Button connectbutton = new Button("连接"); Button sendbutton = new Button("发送"); List list = new List(); TextField content = new TextField(20); TextField servername = new TextField("localhost", 20); Label label = new Label("输入服务器名字:"); Socket socket; String num; public ClientFrame(String num) { this.num = num; this.setTitle("山寨QQ (用户:" + num + ")"); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { MsgBean bean = new MsgBean(); bean.setType("请求下线"); bean.setSource(ClientFrame.this.num); //发送消息请求下线 OutputStream out = socket.getOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( out); objectOutputStream.writeObject(bean); objectOutputStream.flush(); } catch (Exception ew) { ew.printStackTrace(); } //退出系统 System.exit(0); } }); sendbutton.addActionListener(this); connectbutton.addActionListener(this); Panel p1 = new Panel(); p1.add(label); p1.add(servername); p1.add(connectbutton); Panel p2 = new Panel(); p2.add(content); p2.add(sendbutton); Panel p3 = new Panel(); p3.setLayout(new BorderLayout()); p3.add("South", p2); list.setFont(new Font("微软雅黑", Font.BOLD, 14)); p3.add(new ScrollPane().add(list)); this.add("South", p1); this.add(p3); this.setSize(400, 300);// this.setResizable(false); //不能缩放窗口 this.setVisible(true); } public static void main(String[] args) { String num = JOptionPane.showInputDialog(null, "输入自己的QQ号码"); boolean b = false; do { b = false; if (num == null || num.equals("")) { num = JOptionPane.showInputDialog("QQ号码不能为空,请重新输入:"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值