不断收到错误“组件必须可显示”

发布于 2024-10-15 04:50:04 字数 3746 浏览 2 评论 0原文

您好,我不断收到错误“组件必须可显示”并且无法解决它,我查看了有关 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);


       }

}

任何帮助都会很棒!提前致谢!!

Hi I keep getting an error "Component must be displayable" and cant solve it I looked at the Questions on SO and tried the solutions but to no avail. I'm trying to implement a JVLC player the error occurs when I try " .setVideoOutput(canvas1) " in the Vid class

here is the main class.

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);
                }



            }
        });



    }
}

and this is the the class that is giving me the error

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);


       }

}

Any help would be great!! thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-10-22 04:50:04

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

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

I would guess the panel is not visible when you attempt to execute that method. You add the component to the panel but the panel has not yet been added to the frame. Restructure your code something like:

video = new Vid();
this.add(video);
this.revalidate(); // 
this.repaint(); 
video.someMethodToInvoke "setVideoOutput"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文