如何在不覆盖其包含的组件的情况下将背景图像设置为JPanel?

时间:2016-03-06 18:03:34

标签: java swing background jpanel

我目前无法为JPanel设置背景图片。 问题是,一旦我将窗口大小更改为全屏,所有按钮都会消失,直到我将鼠标悬停在它们上面。

我希望有人能够提供帮助。

这是源代码:

public class MFrame extends JFrame{

private static final long serialVersionUID = 1L;
public static Dimension Screen = null;
static MFrame MainFrame = null;

public static File FileForBild1 = new File("C:/xampp/bild.jpg");
public static File FileForBackground = new File("C:/xampp/Photos/background.jpg");


public static JButton FirstButton, SecondButton, ThirdButton, FourthButton, FifthButton, SixthButton;
public static JPanel MainPanel;

public static void main(String[] args){
    Screen = Toolkit.getDefaultToolkit().getScreenSize();
    MainFrame = new MFrame(Screen);
}

public MFrame(Dimension SSize){
    this.setLayout(null);
    double hoehe = SSize.getHeight();
    double breite = SSize.getWidth();
    int ButtonWidth = (int)(breite*.3);
    int ButtonHeight = (int)(hoehe*.35);
    MainPanel = new JPanel(){
        private static final long serialVersionUID = 1L;
            BufferedImage img = new BufferedImage((int)SSize.getWidth(), (int)SSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
            public void paint(Graphics g){
                try{
                    img = ImageIO.read(FileForBackground); 
                }catch(Exception e){
                    e.printStackTrace();
                }
                g.drawImage(img, 0, 0, (int)SSize.getWidth(), (int)SSize.getHeight(), 0, 0, 3888, 2592, null);
            }
        };

    FirstButton = new JButton(){
        private static final long serialVersionUID = 1L;
        BufferedImage img = new BufferedImage((int)(breite*.3), (int)(hoehe*.35), BufferedImage.TYPE_INT_ARGB);
        public void paint(Graphics g){
            try{
                img = ImageIO.read(FileForBild1); 
            }catch(Exception e){
                e.printStackTrace();
            }
            g.drawImage(img, 0, 0, (int)(breite*.3), (int)(hoehe*.35), 0, 0, 960, 637, null);
        }
    };

SecondButton = new JButton("Bild2");
ThirdButton = new JButton("Bild3");
FourthButton = new JButton("Bild4");
FifthButton = new JButton("Bild5");
SixthButton = new JButton("Bild6");

FirstButton.setBounds((int)(breite*.025), (int)(hoehe*.1), ButtonWidth, ButtonHeight);
SecondButton.setBounds((int)(breite*.35), (int)(hoehe*.1), ButtonWidth, ButtonHeight);
ThirdButton.setBounds((int)(breite*.675), (int)(hoehe*.1), ButtonWidth, ButtonHeight);

FourthButton.setBounds((int)(breite*.025), (int)(hoehe*.5), ButtonWidth, ButtonHeight);
FifthButton.setBounds((int)(breite*.35), (int)(hoehe*.5), ButtonWidth, ButtonHeight);
SixthButton.setBounds((int)(breite*.675), (int)(hoehe*.5), ButtonWidth, ButtonHeight);


MainPanel.setSize((int)breite, (int)hoehe);
MainPanel.setLayout(null);
MainPanel.add(FirstButton);
MainPanel.add(SecondButton);
MainPanel.add(ThirdButton);
MainPanel.add(FourthButton);
MainPanel.add(FifthButton);
MainPanel.add(SixthButton);


setSize((int)breite, (int)hoehe);
setTitle("Test");
add(MainPanel);
setVisible(true);
}
}

1 个答案:

答案 0 :(得分:4)

有几个问题,但主要问题是:

  • 你没有在你的覆盖中调用超级的绘画方法。这将破坏您组件的绘画链,并可能导致儿童组件无法绘制。
  • 您正在阅读绘画方法中的文件 - 这会不必要地减慢一个应该闪电般快速且完全没必要的方法。在构造函数中一次读取文件并将其存储到变量中。
  • 您应该覆盖paintComponent,而不是绘制
  • 再次调用super的paintComponent方法作为覆盖中的第一个调用。

所以做这样的事情:

MainPanel = new JPanel(){
    private static final long serialVersionUID = 1L;

    // the img field should be declared as an instance field and created in the constructor
    // BufferedImage img = new BufferedImage((int)SSize.getWidth(), (int)SSize.getHeight(), BufferedImage.TYPE_INT_ARGB);

    @Override
    protected void paintComponent(Graphics g){

        // first call the super's method
        super.paintComponent(g);

        // file reading should be done once, say in a constructor, not here
        // img = ImageIO.read(FileForBackground); 

        g.drawImage(img, 0, 0, (int)SSize.getWidth(), (int)SSize.getHeight(), 0, 0, 3888, 2592, null);
    }
};

确保添加到此图像显示组件的所有JPanel都设置为非不透明,以便图像可以显示。