2011-01-28 84 views
1

嗨我不断收到一个错误“组件必须显示”并且无法解决它我查看了SO上的问题并尝试了解决方案,但无济于事。我试图实现一个JVLC播放器,当我尝试在Vid类中使用“.setVideoOutput(canvas1)”时发生错误继续收到错误“组件必须可显示”

这里是主类。

import java.awt.Component; 
import java.awt.Toolkit; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
import java.util.logging.Level; 
import java.util.logging.Logger; 



/** 
* 
* @author Shane 
*/ 
public class Main extends javax.swing.JFrame { 

    private LessonPanel lesson; 
    private MultiChoice multiChoice; 
    private Vid video; 
    private int xSize; 
    private int ySize; 

    public Main() throws InterruptedException { 

     Toolkit tk = Toolkit.getDefaultToolkit(); 

     xSize = ((int) tk.getScreenSize().width/2); 
     ySize = ((int) tk.getScreenSize().height/2); 
     this.setSize(xSize, ySize); 
     this.setVisible(true); 

     System.out.print("\nIsDisplayable "+this.isDisplayable()); 
     addVideo(); 


     WindowListener listener = new WindowAdapter() { 

      @Override 
      public void windowClosing(WindowEvent w) { 
       System.exit(0); 
      } 
     }; 
     this.addWindowListener(listener); 
    } 

    public void next(String method) throws InterruptedException{ 

     if(method.equalsIgnoreCase("home")){home();} 
     else if(method.equalsIgnoreCase("Multi")){addMultiChoice();} 
     else if(method.equalsIgnoreCase("Video")){addVideo();} 
     else if(method.equalsIgnoreCase("Visual")){addVisual();} 


    } 

    public void removePanels(Component Comp,String doNext) throws InterruptedException { 

     this.remove(Comp); 
     this.repaint(); 

     next(doNext); 

    } 

    public void home() throws InterruptedException { 

     lesson = new LessonPanel(this); 
     this.add(lesson); 
     this.validate(); 
     this.repaint(); 
    } 

    public void addMultiChoice() throws InterruptedException { 

     multiChoice = new MultiChoice(this); 
     this.add(multiChoice); 
     this.validate(); 
     this.repaint(); 


    } 

    public void addVideo() throws InterruptedException { 
     this.invalidate(); 
     video = new Vid(); 
     this.add(video); 
     this.validate(); 
     this.repaint(); 


    } 

    public void addVisual() throws InterruptedException { 

    } 

    public static void main(String args[]) { 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 

        new Main().setVisible(true); 

       } catch (InterruptedException ex) { 
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
       } 



      } 
     }); 



    } 
} 

,这是那是给我的错误

import java.awt.Canvas; 
import java.awt.Color; 
import org.videolan.jvlc.JVLC; 


public class Vid extends javax.swing.JPanel 
{ 

    private static String filename ="C:/Users/Public/Videos/Sample Videos/Wildlife.wmv"; 
    private Canvas canvas; 


    public Vid() 
    { 
      init(); 
    } 

    public void init(){ 


      System.out.print("\nReady to play video"); 
      canvas = new java.awt.Canvas(); 
      canvas.setBackground(Color.red); 
      canvas.setSize(400, 400); 
      canvas.setVisible(true); 

      add(canvas); 



      String[] arg = new String[4]; 
        arg[0] = "--intf=dummy"; 
        arg[1] = "--ignore-config"; 
        arg[2] = "--no-plugins-cache"; 
        arg[3] = "--plugin-path=plugins"; 




      JVLC jvlc = new JVLC(arg); 
      jvlc.play(filename); 
      jvlc.setVideoOutput(canvas); 


     } 

} 

任何帮助将是巨大的类!提前致谢!!

回答

2

我猜测你尝试执行该方法时面板不可见。您将组件添加到面板,但面板尚未添加到框架中。重构你的代码如下:

video = new Vid(); 
this.add(video); 
this.revalidate(); // 
this.repaint(); 
video.someMethodToInvoke "setVideoOutput" 
+0

非常感谢你的工作!感谢您的时间!! – Hallinan13 2011-01-28 16:44:36

相关问题