#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    Displaying java image


    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,
    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
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    my images aren't showing up
    Where are you trying to show any images? What component is supposed to show the image?

    Variables defined inside of a method are local to that method. When the method exits, the variable and its value go away. If you want a variable to exist and have a value after a method exists, you need to define the variable outside of any method.

    There are 4 different variables defined with the same name in the posted code: label1

IMN logo majestic logo threadwatch logo seochat tools logo