2017-07-19 102 views
0

我有以下代碼:SwingWorker的工作不

import javax.swing.*; 
import java.awt.event.*; 
import java.util.List; 
import java.util.concurrent.Executor; 
import java.util.concurrent.Executors; 
public class KaraokeMachine extends JFrame implements ActionListener 
{ 
ClassLoader Idr = this.getClass().getClassLoader(); 
java.applet.AudioClip everythingIsAwesome = JApplet.newAudioClip(Idr.getResource("everything is awesome.wav")); 
JLabel lbl1 = new JLabel(""); 
JButton btn = new JButton("Play"); 
JPanel pnl = new JPanel(); 
final Executor executor = Executors.newCachedThreadPool(); 

public KaraokeMachine() 
{ 
    super("Karaoke"); 
    setSize(520, 280); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pnl.add(lbl1); 
    pnl.add(btn); 
    btn.addActionListener(this); 
    add(pnl); setVisible(true); 
} 

public void actionPerformed(ActionEvent event) 
{ 
    if (event.getSource() == btn) 
    { 
      SwingWorker<Void, String> worker = new SwingWorker<Void, String>() 
      { 
       @Override 
       protected Void doInBackground() throws Exception 
       { 
        everythingIsAwesome.play(); 
        this.publish("Everything"); 
        Thread.sleep(600); 
        this.publish("Everything is"); 
        Thread.sleep(400); 
        this.publish("Everything is Awesome!"); 
        Thread.sleep(2000); 
        this.publish("Everything"); 
        Thread.sleep(600); 
        this.publish("Everything is"); 
        Thread.sleep(400); 
        this.publish("Everything is cool"); 
        Thread.sleep(400); 
        this.publish("Everything is cool when"); 
        Thread.sleep(400); 
        this.publish("Everything is cool when you're"); 
        Thread.sleep(400); 
        this.publish("Everything is cool when you're part"); 
        Thread.sleep(150); 
        this.publish("Everything is cool when you're part of"); 
        Thread.sleep(150); 
        this.publish("Everything is cool when you're part of a"); 
        Thread.sleep(150); 
        this.publish("Everything is cool when you're part of a team"); 
        Thread.sleep(1000); 
        this.publish("Everything"); 
        Thread.sleep(600); 
        this.publish("Everything is"); 
        Thread.sleep(400); 
        this.publish("Everything is Awesome!"); 
        Thread.sleep(1500); 
        this.publish("When"); 
        Thread.sleep(300); 
        this.publish("When you're"); 
        Thread.sleep(300); 
        this.publish("When you're livin'"); 
        Thread.sleep(300); 
        this.publish("When you're livin' in"); 
        Thread.sleep(300); 
        this.publish("When you're livin' in a"); 
        Thread.sleep(300); 
        this.publish("When you're livin' in a dream!"); 
        Thread.sleep(300); 
        return null; 
       } 
      @Override 
      protected void process(List<String> res) 
      { 
       for(String text : res) 
       { 
        lbl1.setText(text); 
       } 
       } 
     }; 
     executor.execute(worker); 
    } 
} 
public static void main(String[] args) 
{ 
    KaraokeMachine karaoke = new KaraokeMachine(); 
} 

}

當我使之成爲一個類文件這一點,它工作正常,但是當我把它變成一個罐子文件,我得到以下錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDegFoundError: KaraokeMachine$1

有誰知道如何更改代碼,以便swingworker在jar文件中工作?

+0

異常堆棧跟蹤也是**文本**。請不要截圖 - 將它們作爲(格式化!)文本添加到您的問題! – GhostCat

+0

好的,謝謝。我會去做。 – DalekCaan99

+0

更好 - 但現在你刪除了「太多」。在這種情況下,它是可以的,但是,一般情況下:可以將「完整」堆棧跟蹤複製到問題中。編輯可以隨時*刪除*不需要的東西 - 但他不能添加缺少的東西。 – GhostCat

回答

3

該異常意味着某些可疑內部類在您的類路徑中缺失。所以最有可能的答案是:當你將你的班級「捆綁」到該班級文件中時,你可以將忘記爲,這些班級可以命名爲A$1.class

KaraokeMachine.class是 「主」 類,但在這裏:

SwingWorker<Void, String> worker = new SwingWorker<Void, String>() { 

要創建一個匿名內部類 - 它進入KaraokeMachine $ 1.class。爲了運行你的應用程序,你需要所有這些類文件存在。

換句話說:您的JAR文件的內容不完整。仔細看看你的如何構建那個JAR文件。例如,請參閱here

+0

我的jar文件的內容是: META-INF/ META-INF/MANIFEST.MF KaraokeMachine.class 一切awesome.wav 是錯過了什麼的呢? – DalekCaan99

+0

哦,看。它是空的。驚喜。嚴重:你缺乏**基本知識;同時,你嘗試一次學習各種各樣的東西。像使用swing執行Java UI編程,然後使用JAR文件。我的2分在這裏:退後一步。閱讀涵蓋真實**基礎**的教程。當你沒有這方面的經驗時,不要去「試錯」......查看我更新的關於使用JAR的幫助。 – GhostCat

+0

好的,我會看看真實的基礎知識。 – DalekCaan99