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);
}
}
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
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>);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.