Created
October 8, 2013 01:40
-
-
Save matiasfha/6878072 to your computer and use it in GitHub Desktop.
Java Swing MenuBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyEvent; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFrame; | |
import javax.swing.JMenu; | |
import javax.swing.JMenuBar; | |
import javax.swing.JMenuItem; | |
import javax.swing.SwingUtilities; | |
public class Example extends JFrame { | |
public Example() { | |
initUI(); | |
} | |
public final void initUI() { | |
JMenuBar menubar = new JMenuBar(); | |
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png")); | |
JMenu file = new JMenu("File"); | |
file.setMnemonic(KeyEvent.VK_F); | |
JMenuItem eMenuItem = new JMenuItem("Exit", icon); | |
eMenuItem.setMnemonic(KeyEvent.VK_E); | |
eMenuItem.setToolTipText("Exit application"); | |
eMenuItem.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
System.exit(0); | |
} | |
}); | |
file.add(eMenuItem); | |
menubar.add(file); | |
setJMenuBar(menubar); | |
setTitle("Simple menu"); | |
setSize(300, 200); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
Example ex = new Example(); | |
ex.setVisible(true); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment