2

好的,我又来了。

我有一个 JPanel,我在其上覆盖paintComponent()并绘制了一个自定义 bufferedImage,以为我的 JPanel 提供背景图像。我也有一个 JButton,我做了同样的事情,以便创建一个具有自定义形状的按钮。问题是,虽然 JButton 似乎已正确添加到带有背景图像的 JPanel,但它没有正确绘制。

我可以从按钮获取输入,就好像它确实存在一样,但它实际上并没有显示出来,而它在标准 JPanel 上显示。下面是一个例子。

提前感谢您的帮助。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test {
    public static void main (String[] args){
        final JFrame frame = new JFrame();
        DrawPanel panel1 = new DrawPanel(createBufferedImage("background.png"));
        JPanel panel2 = new JPanel();

        DrawButton button1 = new DrawButton(createBufferedImage("button.png"));
        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                JOptionPane.showMessageDialog(frame, "input detected, button1");
            }
        });

        DrawButton button2 = new DrawButton(createBufferedImage("button.png"));
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                JOptionPane.showMessageDialog(frame, "input detected, button2");
            }
        });

        panel1.add(button1);
        panel2.add(button2);
        frame.getContentPane().add(panel1, BorderLayout.NORTH);
        frame.getContentPane().add(panel2, BorderLayout.CENTER);
        frame.validate();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    protected static BufferedImage createBufferedImage(String path){
        File img = new File(path);
        BufferedImage bi = null;
        try{
            bi = ImageIO.read(img);
        }
        catch (IOException ioe){
            throw new RuntimeException();
        }
        BufferedImage newImage = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = newImage.createGraphics();
        g2d.drawImage(bi, 0, 0, null);
        g2d.dispose();  
        return newImage;
    }
}

class DrawPanel extends JPanel{

    private BufferedImage bg; 

    public DrawPanel(BufferedImage bg){
        this.bg = bg;
        new JPanel();
        setPreferredSize(new Dimension(bg.getWidth(), bg.getHeight()));
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(bg, 0, 0, null);
        g.dispose();
    }
}

class DrawButton extends JButton{

    private BufferedImage bi;

    public DrawButton(BufferedImage bi){
        setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
        this.bi = (BufferedImage) bi;
        setContentAreaFilled(false);
    }

    @Override
    public Dimension getPreferredSize(){
        return new Dimension(bi.getWidth(), bi.getHeight());
    }

    public BufferedImage getIconImage(){
        return bi;
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(bi, 0, 0, null);
        g.dispose();
    }
}
4

1 回答 1

4

切勿对 JVM 提供给您的 Graphics 对象调用 dispose,仅对您自己创建的 Graphics 对象调用。

所以这没关系:

    BufferedImage newImage = new BufferedImage(bi.getWidth(), bi.getHeight(), 
         BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = newImage.createGraphics();
    g2d.drawImage(bi, 0, 0, null);
    g2d.dispose();

但这不是:

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(bg, 0, 0, null);
    g.dispose();
}

JVM 可能(并且可能会)需要使用该对象进行进一步的绘制,包括绘制容器的子组件,如果您将其丢弃,则可以防止这种情况发生。

另一个建议:考虑覆盖 getPreferredSize,而不是 setPreferredSize。所以而不是这个:

setPreferredSize(new Dimension(bg.getWidth(), bg.getHeight()));

考虑:

@Override
public Dimension getPreferredSize() {
  if (bg == null) {
    return super.getPreferredSize();
  } else {
    return new Dimension(bg.getWidth(), bg.getHeight());
  }
}

这样,组件的首选大小就可以可靠地设置,并且组件的任何用户都无法更改它。

于 2012-12-12T02:31:33.970 回答