Results 1 to 4 of 4
  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default diplaying java images

    Hi I'm trying to create a program that displays buttons(JButton) as well as images inside of panels. Everything is going well except my images aren't showing up. Buttons are fine its just the images that's giving me the problem. here's the code thanks,
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Work extends JFrame
    {
    	private final int WINDOW_WIDTH = 1000;				//Window width
    	private final int WINDOW_HEIGHT = 1000;				//Window height
    	private JPanel imagePanel;
    	private JLabel imageLabel;
    	private JLabel ImageIcon;
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    
    	public Work()
    		{
    			// Set the title bar text.
    			setTitle("Border Layout");
    
    			setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    			// Specify an action for the close button.
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			//Add a BorderLayout manager to the content pane.
    		setLayout(new BorderLayout());
    
    		//Create five panels.
    				JPanel panel1 = new JPanel();
    				JPanel panel2 = new JPanel();
    				JPanel panel3 = new JPanel();
    				JPanel panel4 = new JPanel();
    				JLabel panel5 = new JLabel();
    
    				JButton button1 = new JButton("Click to Enter");
    				JButton button2 = new JButton("Exit");
    				JLabel label1 = new JLabel("bible");
    				JLabel label2 = new JLabel ();
    				JLabel label3 = new JLabel();
    
    				panel1.add(button1);
    				panel2.add(button2);
    				panel3.add(label1);
    				panel4.add(label2);
    				panel5.add(label3);
    
    				add(panel1, BorderLayout.CENTER);
    				add(panel2, BorderLayout.SOUTH);
    				add(panel3, BorderLayout.WEST);
    				add(panel4, BorderLayout.EAST);
    				add(panel5, BorderLayout.NORTH);
    
    				pack();
    				setVisible(true);
    			}
    			private void ImageIcon()
    			{
    				ImageIcon image = new ImageIcon("bible3.gif");
    				JLabel label1 = new JLabel(image);
    				pack();
    				setVisible(true);
    
    			}
    			private void label1()
    			{
    				ImageIcon image = new ImageIcon("bible3.gif");
    				JLabel label1 = new JLabel(image);
    				pack();
    				setVisible(true);
    			}
    
    			public static void main(String[] args)
    			{
    				new Work();
    			}
    
    
    
    		}
    ps the default size isn't showing what I set it up to be its not that important to me now, but I know I'm going to have to fix it in the future.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default Re: diplaying java images

    The major issue is that you are re-declaring your labels as JLabels inside of your constructor and in your ImageIcon method. And you don't need these methods.

    Java Code:
      private void ImageIcon()            {
                    ImageIcon image = new ImageIcon("bible3.gif");
                    JLabel label1 = new JLabel(image);
                    pack();
                    setVisible(true);
     
                }
                private void label1()
    
                {
                    ImageIcon image = new ImageIcon("bible3.gif");
                    JLabel label1 = new JLabel(image);
                    pack();
                    setVisible(true);
    
                }


    Just put add the two lines after your Label1 assignement
    Java Code:
                    ImageIcon image = new ImageIcon("bible3.gif);
                              label1 = new JLabel(image);
    Last edited by jim829; 03-06-2013 at 02:21 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: diplaying java images

    Thanks really appreciate the help

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: diplaying java images

    Quote Originally Posted by jumpman8947 View Post
    Java Code:
    	private final int WINDOW_WIDTH = 1000;				//Window width
    	private final int WINDOW_HEIGHT = 1000;				//Window height
    Who'd have thought it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2012, 11:15 AM
  2. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  3. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  4. Replies: 7
    Last Post: 07-22-2008, 05:54 PM
  5. Help using images in Java
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:54 AM

Posting Permissions

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