0

I need to set an image as a background to the JPanel I have created. I have made a basic calculator.

I tried a code that someone told me to try and it set the image as the background but now I've lost my calculator buttons etc. I'm guessing this is because I added it to my JFrame?

But I don't know where to go from here.

I am new to this, I know my code probably isn't even that good, but it works for now. And it will be looked at and criticized later but for now I need this background added :/ Can someone help , please and thank you!

Please excuse the name GridBag2.

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

  /**
 * Created by IntelliJ IDEA.
 * Date: 09/01/2015
 * Time: 15:52
 */
public class GridBag2 extends JFrame implements ActionListener
{


   private JPanel panel=new JPanel(new GridBagLayout());


   private GridBagConstraints gBC = new GridBagConstraints();

   private JButton zeroButton = new JButton("0");
   private JButton oneButton = new JButton("1");
   private JButton twoButton = new JButton("2");
   private JButton threeButton = new JButton("3");
   private JButton fourButton = new JButton("4");
   private JButton fiveButton = new JButton("5");
   private JButton sixButton = new JButton("6");
   private JButton sevenButton = new JButton("7");
   private JButton eightButton = new JButton("8");
   private JButton nineButton = new JButton("9");

   private JButton addButton = new JButton("+");
   private JButton subButton = new JButton("−");
   private JButton multButton = new JButton(" X ");
   private JButton divideButton = new JButton(" ÷ ");
   private JButton equalButton= new JButton("=");
   private JButton clearButton = new JButton("C");


   private JTextArea input=new JTextArea("");



   Double number1,number2,result;
   int add=0,sub=0,mult=0,divide=0;



   public GridBag2()
   {


      zeroButton.addActionListener(this);
      oneButton.addActionListener(this);
      twoButton.addActionListener(this);
      threeButton.addActionListener(this);
      fourButton.addActionListener(this);
      fiveButton.addActionListener(this);
      sixButton.addActionListener(this);
      sevenButton.addActionListener(this);
      eightButton.addActionListener(this);
      nineButton.addActionListener(this);

      addButton.addActionListener(this);
      subButton.addActionListener(this);
      multButton.addActionListener(this);
      divideButton.addActionListener(this);
      equalButton.addActionListener(this);
      clearButton.addActionListener(this);



      gBC.insets = new Insets(5, 5, 5, 5);


      gBC.gridx = 1;
      gBC.gridy = 0;
      gBC.gridwidth = 4;
      gBC.fill = GridBagConstraints.BOTH;
      panel.add(input, gBC);


      gBC.gridx = 2;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(zeroButton, gBC);


      gBC.gridx = 1;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(oneButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(twoButton, gBC);


      gBC.gridx = 2;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(threeButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(fourButton, gBC);

      gBC.gridx = 2;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(fiveButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(sixButton, gBC);

      gBC.gridx = 1;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(sevenButton, gBC);

      gBC.gridx = 2;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(eightButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(nineButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(addButton, gBC);

      gBC.gridx = 3;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(subButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 1;
      gBC.gridwidth = 1;
      panel.add(divideButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 2;
      gBC.gridwidth = 1;
      panel.add(multButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 3;
      gBC.gridwidth = 1;
      panel.add(equalButton, gBC);

      gBC.gridx = 4;
      gBC.gridy = 4;
      gBC.gridwidth = 1;
      panel.add(clearButton, gBC);

      setVisible(true);
      setSize(300, 340);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      input.setEditable(false);

      getContentPane().add(panel);


      setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));




   }//GridBag2


   public void actionPerformed(ActionEvent e)
   {

      Object source = e.getSource();

      if(source==clearButton)
      {
         number1=0.0;
         number2=0.0;
         input.setText("");
      }//if

      if (source == zeroButton)
      {
         input.setText(input.getText() + "0");
      }//if

      if (source == oneButton)
      {
         input.setText(input.getText() + "1");
      }//if
      if (source == twoButton)
      {
         input.setText(input.getText() + "2");
      }//if
      if (source == threeButton)
      {
         input.setText(input.getText() + "3");
      }//if
      if (source == fourButton)
      {
         input.setText(input.getText() + "4");
      }//if
      if (source == fiveButton)
      {
         input.setText(input.getText() + "5");
      }//if
      if (source == sixButton)
      {
         input.setText(input.getText() + "6");
      }//if
      if (source == sevenButton)
      {
         input.setText(input.getText() + "7");
      }//if
      if (source == eightButton)
      {
         input.setText(input.getText() + "8");
      }//if
      if (source == nineButton)
      {
         input.setText(input.getText() + "9");
      }//if
      if (source == addButton)
      {
         number1=number_reader();
         input.setText("");
         add=1;
         sub=0;
         divide=0;
         mult=0;
      }//if
      if (source == subButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=1;
         divide=0;
         mult=0;
      }//if
      if (source == divideButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=0;
         divide=1;
         mult=0;
      }//if
      if (source == multButton)
      {
         number1=number_reader();
         input.setText("");
         add=0;
         sub=0;
         divide=0;
         mult=1;
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(add>0)
         {
            result=number1+number2;
            input.setText(Double.toString(result));
         }//if
      }//if

      if(source==equalButton)
      { number2=number_reader();

         if(sub>0)
         {
            result=number1-number2;
            input.setText(Double.toString(result));
         }//if
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(divide>0)
         {
            result=number1/number2;
            input.setText(Double.toString(result));
         }//if
      }//if
      if(source==equalButton)
      { number2=number_reader();

         if(mult>0)
         {
            result=number1*number2;
            input.setText(Double.toString(result));
         }//if
      }//if
   }//actionPerformed


   public double number_reader()
   {
      Double num1;
      String s;
      s=input.getText();
      num1=Double.valueOf(s);

      return num1;
   }//number_reader



   public static void main(String[] args){

      GridBag2 gui = new GridBag2();


   }//main



}//class
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Note that an example of creating a BG image panel can be achieved in around 30 lines of code. A BG image with a component on top, 31 LOC. – Andrew Thompson Jan 14 '15 at 23:44
  • 3
    `JLabel` is a bad choice for this kind of thing, as it will not calculate the preferred size of the child components, but will only calculate the preferred size based on the image (and the text of the label), this could result in your UI been sized incorrectly... – MadProgrammer Jan 14 '15 at 23:51

2 Answers2

0
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));

This line doesn't add the JLabel to you panel. This line makes the JLabel the root control of the JFrame. The replaces the content pane of the JFrame with the JLabel - a JFrame only has one content pane.

You probably wanted to treat it the same way as every other control. Something like:

JLabel background = new JLabel(new new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg"));
gBC.gridx = some number;
gBC.gridy = some number;
gBC.gridwidth = some number;
gBC.gridheight = some number;
panel.add(background, gBC);
user253751
  • 57,427
  • 7
  • 48
  • 90
  • The OP could call `setContentPane` BEFORE adding any new content to the frame...you're right, it does "replace" the current content pane which is the core issue... – MadProgrammer Jan 14 '15 at 23:48
  • I have tried this and it worked to a certain extent. It has added the background but distorted some of my buttons. It has made the bottom row of my buttons my zeroButton, clearButton, minusButton and my oneButton are all stretched touching the bottom of the frame. They aren't the same size as the other buttons anymore? – Sarah Jones Jan 15 '15 at 00:08
-2
getContentPane().add(panel);

setContentPane(new JLabel(new ImageIcon("C:\\Users\\Cara\\Pictures\\christmas.jpg")));

My guess is that you need to switch the order of these two lines. You're adding the JPanel to the JFrame's default content pane, but then you're setting the content pane to something else (a JLabel), effectively throwing away the old content pane, which you added the panel to.

On a side note, it's good practice to use java.io.File.separator in your filename for the image, rather than hardcoding backslashes into the string. So you would instead write:

"C:"+File.separator+"Users"+File.separator+"Cara"+File.separator+"Pictures"+File.separator+"christmas.jpg"

while being sure to import java.io.File at the top of your code. This ensures that it won't break on a unix-based system that uses forward slashes instead of backslashes like Windows does.

  • 1
    Please stop guessing.. The first suggestion would simply change what is seen from the icon to the panel. On the 'side note', this is apparently an application resource that should be loaded using `getResource(String)` where the `String` uses `/` as path separator, since it is going to form an `URL` (always forward slash), rather than a `File`. – Andrew Thompson Jan 14 '15 at 23:48
  • I have tried swapping the order and it still doesn't work :/ – Sarah Jones Jan 15 '15 at 00:01
  • And it's even better practice to use the `getResource` method of the `Class` class, which erases dependency on particular operating systems. – TNT Jan 15 '15 at 00:15