首页 > 解决方案 > 不能将新实例与自定义 JFileChooser 一起使用:没有构造函数?

问题描述

这不是我的程序,但还没有人遇到这个问题,我是第一个,所以最初的开发人员已经尝试帮助我,但无法提供帮助,因为该程序对其他人都很好。

我需要点击一个按钮,这个按钮应该显示一个弹出窗口来选择一个文件(JFileChooser),但是在每个人的电脑上它都可以正常工作,但在我的身上却没有,注意到发生了,没有弹出窗口,只是调试器上的错误。

该程序有一个调试器,即创建错误日志的代码:

      return (T)clazz.getConstructor(new Class[0]).newInstance(new Object[0]);
    } catch (ReflectiveOperationException e) {
      e.printStackTrace();
      throw new RuntimeException("No default constructor found for " + clazz.getName());
    } 
  }

/\ 这就是创建日志的原因。


这是调试日志中描述的问题:/

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: `
No default constructor found for com.github.manolo8.darkbot.gui.tree.components.JFileOpener

我已经安装了正确的 sdk 和运行时,每个人都在使用,他们没有问题。

这是文件内的代码:“JFileOpener”

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

public class JFileOpener extends JLabel implements OptionEditor {
  private final JFileChooser fc = new JFileChooser(new File("."))
    {
      protected JDialog createDialog(Component parent) throws HeadlessException {
        JDialog dialog = super.createDialog(parent);
        dialog.setAlwaysOnTop(true);
        return dialog;
      }
    };

  public JComponent getComponent() { return this; }

  public void edit(ConfigField field) {
    setText(Strings.fileName((String)field.get()));
    SwingUtilities.invokeLater(() -> {
          if (this.fc.showOpenDialog(null) != 0)
            return;  field.set(this.fc.getSelectedFile().getAbsolutePath());
          setText(Strings.fileName((String)field.get()));
        });
  }
}

标签: javaswingjfilechooser

解决方案


您是否尝试添加您的JFileOpener类的默认构造函数?只是为了看看会发生什么?

public class JFileOpener extends JLabel implements OptionEditor {

    public JFileOpener() { //Here
        super();
    }

    private final JFileChooser fc = new JFileChooser(new File(".")) {
        @Override
        protected JDialog createDialog(Component parent) throws HeadlessException {
            JDialog dialog = super.createDialog(parent);
            dialog.setAlwaysOnTop(true);
            return dialog;
        }
    };

    public JComponent getComponent() {
        return this;
    }

    public void edit(ConfigField field) {
        setText(Strings.fileName((String) field.get()));
        SwingUtilities.invokeLater(() -> {
            if (this.fc.showOpenDialog(null) != 0)
                return;
            field.set(this.fc.getSelectedFile().getAbsolutePath());
            setText(Strings.fileName((String) field.get()));
        });
    }
}

推荐阅读