Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (woodrow73):

Java, how to set a jpg as a background in a JFrame.

OpenStudy (woodrow73):

Within the context of using images - with the ImageIcon class, I have learned that it's possible to set the ImageIcon object as a picture by doing the following: 1) declare an instance of the ImageIcon class referencing the jpg you want ``` ImageIcon ic = new ImageIcon("C:\\woodrowPic.jpg"); ``` 2) add the object to the constructor of the JLabel class: ``` JLabel lab = new JLabel(ic); ``` 3) add the JLabel object to a JPanel's add method: ``` JPanel pan = new JPanel(); pan.add(lab); ``` 4) add the JPanel to the JFrame via the JFrame's add method: ``` JFrame jf = new JFrame("Here is a pic"); //this constructor is the title of window jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exits program when closed jf.add(pan); //adds JPanel to the JFrame jf.pack(); //sets the dimensions of the window to the picture's size jf.setVisible(true); //sets the window to visible ``` Now there is a window with a picture inside of it-- However, this is a JLabel, and it is not possible to place other components ontop of a JLabel (such as JButton || more JLabels || JTextField), hence I am unable to place any components ontop of the picture -- any JButton added would simply be in the white-space of the JFrame to the right of the picture or below it. My goal is to be able to place components ontop of a jpg in a JFrame. I have found that JButtons can even reference a picture-- ``` ImageIcon ic = new ImageIcon("C:\\woodrowPic.jpg"); JButton but = new JButton(ic); JFrame jf = new JFrame("Giant Button Picture"); jf.add(but); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); ``` Apparantly, a JButton can be added directly to a JFrame -- with the ImageIcon object in the JButton's constructor -- the entire image has become a button. I'm happy that I can get a whole picture to be a button - though I'm more-or less striving to be able to put multiple buttons on a picture -- as well as the fact that I only want a portion of the picture to be a button, not the whole picture. For instance- here is a giant JButton: (I attatched a jpg -- it will appear somewhere.. most likely.) The whole picture is a button as opposed to just the 'button shaped' portion. fyi -- in terms of adding a JButton as a picture - this is also proper syntax: adding the button to the panel, then adding it to the frame - instead of adding the button to the frame directly- ``` ImageIcon ic = new ImageIcon("C:\\woodPic.jpg"); JButton but = new JButton(ic); JButton b22 = new JButton("A button on the side of the pic/frame"); JPanel pan = new JPanel(); pan.add(but); pan.add(b22); JFrame jf = new JFrame("Pic display"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.add(pan); jf.pack(); jf.setVisible(true); ``` I also added a 2nd JButton onto the JPanel before adding it to the JFrame. This button 'b22' will be on the side of the picture, awkwardly placed. In terms of setting backgrounds as a picture, from where I can add JPanel components ontop -- here are some failed attempts: -try setting ImageIcon object directly to JFrame; causes error ``` JFrame jf = new JFrame(); ImageIcon ic = new ImageIcon("C:\\officeeclipsewood\\garwood.jpg"); jf.add(ic); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); ``` -attempt adding a JPanel ontop of another JPanel to see if they overlay: ``` JFrame jf = new JFrame(); ImageIcon ic = new ImageIcon("C:\\officeeclipsewood\\garwood.jpg"); JLabel lab = new JLabel(ic); JPanel pan = new JPanel(); pan.add(lab); JPanel p2 = new JPanel(); JButton buttonOnP2 = new JButton("Test"); p2.add(buttonOnP2); jf.add(pan); jf.add(p2); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); ``` The second panel completely blocks off the first panel that I added to the JFrame - so nothing from the first JPanel pan displays when compiled & run. I'm currently learning layout managers - hopefully I learn something that helps - any input is appreciated. The background of a JPanel can be set to one of 13 colors such as: ``` JPanel pan = new JPanel(); pan.setBackground(Color.CYAN); ``` If only setting a jpg as a background was as easy.

OpenStudy (anonymous):

What sort of project have you had to do with JFrame library?

OpenStudy (woodrow73):

None in particular; though adding JPanel components ontop of a picture would be rad. I've heard anything is possible with javafx - but I'm currently looking for a swing/awt solution since it's what I'm learning at the moment.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!