我正在使用我的代码,通过使用摄像头解码二维码。我看过一个与此相关的项目,并使用了它。我在https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-qrcode上看到的
因此,我创建了一个JFrame表单,然后在表单中心拖入JPanel。我想让摄像头显示在居中的JPanel中。如下所示:

我使用了下面的代码,但给出了一个错误的布局。截图如下:

public class main extends javax.swing.JFrame {
private Webcam webcam = null;
private WebcamPanel panel = null;
private JTextArea textarea = null;
public main() {
initComponents();
Dimension size = WebcamResolution.QVGA.getSize();
webcam = Webcam.getWebcams().get(0);
webcam.setViewSize(size);
WebcamPanel lab1 = new WebcamPanel(webcam);
setLayout(new FlowLayout());
add(lab1 = new WebcamPanel(webcam));
}
}发布于 2015-08-31 05:24:02
FlowLayout在一行上从左到右显示标签。
我想让摄像头显示在居中的JPanel中。
使组件在面板上居中的最简单方法是使用GridBagLayout:
JPanel panel = new JPanel( new GridBagLayout() );
panel.add(someComponent, new GridBagConstraints());现在,"someComponent“将在面板可用的空间中水平和垂直居中。
https://stackoverflow.com/questions/32298581
复制相似问题