JFrame background image

前端 未结 4 2028
攒了一身酷
攒了一身酷 2020-11-27 20:11

I am creating a GUI, albeit a simple one, and I want to have a background image (2048 X 2048) fill up the whole window and a square to the left top corner where the occasion

4条回答
  •  死守一世寂寞
    2020-11-27 21:11

    I used a very similar method to @bott, but I modified it a little bit to make there be no need to resize the image:

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("image.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
    ImageIcon imageIcon = new ImageIcon(dimg);
    setContentPane(new JLabel(imageIcon));
    

    Works every time. You can also get the width and height of the jFrame and use that in place of the 800 and 508 respectively.

提交回复
热议问题