Hello everybody,

I'm working in a java interface and I'm trying to use an image as a background but nothing from the result I found in the internet seems to work well with me :/


my code is:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Color;

public class index extends JFrame {

        private JButton Exit;

public index() {
                super();
                
                addWindowListener(new   WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                         hide();
                        }
                } );
                
                JPanel Panel = new JPanel();
                Panel.setLayout(new GridBagLayout());
            Panel.setMinimumSize(new Dimension(700, 500));
            Panel.setPreferredSize(new Dimension(700, 500));
            

            ImageIcon image = new ImageIcon("images/GetAttachment.aspx-(22).jpg");
            JLabel ImageLabel = new JLabel(image);
            ImageLabel.setMinimumSize(new Dimension(300,300));
            ImageLabel.setPreferredSize(new Dimension(300,300));
            ImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx=0;
            gridBagConstraints.gridy=0;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
            gridBagConstraints.insets = new Insets(5, 3, 0, 3);
            
            Panel.add(ImageLabel, gridBagConstraints);

            Exit = new JButton("Exit");
            Exit.setMinimumSize(new Dimension(100,35));
            Exit.setPreferredSize(new Dimension(100,35));
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.gridx=0;
            gridBagConstraints3.gridy=8;
            gridBagConstraints3.anchor = java.awt.GridBagConstraints.SOUTH;
            gridBagConstraints3.insets = new Insets(5, 3, 0, 3);
            Exit.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                                hide();
                        }
                        } );
            
  
            Panel.add(Exit, gridBagConstraints3);
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            getContentPane().add(Panel, BorderLayout.CENTER);
            
            pack();//for drawing
            
            setResizable(false);
    }
                
}

it did try this :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Color;

public class index extends JFrame {

        private JButton Exit;
	    private Image img = new ImageIcon("images/GetAttachment.aspx-(22).jpg").getImage();

          public index() {
                super();
                
		Dimension size = new Dimension(700, 500);
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
		
                addWindowListener(new   WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                         hide();
                        }
                } );
                
                JPanel Panel = new JPanel();
                Panel.setLayout(new GridBagLayout());
            Panel.setMinimumSize(new Dimension(700, 500));
            Panel.setPreferredSize(new Dimension(700, 500));
            

            ImageIcon image = new ImageIcon("images/GetAttachment.aspx-(22).jpg");
            JLabel ImageLabel = new JLabel(image);
            ImageLabel.setMinimumSize(new Dimension(300,300));
            ImageLabel.setPreferredSize(new Dimension(300,300));
            ImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx=0;
            gridBagConstraints.gridy=0;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
            gridBagConstraints.insets = new Insets(5, 3, 0, 3);
            
            Panel.add(ImageLabel, gridBagConstraints);

            Exit = new JButton("Exit");
            Exit.setMinimumSize(new Dimension(100,35));
            Exit.setPreferredSize(new Dimension(100,35));
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.gridx=0;
            gridBagConstraints3.gridy=8;
            gridBagConstraints3.anchor = java.awt.GridBagConstraints.SOUTH;
            gridBagConstraints3.insets = new Insets(5, 3, 0, 3);
            Exit.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                                hide();
                        }
                        } );
            
  
            Panel.add(Exit, gridBagConstraints3);
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            getContentPane().add(Panel, BorderLayout.CENTER);
            
            pack();//for drawing
            
            setResizable(false);
    }

	  public void paintComponent(Graphics g) {
		    g.drawImage(img, 0, 0, null);
		  }	  
}

but it does not work :(
any help?

the problem have been solved,
By:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Color;

public class index extends JFrame {

        private JButton Exit;
        private Image img = new ImageIcon("images/so0o0o0o0os.jpg").getImage();


          public index() {
                super();
                
                Dimension size = new Dimension(700, 500);
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
                
                addWindowListener(new   WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                         hide();
                        }
                } );
                
                JPanel Panel = new JPanel(){
                  public void paintComponent(Graphics g) {
                    g.drawImage(img, 0, 0, null); }
                  };
                Panel.setLayout(new GridBagLayout());
            Panel.setMinimumSize(new Dimension(700, 500));
            Panel.setPreferredSize(new Dimension(700, 500));


            Exit = new JButton("Exit");
            Exit.setMinimumSize(new Dimension(100,35));
            Exit.setPreferredSize(new Dimension(100,35));
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.gridx=0;
            gridBagConstraints3.gridy=8;
            gridBagConstraints3.anchor = java.awt.GridBagConstraints.SOUTH;
            gridBagConstraints3.insets = new Insets(5, 3, 0, 3);
            Exit.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                                hide();
                        }
                        } );
            
  
            Panel.add(Exit, gridBagConstraints3);
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            
            Panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
            getContentPane().add(Panel, BorderLayout.CENTER);
            
            pack();//for drawing
            
            setResizable(false);
    }     
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.