OnJava8-Examples/ui/SimpleMenus.java
2015-12-15 11:47:04 -08:00

41 lines
1.2 KiB
Java

// ui/SimpleMenus.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static onjava.SwingConsole.*;
public class SimpleMenus extends JFrame {
private JTextField t = new JTextField(15);
private ActionListener al = e ->
t.setText(((JMenuItem)e.getSource()).getText());
private JMenu[] menus = {
new JMenu("Winken"), new JMenu("Blinken"),
new JMenu("Nod")
};
private JMenuItem[] items = {
new JMenuItem("Fee"), new JMenuItem("Fi"),
new JMenuItem("Fo"), new JMenuItem("Zip"),
new JMenuItem("Zap"), new JMenuItem("Zot"),
new JMenuItem("Olly"), new JMenuItem("Oxen"),
new JMenuItem("Free")
};
public SimpleMenus() {
for(int i = 0; i < items.length; i++) {
items[i].addActionListener(al);
menus[i % 3].add(items[i]);
}
JMenuBar mb = new JMenuBar();
for(JMenu jm : menus)
mb.add(jm);
setJMenuBar(mb);
setLayout(new FlowLayout());
add(t);
}
public static void main(String[] args) {
run(new SimpleMenus(), 200, 150);
}
}