1

I have tried numerous ways of added a jpg image into the Jframe without success, the code will compile but the image wont appear. Th image is stored in a source file in the project! Thanks for the help.

public class GordonsWindow extends JFrame {
    public GordonsWindow() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu();
        JMenu menuStores = new JMenu();
        JMenuItem menuFileExit = new JMenuItem();

        JMenuItem menuStoresArmagh = new JMenuItem();

        JPanel paneStores = new JPanel(new FlowLayout());
        paneStores = new JPanel();
        paneStores.setPreferredSize(new Dimension(500,300));
        paneStores.setBackground(Color.blue);
        JButton ArmaghButton = new JButton();
        ImageIcon icon = new ImageIcon("Gordons.jpg"); 
        JLabel label = new JLabel(); 
        label.setIcon(icon);        

        ArmaghButton.setText("Armagh");

        paneStores.add(ArmaghButton);
        paneStores.add(label);

        add(paneStores);

        menuFile.setText("File");
        menuFileExit.setText("Exit");

        menuStores.setText("Stores");
        menuStoresArmagh.setText("Armagh");

        ArmaghButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Armagh.windowActivated();
            }
        });

        menuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GordonsWindow.this.windowClosed();
            }
        });

        menuFile.add(menuFileExit);
        menuBar.add(menuFile);

        menuBar.add(menuStores);
        menuStores.add(menuStoresArmagh);

        setTitle("Gordons Chemists Application");
        setJMenuBar(menuBar);
        setSize(new Dimension(800, 800));

        //Add Window Listener
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                GordonsWindow.this.windowClosed();
            }
        });
    }

    protected void windowClosed() {
        System.exit(0);
    }
}
admdrew
  • 3,790
  • 4
  • 27
  • 39
colliec
  • 35
  • 4
  • http://stackoverflow.com/questions/13428796/adding-background-pic-to-jframe You may want to take a look at that – washcloth Feb 13 '14 at 15:23
  • Maybe a resource loading problem? Right now the image needs to be located in the same package/folder as GordonsWindow or in a folder directly on the classpath... – Rob Feb 13 '14 at 15:47

2 Answers2

2

You want to load it through your class path. Simply

ImageIcon icon = new ImageIcon(GordonsWindow.class.getResource("/images/Gordons.jpg");

Where images is located directly in the src

ProjectRoot
         src
            images
                 Gordons.jpg
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

sorry; missed that label.seticon() the other code not needed for the example confuzled me.

Next time use an SSCCE

and then you need to draw it on a component somehow;

Method i use is:

JPanel imageholder=new JPanel()
        {
            protected void paintComponent(Graphics g)
            {
                super.paintComponent( g)
                g.drawImage(ImageVariable,0,0);
            }
        };
        j.setBounds(<insert size here>);
masterX244
  • 221
  • 2
  • 11