A Simple Modal Dialog : JDialog « Swing « Java Tutorial






A Simple Modal Dialog
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AboutDialog extends JDialog implements ActionListener {
  public AboutDialog(JFrame parent, String title, String message) {
    super(parent, title, true);
    if (parent != null) {
      Dimension parentSize = parent.getSize(); 
      Point p = parent.getLocation(); 
      setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
    }
    JPanel messagePane = new JPanel();
    messagePane.add(new JLabel(message));
    getContentPane().add(messagePane);
    JPanel buttonPane = new JPanel();
    JButton button = new JButton("OK"); 
    buttonPane.add(button); 
    button.addActionListener(this);
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack(); 
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    setVisible(false); 
    dispose(); 
  }
  public static void main(String[] a) {
    AboutDialog dlg = new AboutDialog(new JFrame(), "title", "message");
  }
}








14.74.JDialog
14.74.1.Dialog Introduction
14.74.2.A Simple Modal DialogA Simple Modal Dialog
14.74.3.JDialog is specify that pressing the Escape key cancels the dialog.JDialog is specify that pressing the Escape key cancels the dialog.
14.74.4.Address DialogAddress Dialog
14.74.5.Set Default Close Operation for Dialog
14.74.6.Extending JDialog
14.74.7.extends JDialog to create your own dialog
14.74.8.The base class for standard dialogs.
14.74.9.A dialog that presents the user with a sequence of steps for completing a task.
14.74.10.Positions the specified dialog at a position relative to its parent.
14.74.11.A dialog allow selection and a font and its associated info.
14.74.12.Font Chooser extends javax.swing.JDialog
14.74.13.FontChooser by Noah w
14.74.14.Dialog Panel