如何在画布中添加Button?

时间:2011-04-24 07:19:30

标签: java user-interface video

我有一个画布,在画布里面我有一个网络摄像头。在视频图像的顶部,我想放置一个“按钮”并附加一个事件,这样我就可以控制全屏并最小化它。 但是这种方法给出了错误,如何解决?

  public static void main(String[] args) 
  { 

        JFrame frame = new JFrame("Overlay"); 
        frame.setBackground(Color.RED);

        // Canvas, to have a video and on top a button    
        final Canvas canvas = new Canvas(); 
        Button button = new Button(canvas); // ERROR
        button.setBounds(10,10, 100, 40);
        canvas.setPreferredSize(new Dimension(200, 200));     

        // Layout
        JPanel content = new JPanel(new GridLayout(2,2));
        content.add(canvas);
        content.add(new JButton("test"));  // for empty cell

        // Show
        frame.add(content);                        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.pack(); 
        frame.setVisible(true); 

        // Third party tools to capture the webcam, and plugin to our canvas
        final Video video = player.getElementByName("gl"); 
        XOverlay.wrap(video).setWindowID(canvas); 
    } 

2 个答案:

答案 0 :(得分:1)

它给出了错误,因为Button类中没有以canvas为参数的构造函数。您可以将按钮添加到添加画布的同一JPanel中。

答案 1 :(得分:1)

canvas.add(button);

canvas.add(button, 55, 30);