CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: java images

  1. #1
    Join Date
    Feb 2013
    Posts
    22

    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,
    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
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java images

    You aren't calling the methods that add the icons and you in those methods you aren't adding the JLabels containing the icons to the container.
    BTW I'm not sure about your code, you seem to be calling pack() and setVisible() in too many places.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured