|
Auch die Einbindung dieser neuen Komponente ist sehr einfach und kann in Anlehnung an die vorherigen Beispiele durch Aufruf von add in einem Container erfolgen:
001 /* Listing2302.java */
002
003 import java.awt.*;
004 import java.awt.event.*;
005
006 class MyDialog2302
007 extends Dialog
008 implements ActionListener
009 {
010 public MyDialog2302(Frame parent)
011 {
012 super(parent,"MyDialog2302",true);
013 setBounds(100,100,400,300);
014 setBackground(Color.lightGray);
015 setLayout(new BorderLayout());
016 Panel panel = new Panel();
017 customizeLayout(panel);
018 add("Center",panel);
019 //Ende-Button
020 Button button = new Button("Ende");
021 button.addActionListener(this);
022 add("South", button);
023 pack();
024 //Window-Ereignisse
025 addWindowListener(
026 new WindowAdapter() {
027 public void windowClosing(WindowEvent event)
028 {
029 endDialog();
030 }
031 }
032 );
033 }
034
035 private void customizeLayout(Panel panel)
036 {
037 panel.setLayout(new FlowLayout());
038 panel.add(new Segment7(0));
039 panel.add(new Segment7(1));
040 panel.add(new Segment7(2));
041 panel.add(new Segment7(3));
042 panel.add(new Segment7(4));
043 panel.add(new Segment7(5));
044 panel.add(new Segment7(6));
045 panel.add(new Segment7(7));
046 panel.add(new Segment7(8));
047 panel.add(new Segment7(9));
048 }
049
050 public void actionPerformed(ActionEvent event)
051 {
052 String cmd = event.getActionCommand();
053 if (cmd.equals("Ende")) {
054 endDialog();
055 }
056 }
057
058 void endDialog()
059 {
060 setVisible(false);
061 dispose();
062 ((Window)getParent()).toFront();
063 getParent().requestFocus();
064 }
065 }
066
067 public class Listing2302
068 extends Frame
069 implements ActionListener
070 {
071 public static void main(String[] args)
072 {
073 Listing2302 wnd = new Listing2302();
074 wnd.setSize(300,200);
075 wnd.setVisible(true);
076 }
077
078 public Listing2302()
079 {
080 super("Listing2302");
081 setBackground(Color.lightGray);
082 setLayout(new FlowLayout());
083 //Dialog-Button
084 Button button = new Button("Dialog");
085 button.addActionListener(this);
086 add(button);
087 //Ende-Button
088 button = new Button("Ende");
089 button.addActionListener(this);
090 add(button);
091 //Window-Ereignisse
092 addWindowListener(
093 new WindowAdapter() {
094 public void windowClosing(WindowEvent event)
095 {
096 setVisible(false);
097 dispose();
098 System.exit(0);
099 }
100 }
101 );
102 }
103
104 public void actionPerformed(ActionEvent event)
105 {
106 String cmd = event.getActionCommand();
107 if (cmd.equals("Dialog")) {
108 MyDialog2302 dlg = new MyDialog2302(this);
109 dlg.setVisible(true);
110 } else if (cmd.equals("Ende")) {
111 setVisible(false);
112 dispose();
113 System.exit(0);
114 }
115 }
116 }
|
Listing2302.java |
Das Ergebnis kann sich sehen lassen:
Abbildung 23.2: Ein Beispiel für die Anwendung der 7-Segment-Anzeige
Wir wollen nun die Entwicklung von Dialogen abschließen und uns in Kapitel 24 der Einbindung von Bildern und der Entwicklung von Animationen zuwenden.
|
| Go To Java 2, Addison Wesley, Version 1.0.2, © 1999 Guido Krüger, http://www.gkrueger.com |