简体   繁体   中英

Setting image background java swing

I am trying to set an image as background but I am not sure what is wrong with this code itself.. Could you please assist here and pointed out what can be changed? When I run it, it gives me a blank background as before without expected result.

public
    class MenuFrame extends JFrame {

    public MenuFrame(){
        generateMenuFrame();
    }

    public void generateMenuFrame()
    {
        JLabel background;
        JFrame menuFrame = new JFrame("Koronawirus AntiPlague - Menu");
        try {
            menuFrame.setIconImage(ImageIO.read(MenuFrame.class.getResourceAsStream("resources/images/Icon.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        ImageIcon backgroundImage = new ImageIcon("resources/images/MenuBackground.png");
        background = new JLabel("",backgroundImage,JLabel.CENTER);
        background.setBounds(0,0,getWidth(),getHeight());
        menuFrame.add(background);
        menuFrame.setSize(700,400);
        menuFrame.setVisible(true);
        menuFrame.setResizable(false);
        menuFrame.setLocationRelativeTo(null);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

UPDATE:

Thank you guys for any tips. As Andrew recommended I decided to stay with one instance without extands and moreover I used an override of the paintComponent method:

public
    class MenuFrame {

    private JPanel contentPane;

    public MenuFrame(){
        generateMenuFrame();
    }

    public void generateMenuFrame()
    {
        JFrame menuFrame = new JFrame("Koronawirus AntiPlague - Menu");
        try {
            menuFrame.setIconImage(ImageIO.read(MenuFrame.class.getResourceAsStream("resources/images/Icon.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        menuFrame.setSize(700,400);
        menuFrame.setVisible(true);
        menuFrame.setResizable(false);
        menuFrame.setLocationRelativeTo(null);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel() {
                public void paintComponent(Graphics g) {
                                      Image img = Toolkit.getDefaultToolkit().getImage(
                                      MenuFrame.class.getResource("resources/images/MenuBackground.jpg"));
                                      g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
                                     }
               };
        menuFrame.setContentPane(contentPane);
    }
}
background.setBounds(0,0,getWidth(),getHeight());

The above code will set the size of the label to (0, 0) since you are extending JFrame and invoking the getWidth() and getHeight() methods of the frame. The default size of all Swing components is (0, 0), so there is nothing to paint.

Don't attempt to play with the bounds of the label.

The size of the label will automatically be set to the size of the image.

You should then use:

//menuFrame.setSize(700,400);
menuFrame.pack();

so the frame can be size to properly display the label and its image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM