我无法将图像背景设置为已创建到Frame中的JPanel

时间:2014-06-15 07:10:33

标签: java image swing background jpanel

所以这是我的代码..我无法使其发挥作用 我有一个创建的面板用netbeans中的调色板制作,它还有几个标签和按钮。我需要为面板设置一个背景,以及它的所有内容。怎么了?

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class PanelPrincipal extends javax.swing.JPanel {

class ImagePanel extends JPanel {

    private Image img;
    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
    } 
public void paintComponent(Graphics g){
    g.drawImage(img,0,0, null); 
}
}
        /**
         * Creates new form PanelPrincipal
         */
    public PanelPrincipal(VentanaPrincipal ventanaPrincipal) {
        initComponents();

0 个答案:

没有答案