Studyon Minte9.com
Java SE




Menu



Attach a menu to frame.

package testexample; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 600, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.setVisible(true); } public Test() { initMenu(); } public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); JMenuItem menuItemNew = new JMenuItem("New"); JMenuItem menuItemOpen = new JMenuItem("Open"); menu.setMnemonic('F'); menuItemNew.setMnemonic('N'); menuItemOpen.setMnemonic('O'); menuBar.add(menu); menu.add(menuItemNew); menu.add(menuItemOpen); // View JMenu menuView = new JMenu("View") {{ setMnemonic('V'); add (new JMenu("Toolbars") {{ add (new JCheckBoxMenuItem("Buttons") {{ setMnemonic('B'); addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // } }); }}); }}); }}; menuBar.add(menuView); /* --- Look here --- */ this.setJMenuBar(menuBar); } }
 


http://www.herongyang.com/Swing/JMenuBar-Check-Box-Menu-Item-Test-Program.html