I am trying a simple code to add ImageIcons to Jbutton. I tried with JmenuBar too, but not able to import them on GUI application. This is the starter code I am using and the image address is correct.
import javax.swing.*;
import java.awt.*;
class demo
{
//constructor being used
demo()
{
//creating our Frame object
JFrame frame = new JFrame("JToolBar Demo");
//creating out tool bar object
JToolBar toolbar = new JToolBar("Applications");
//adding button icons instead of text
JButton btnCalendar = new JButton(new ImageIcon("exit.png"));
JButton btnClock = new JButton(new ImageIcon("exit2.png"));
JButton btnContacts = new JButton(new ImageIcon("src/images/Contacts1.png"));
JButton btnMail = new JButton(new ImageIcon("src/images/Mail1.png"));
JButton btnMessages = new JButton(new ImageIcon("src/images/Message1.png"));
//creating combo box object
//JComboBox fonts=new JComboBox();
//adding combo box items
//fonts.addItem("Arial");
//fonts.addItem("Times New Roman");
//fonts.addItem("Comic Sans Ms");
//adding combo box and buttons to our tool bar
toolbar.add(btnCalendar);
toolbar.add(btnClock);
toolbar.add(btnContacts);
toolbar.add(btnMail);
toolbar.add(btnMessages);
//toolbar.add(fonts);
//setting properties for the Frame
frame.setLayout(new BorderLayout());
frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setVisible(true);
}
public static void main(String args[]) throws Exception
{
//calling our class
new demo();
}
}