Java JMenu(Action a) Constructor
Syntax
JMenu(Action a) constructor from JMenu has the following syntax.
public JMenu(Action a)
Example
In the following code shows how to use JMenu.JMenu(Action a) constructor.
import java.awt.event.ActionEvent;
/*from ww w .j ava 2 s. c o m*/
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu(new AbstractAction("File"){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("action");
}
});
bar.add(menu);
menu.add(new JMenuItem("Close"));
menu.add(new JSeparator()); // SEPARATOR
menu.add(new AbstractAction("Exit"){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("action");
}
});
setJMenuBar(bar);
add(new JLabel("A placeholder"));
pack();
setSize(300, 300);
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}
Home »
Java Tutorial »
javax.swing »
Java Tutorial »
javax.swing »