我正在尝试编写一个使用网络摄像头捕获照片的程序。我想将 JButton 添加到实时视频的 JFrame 中。然而,该按钮没有显示。我相信视频已经涵盖了这一点。我在下面附上了我的代码。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.contentPane.add(new JButton("Camera"));
frame.setVisible(true);
}
// Create the frame.
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 490);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
new MyThread().start();
}
VideoCap videoCap = new VideoCap();
public void paintComponent(Graphics g){
g = contentPane.getGraphics();
g.drawImage(videoCap.getOneFrame(), 0, 0, this);
}
class MyThread extends Thread{
@Override
public void run() {
for (;;){
repaint();
try { Thread.sleep(30);
} catch (InterruptedException e) { }
}
}
}
}
任何建议都会很棒。
我浏览了很多关于 Jbuttons 没有出现的问题,但我不确定哪一个与我的相似。如果有的话请给我指出正确的方向。
提前谢谢您。
最佳答案
您正在向内容 Pane 添加按钮,然后在其上绘制视频。当然,视频会覆盖您的按钮。
一些建议:
不要设置自定义内容 Pane 。
创建两个单独的面板并将它们添加到内容 Pane 。
向一个面板添加按钮。
将您的视频绘制到另一个视频上。
重构代码以正确覆盖
paintComponent
并从另一个线程重绘它。
关于java - 按钮似乎被视频覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44380478/