簡體   English   中英

動態地將JPanel添加到groupLayout

[英]Dynamically add a JPanel to a groupLayout

我嘗試使用Java為大學項目構建網絡攝像頭應用程序。 到目前為止,我從不需要GUI,因此我對此沒有經驗,因此我在netbeans中使用了gui生成器。

現在,GUI看起來像這樣:

http://i.stack.imgur.com/3MCiv.png

它只是在gui構建器中添加的jPanel和jButton。

我要顯示的圖像是使用openCV拍攝的。 這工作得很好,我得到了bufferedImage。 為了顯示此圖像,我創建了jPanel的子類並更改了paintComponent方法。

package WebcamImageCapture;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class ImagePanel extends JPanel
{
    /**
     * Creates a new empty ImagePanel.
     */
    public ImagePanel()
    {
        this.image = null;
    }

    /**
     * Creates a new ImagePanel from BufferedImage img.
     * @param img The BufferedImage to display on the ImagePanel
     */
    public ImagePanel(BufferedImage img)
    {
        this.image = img;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), null);

        g.setColor(new Color(240, 160, 40));
        g.fillRect(10, 10, 25, 25);

        this.repaint();
    }

    /**
     * Sets the BufferedImage img to display on ImagePanel.
     * @param img The BufferedImage to display on the ImagePanel
     */
    public void setImage(BufferedImage img)
    {
        this.image = img;
    }

    private BufferedImage image;
}

GUI類具有成員openCameraButtonoutputPanel ,它們是您可以在屏幕截圖中看到的元素。 imagePanel了以下操作,將我的imagePanel添加到處理按鈕的ActionPerformed事件的方法內部的outputPanel

// create the custom jPanel
ImagePanel webcamFrame = new ImagePanel(img);
webcamFrame.setPreferredSize(new Dimension(640, 480));

this.outputPanel.getLayout().addLayoutComponent("webcamFrame", webcamFrame);
this.outputPanel.revalidate();
this.outputPanel.repaint();

this.revalidate();
this.repaint();

這不起作用=(。我在Google周圍搜索並測試了2天(還閱讀了有關布局的oracle文檔: documentation ),但未找到解決方案。

因此,主要問題是:

  1. 如何添加ImagePanel?
  2. 我應該以其他布局手動實現GUI嗎?

在此先感謝您的幫助。

2.是否應該以其他布局手動實現GUI?

是的,IDE生成的代碼很難維護和更改。

JFrame默認情況下使用BorderLayout,因此您可以執行以下操作:

ImagePanel = new ImagePanel();
frame.add(imagePanel, BorderLayout.CENTER);

JPanel south = new JPanel();
JButton load = new JButton("Load Image");
south.add(load);
frame.add(south, BorderLayout.PAGE_END);

因此,您的想法是在設計時將面板添加到框架。 然后,無論何時捕獲圖像,都將調用setImage()方法。 setImage()方法中的代碼為:

this.image = img;
repaint();

您的paintComponent()代碼將更改為:

if (image != null);
    g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), null);

還要擺脫repaint()。 這將導致無限循環。

我按照您的建議更改了代碼...

package WebcamImageCapture;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class ImagePanel extends JPanel
{
    /**
     * Creates a new empty ImagePanel.
     */
    public ImagePanel()
    {
        this.image = null;
    }

    /**
     * Creates a new ImagePanel from BufferedImage img.
     * @param img The BufferedImage to display on the ImagePanel
     */
    public ImagePanel(BufferedImage img)
    {
        this.image = img;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        if(this.image != null)
        {
            super.paintComponent(g);
            g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), null);

            g.setColor(new Color(240, 160, 40));
            g.fillRect(10, 10, 25, 25);

            System.out.println("image drawn ...");
        }
        else
        {
            System.out.println("image not drawn ...");
        }
    }

    /**
     * Sets the BufferedImage img to display on ImagePanel.
     * @param img The BufferedImage to display on the ImagePanel
     */
    public void setImage(BufferedImage img)
    {
        this.image = img;
        this.repaint();
    }

    private BufferedImage image;
}

...並手動創建了GUI。

private void init()
{
    this.imageOutput = new ImagePanel();
    this.add(imageOutput, BorderLayout.CENTER);

    this.openCameraButton = new JButton("open camera");
    this.openCameraButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            openCameraButtonActionPerformed(evt);
        }
    });

    JPanel navigation = new JPanel();
    navigation.add(this.openCameraButton, BorderLayout.PAGE_END);

    this.add(navigation);
    this.setTitle("Webcam");
    this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    this.pack();
}

openCameraButtonActionPerformed方法內部,我拍攝了圖像並使用ImagePanelsetImage方法進行設置。

this.imageOutput.setImage(img);

但是不幸的是沒有任何反應。 如果我執行以下操作,則會得到一個新窗口,其中包含圖像。

JFrame myFrame = new JFrame("myFrame");
ImagePanel out = new ImagePanel(img);
myFrame.add(out);
myFrame.pack();
myFrame.setVisible(true);

我做錯了什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM