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:
import java.awt.*;
import java.awt.event.*;
class MyDialog
extends Dialog
implements ActionListener
{
public MyDialog(Frame parent)
{
super(parent,"MyDialog",true);
setBounds(100,100,400,300);
setBackground(Color.lightGray);
setLayout(new BorderLayout());
Panel panel = new Panel();
customizeLayout(panel);
add("Center",panel);
//Ende-Button
Button button = new Button("Ende");
button.addActionListener(this);
add("South", button);
pack();
//Window-Ereignisse
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
endDialog();
}
}
);
}
private void customizeLayout(Panel panel)
{
panel.setLayout(new FlowLayout());
panel.add(new Segment7(0));
panel.add(new Segment7(1));
panel.add(new Segment7(2));
panel.add(new Segment7(3));
panel.add(new Segment7(4));
panel.add(new Segment7(5));
panel.add(new Segment7(6));
panel.add(new Segment7(7));
panel.add(new Segment7(8));
panel.add(new Segment7(9));
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if (cmd.equals("Ende")) {
endDialog();
}
}
void endDialog()
{
setVisible(false);
dispose();
((Window)getParent()).toFront();
getParent().requestFocus();
}
}
public class Example2301
extends Frame
implements ActionListener
{
public static void main(String[] args)
{
Example2301 wnd = new Example2301();
wnd.setSize(300,200);
wnd.setVisible(true);
}
public Example2301()
{
super("Example2301");
setBackground(Color.lightGray);
setLayout(new FlowLayout());
//Dialog-Button
Button button = new Button("Dialog");
button.addActionListener(this);
add(button);
//Ende-Button
button = new Button("Ende");
button.addActionListener(this);
add(button);
//Window-Ereignisse
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
setVisible(false);
dispose();
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if (cmd.equals("Dialog")) {
MyDialog dlg = new MyDialog(this);
dlg.setVisible(true);
} else if (cmd.equals("Ende")) {
setVisible(false);
dispose();
System.exit(0);
}
}
}
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 im nächsten Kapitel der Einbindung von Bildern und der Entwicklung von Animationen zuwenden.