Java Reference
In-Depth Information
public class ModalDemo extends Frame implements ActionListener
{
private Dialog modal;
private JButton go, ok;
public ModalDemo(String title)
{
super(title);
//Prepare the buttons.
go = new JButton(“Go”);
go.addActionListener(this);
ok = new JButton(“OK”);
ok.addActionListener(this);
//prepare the Dialog window
modal = new Dialog(this, “A modal dialog”, true);
modal.setLayout(new FlowLayout());
modal.add(ok);
modal.setBounds(60,100,180,60);
//Add the “Go” button to the Frame.
JPanel center = new JPanel();
center.add(go);
this.add(center, BorderLayout.CENTER);
}
//Clicking either button causes this method to be invoked
public void actionPerformed(ActionEvent e)
{
String label = e.getActionCommand();
if(label.equals(“Go”))
{
modal.show();
}
else if(label.equals(“OK”))
{
modal.hide();
}
}
public static void main(String [] args)
{
Frame f = new ModalDemo(“Modal Demo”);
f.setSize(300, 300);
f.setVisible(true);
}
}
Figure 12.11 shows the output of the ModalDemo program. Notice that true was passed
in to the Dialog constructor, making the Dialog modal. The ActionListener interface and
actionPerformed() method are discussed in detail in Chapter 13.
continued
Search WWH ::




Custom Search