diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java new file mode 100644 index 0000000..4b6ae83 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3005.java @@ -0,0 +1,118 @@ +/* Listing3005.java */
+
+import java.awt.*;
+import java.awt.event.*;
+
+class MainMenu4
+extends MenuBar
+{
+ private MenuItem miRueck;
+ private CheckboxMenuItem miFarbe;
+
+ private static void
+ addNewMenuItem(Menu menu, String name, ActionListener listener)
+ {
+ int pos = name.indexOf('&');
+ MenuShortcut shortcut = null;
+ MenuItem mi;
+ if (pos != -1) {
+ if (pos < name.length() - 1) {
+ char c = name.charAt(pos + 1);
+ shortcut=new MenuShortcut(Character.toLowerCase(c));
+ name=name.substring(0,pos)+name.substring(pos + 1);
+ }
+ }
+ if (shortcut != null) {
+ mi = new MenuItem(name, shortcut);
+ } else {
+ mi = new MenuItem(name);
+ }
+ mi.setActionCommand(name);
+ mi.addActionListener(listener);
+ menu.add(mi);
+ }
+
+ public MainMenu4(ActionListener listener)
+ {
+ Menu m;
+
+ //Menü "Größe"
+ m = new Menu("Größe");
+ addNewMenuItem(m, "&Größer", listener);
+ addNewMenuItem(m, "&Kleiner", listener);
+ m.addSeparator();
+ addNewMenuItem(m, "B&eenden", listener);
+ add(m);
+
+ //Menü "Position"
+ m = new Menu("Position");
+ addNewMenuItem(m, "&Links", listener);
+ addNewMenuItem(m, "&Rechts", listener);
+ addNewMenuItem(m, "&Oben", listener);
+ addNewMenuItem(m, "&Unten", listener);
+ add(m);
+ }
+}
+
+public class Listing3005
+extends Frame
+implements ActionListener
+{
+ public static void main(String[] args)
+ {
+ Listing3005 wnd = new Listing3005();
+ }
+
+ public Listing3005()
+ {
+ super("Menü-ActionEvents");
+ setLocation(100,100);
+ setSize(300,200);
+ setMenuBar(new MainMenu4(this));
+ setVisible(true);
+ addWindowListener(new WindowClosingAdapter(true));
+ }
+
+ public void paint(Graphics g)
+ {
+ Insets in = getInsets();
+ Dimension d = getSize();
+ g.setColor(Color.red);
+ g.drawLine(
+ in.left, in.top,
+ d.width - in.right, d.height - in.bottom
+ );
+ g.drawLine(
+ in.left, d.height - in.bottom,
+ d.width - in.right, in.top
+ );
+ }
+
+ public void actionPerformed(ActionEvent event)
+ {
+ String cmd = event.getActionCommand();
+ if (cmd.equals("Größer")) {
+ Dimension d = getSize();
+ d.height *= 1.05;
+ d.width *= 1.05;
+ setSize(d);
+ } else if (cmd.equals("Kleiner")) {
+ Dimension d = getSize();
+ d.height *= 0.95;
+ d.width *= 0.95;
+ setSize(d);
+ } else if (cmd.equals("Beenden")) {
+ setVisible(false);
+ dispose();
+ System.exit(0);
+ } else if (cmd.equals("Links")) {
+ setLocation(getLocation().x-10, getLocation().y);
+ } else if (cmd.equals("Rechts")) {
+ setLocation(getLocation().x+10, getLocation().y);
+ } else if (cmd.equals("Oben")) {
+ setLocation(getLocation().x, getLocation().y-10);
+ } else if (cmd.equals("Unten")) {
+ setLocation(getLocation().x, getLocation().y+10);
+ }
+ }
+}
\ No newline at end of file |
