0

当单击按钮时,我试图将我从网络摄像头捕获的图像添加到现有的 JPanel,但 JPanel 从不显示图像。谁能指出我正确的方向?

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);

    try {
        grabber.start();
        IplImage img = grabber.grab();
        if (img != null) {

            img1 = img.getBufferedImage();
            ImageIcon icon = new ImageIcon(img1);
            label = new JLabel(icon);
            //photo is the name of the JPanel
            photo.add(label);
            photo.setVisible(true);
            grabber.stop();
            System.out.println("done");

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

编辑:这是整个班级。(为了便于阅读,剪掉了)

package honoursproject;

    import com.googlecode.javacv.OpenCVFrameGrabber;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


public class AddPerson extends javax.swing.JFrame {

/** Creates new form AddPerson */
public AddPerson() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    nameField = new javax.swing.JTextField();
    surnameField = new javax.swing.JTextField();
    photo = new javax.swing.JPanel();
    captureButton = new javax.swing.JButton();
    saveButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    photo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0,       0)));

    pack();
}// </editor-fold>

private void captureButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    BufferedImage img1;
    JLabel label;
    final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
    JFrame frame = new JFrame();
    try {
        grabber.start();
        IplImage img = grabber.grab();

        if (img != null) {

       img1 = img.getBufferedImage();
        ImageIcon icon = new ImageIcon(img1);
        label = new JLabel(icon);
        //photo is the name of the JPanel
        photo.add(label);
        photo.setVisible(true);
        grabber.stop();
        System.out.println("done");


            grabber.stop();
            System.out.println("done");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}                                             



public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new AddPerson().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JButton captureButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField nameField;
private javax.swing.JPanel photo;
private javax.swing.JButton saveButton;
private javax.swing.JTextField surnameField;
// End of variables declaration

}

4

3 回答 3

3

如果您要向您的容器中添加新组件,photo JPanel则需要更新和绘制容器:

photo.revalidate();
photo.repaint();

或者,JLabel也可以在启动时添加,允许您调用setIcon来更新图像。

更新:

您似乎没有将您的添加JPanel photo到您的JFrame任何地方:

add(photo);

不要创建另一个JFrame来显示您的图像。将其添加到您JPanel使用的原始JFrame. 看到这个帖子

于 2013-03-08T20:43:25.887 回答
0

我通过使用 JLabel 而不是 JFrame 或 JPanel 解决了它,它可以工作。

于 2013-03-29T21:03:55.050 回答
0

你忘了打电话setIcon

label.setIcon(icon);
于 2017-01-20T15:48:23.193 回答