import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
public class Swing {
public Swing() {
}
public static void main(String[] args) {
ImageIcon img= new ImageIcon("C:\\Users\\Sovan\\Desktop\\icon.png");
//Add Frame
JFrame frame = new JFrame("Menu");
frame.setIconImage(img.getImage());
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Just Create MenuBar
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
//Add an Jmenu
JMenu file=new JMenu("Menu");
menubar.add(file);
//Add an JMenuItem
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
exit.addActionListener(new exitapp());
JMenu help=new JMenu("Help");
menubar.add(help);
JMenuItem about =new JMenuItem("About");
help.add(about);
//Add JButton
JButton exitButton= new JButton("Exit!");
frame.add(exitButton);
exitButton.addActionListener(new exitapp());
exitButton.setSize(40,40);
frame.setVisible(true);
}
//exit app
static class exitapp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
+Output
No comments:
Post a Comment