Results 1 to 4 of 4
  1. #1
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Question Images not displaying in JFrame.

    I have My images in Logo().
    My Image displays fine when I use this code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class DisplayLogo {
    
        public static void main(String[] args) {
    	Logo logo = new Logo();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(logo);
    	closeLogo.setBounds(50, 50, 400, 100);
            f.setSize(800, 520);
            f.setVisible(true);
    	f.setResizable(false);
    	f.setLocationRelativeTo(null);
        }
    }
    But now I want to add a JButton to the jframe. And I do this:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class ModScape {
    
        public static void main(String[] args) {
    	Logo logo = new Logo();
            JFrame f = new JFrame("ModScape");
    	f.setLayout (null);
    	JButton closeLogo = new JButton("Click to Start");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(logo);
            f.add(closeLogo);
    	closeLogo.setBounds(50, 50, 400, 100);
            f.setSize(800, 520);
            f.setVisible(true);
    	f.setResizable(false);
    	f.setLocationRelativeTo(null);
        }
    }
    Now my button is displayed, but now my images in the background are not being displayed.

    Here are my problems:
    Java Code:
    //When I remove
    f.setLayout (null);
    //My image is displayed but my jbutton dosen't obey:
    closeLogo.setBounds(50, 50, 400, 100);
    //and my background image is covered by the jbutton
    How can I get my JBUTTON in the place where I want it, and my Background displayed where I want it?
    The Problem is with the f.SetLayout method. When I get rid of it, my jbutton dosen't want to work, but with it, my image dosen't get displayed.
    Last edited by jlennards; 07-12-2011 at 03:12 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I recommend against using a null layout...let a proper layout manager handle the layout of your components. For instance, create a JPanel, set its layout as appropriate if needed, add the components to this JPanel, and add said JPanel to the content pane of the JFrame. And make note that JPanels with different layouts can be combined together
    Suggested reading: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The content pane of a frame is a JPanel that uses a BorderLayout.

    In the first example when you add a component to frame it get added to "CENTER" of the BorderLayout. This means it is sized to take up all the available space. This is done by the layout manager whicihl invokes the setSize(...) method of the Logo class.

    In the second example you use a null layout so you are responsible to setting the size and location of every component added to the frame. You didn't set the size of the LOGO so its default size is (0, 0) which means there is nothing to paint. Even if you set the size it still won't paint the way you want because components are actually painted in the reverse order they are added to the Container. Which mean the button would be painted first and then the Logo would be painted on top. If you change the order in which the components are added it should work.

    However a better solution would be one of the suggestions found in: Background Panel Java Tips Weblog

  4. #4
    jlennards's Avatar
    jlennards is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Ah, I see. Thanks, I got it working now.
    Last edited by jlennards; 07-12-2011 at 03:50 AM.

Similar Threads

  1. Displaying multiple images
    By asmitarnd in forum AWT / Swing
    Replies: 2
    Last Post: 04-19-2011, 12:14 PM
  2. Displaying moving images
    By Maulik in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2011, 12:00 AM
  3. Displaying large images
    By pir8ped in forum AWT / Swing
    Replies: 2
    Last Post: 01-21-2009, 09:20 PM
  4. [SOLVED] Need help in displaying images
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-21-2008, 05:36 AM
  5. Images not displaying in JSP in IE7
    By chadscc in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2007, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •