【问题标题】:@Override are not working in SwingWorker method@Override 在 SwingWorker 方法中不起作用
【发布时间】:2014-05-14 00:54:44
【问题描述】:

在多次尝试找到@Override 类 Worker 扩展 SwingWorker 类的问题后,我希望有人帮我找到答案,我的旧代码是:

这段代码负责读取文件并显示JTable中的信息,但是速度很慢所以我需要使用swing worker,所以我更新了代码(看下一段代码)

private DefaultTableModel createModel(String srtPath) {
    //int maxLine = ReadFile.maxLine(srtPath);  // debug
    //Object[][] data = new Object[maxLine][];
    //System.out.println(maxLine);  // debug
    DefaultTableModel model = new DefaultTableModel(columnNames, 0);
    ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
    ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
    ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
    ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
    for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
        //System.out.println(lins.get(i)+" "+starts.get(i)+" "+ ends.get(i)+" "+ subs.get(i));
        model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
    }
    return model;

现在为了使用 SwingWorker,我应用了这些更改

  @Override
     class Worker extends SwingWorker<DefaultTableModel, Void> {
       private final String srtPath;
       private final JTable table;
       DefaultTableModel model;
        public Worker(String srtPath, JTable table) {
                this.srtPath = srtPath;
                this.table = table;
             }

      protected DefaultTableModel doInBackground(String srtPath)  {
     model = new DefaultTableModel(columnNames, 0);
    ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
    ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
    ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
    ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
    for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
        model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
    }
    return model;
}
    @Override
protected void done() {
    table.setModel(model);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    TableColumn columnA = table.getColumn("#");
    columnA.setMinWidth(10);
    columnA.setMaxWidth(40);
    TableColumn columnB= table.getColumn("Start");
    columnB.setMinWidth(80);
    columnB.setMaxWidth(90);
    TableColumn columnC= table.getColumn("End");
    columnC.setMinWidth(80);
    columnC.setMaxWidth(90);
}
}

这段代码是我的操作方法

Action openAction = new AbstractAction("Open Subtitle", openIcon) {
        @Override
        public void actionPerformed(ActionEvent e) {
            ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            ourFileSelector.showSaveDialog(null);
            ourSrtFile =  ourFileSelector.getSelectedFile();
            srtPath = ourSrtFile.getAbsolutePath();
            Worker p = new Worker(srtPath, table);
            p.execute();


        }
    };

我得到的错误

    C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java:241: error: annotation type not applicable to this kind of declaration
     @Override
C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java:242: error: GuiInterface.Worker is not abstract and does not override abstract method doInBackground() in SwingWorker
     class Worker extends SwingWorker<DefaultTableModel, Void> {
Note: C:\Users\isslam\Documents\NetBeansProjects\AnimeAid\src\animeaid\GuiInterface.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

【问题讨论】:

    标签: java swing jtable jfilechooser swingworker


    【解决方案1】:

    不要@Override 类。仅对方法使用 @Override。

    【讨论】:

    • doInBackground 很好,没有 Override 类询问抽象方法以添加它们或使类抽象
    【解决方案2】:

    @Override 的目标是METHOD。 第二个问题是你没有覆盖超类的所有抽象方法(SwingWorker)。验证 doInBackground 的签名是否与超类中的签名相同。

    【讨论】:

    • 你能在我需要@Override的地方写简单的代码吗
    • @jackpeton 我修改了我的答案。
    • 问题来自受保护的 DefaultTableModel doInBackground(String srtPath) 我必须删除 String srtPath 但我有时间多次上传文件
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多