Hi Guys,
I am providing the code i wrote for java swing.In it the images for creating main menu is taken from net(via URL).
How can i use a local image , instead of remote one.
Code :
import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.SwingConstants; import java.awt.image.BufferedImage; public class MenuTest { public static void main(String[] argv) throws Exception { // Create the menu bar JMenuBar menuBar = new JMenuBar(); // Create a menu JMenu menu = new JMenu("File"); BufferedImage image = ImageIO.read(new URL("http://www.dllc.org/thumbnail.aspx?path=%2FContent%2F10923%2Fthumbnails%2F331827-thumbnail.jpg&width=40&height=40")); menu.setHorizontalTextPosition(SwingConstants.CENTER); menu.setVerticalTextPosition(SwingConstants.BOTTOM); menu.setIcon(new ImageIcon(image)); menuBar.add(menu); JMenu menu1 = new JMenu("View"); BufferedImage image1 = ImageIO.read(new URL("http://www.gettyicons.com/free-icons/119/sleek-xp-software/png/48/compupic_48.png")); menu1.setHorizontalTextPosition(SwingConstants.CENTER); menu1.setVerticalTextPosition(SwingConstants.BOTTOM); menu1.setIcon(new ImageIcon(image1)); menuBar.add(menu1); // Create a menu item JMenuItem item = new JMenuItem("Test Item"); JMenuItem new_item=new JMenuItem("New"); JMenuItem save=new JMenuItem("Save"); JMenuItem open=new JMenuItem("Open"); menu.add(new_item); menu.add(item); menu.add(open); menu.add(save); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(menuBar); frame.setSize(500, 550); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Screen Shot
img_Menu.jpg