0
public static void main(String[] args) {
    myForm.setSize(500, 400);
    myForm.setLocation(0, 0);
    myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);              
    myForm.setVisible(true);
    // try {
        // myForm.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Heshamm\\Desktop\\light.jpg"))) ) );
         //} catch (IOException e1) {
         //JOptionPane.showMessageDialog(null, "Error !!!", null, JOptionPane.PLAIN_MESSAGE);
         //}
   draw.mai(myForm, jp1, button, tf[0], tf[1], tf[2], tf[3]);

And this is the mai() function in another class

        public void mai(JFrame myForm, JPanel jp1, JButton[] button, JTextField tf1, JTextField tf2, JTextField tf3, JTextField tf4)
    {

        myForm.getContentPane().removeAll();
        jp1.removeAll();
        jp1.add(button[0]);
        jp1.add(button[1]);
        jp1.add(button[2]);
        jp1.add(button[3]);
        jp1.add(button[9]);
        jp1.add(button[4]);
        tf1.setText("");
        tf2.setText("");
        tf3.setText("");
        tf4.setText("");
        Hotel.update();
    }

Okay so, I have this code, and I wanted the img to be displayed as a background behind the buttons, but when I uncomment try and whats inside it, the frame only displays the background without the buttons, how can I fix it?

And here's the update() function.

    public static void update()
{
    cbChoice="";
    myForm.add(jp1);
    myForm.repaint();
    myForm.revalidate();
}

Thanks in advance.

mhasan
  • 3,703
  • 1
  • 18
  • 37
Ahmed
  • 49
  • 1
  • 7

1 Answers1

1

You can add an image to JLabel and strech the JLabel like background.

There is a simple example :

   ImageIcon icon = createImageIcon("images/middle.gif");
   . . .
   JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
   label1.setSize(frame width, frame height);
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • I get this error "The method createImageIcon(String) is undefined for the type Hotel" – Ahmed Sep 24 '16 at 10:51
  • Please be sure from your (pic) file path. – Coder ACJHP Sep 24 '16 at 10:53
  • ImageIcon icon = createImageIcon("C:\\Users\\Heshamm\\Desktop\\light.jpg"); Same path as the code I posted in the original post, and the other can sucessfully find the pic. – Ahmed Sep 24 '16 at 10:57
  • Ok, try this way if you want : JLabel label = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("image path")))). – Coder ACJHP Sep 24 '16 at 11:02