如何在画布中添加按钮?
我有一个画布,画布内有一个网络摄像头。在视频图像的顶部,我想放置一个“按钮”并附加一个事件,以便我可以控制全屏并将其最小化。 但是这个方法报错了,怎么解决呢?
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);
}
I have a canvas and inside the canvas i have a webcam. On top of the video image, i want to place an "Button" and attach an event, so that i have control for full screen and minimize it.
But this method is giving error, how to fix it?
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它会给出错误,因为
Button
类中没有将canvas
作为参数的构造函数。您可以将按钮添加到要添加画布的同一 JPanel 中。It gives error because there are no constructor in
Button
class that takescanvas
as an argument.You can add your button to same JPanel where you are adding your canvas.或者
or