OnJava8-Examples/ui/SimpleMenus.java

41 lines
1.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// ui/SimpleMenus.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static onjava.SwingConsole.*;
2015-06-15 17:47:35 -07:00
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);
}
2015-09-07 11:44:36 -06:00
}