creating a gui with jfc or swing

177
JAVA – SUN tutorial Creating a GUI with JFC/Swing  1

Upload: strikeru

Post on 08-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Creating a GUI With JFC or Swing

    1/177

    JAVA SUN tutorial

    Creating a GUI withJFC/Swing

    1

  • 8/6/2019 Creating a GUI With JFC or Swing

    2/177

    Trail: Creating a GUI with JFC/Swing

    Lesson: Using Swing Components

    How to Make DialogsSeveral classes support dialogswindows that are more limited thanframes. To create simple, standard

    dialogs, you use theJOptionPane class. TheProgressMonitorclass can put up a dialog that

    shows the progress of an operation. Two other classes,JColorChooserandJFileChooser, alsosupply standard dialogs. To bring up a print dialog, you can use either the getPrintJob method defined

    in theToolkit class or, if youre using the Java 2 platform, the Printing API. To create custom

    dialogs, use the JDialog class directly.

    The code for simple dialogs can be minimal. For example, heres an

    informational dialog:

    Here is the code that creates and shows it:

    JOptionPane.showMessageDialog(frame, Eggs arentsupposed to be green.);

    The rest of this section covers the following topics:

    An Overview of Dialogs

    The DialogDemo Example

    JOptionPane Features

    Creating and Showing Simple Dialogs

    Customizing Button Text Getting the Users Input from a Dialog

    Stopping Automatic Dialog Closing

    The Dialog API

    Examples that Use Dialogs

    An Overview of Dialogs

    Every dialog is dependent on a frame. When that frame is destroyed, so are its dependent dialogs. When

    the frame is iconified, its dependent dialogs disappear from the screen. When the frame is deiconified, its

    dependent dialogs return to the screen. The AWT automatically provides this behavior.

    A dialog can be modal. When a modal dialog is visible, it blocks user input

    to all other windows in the program. The dialogs that JOptionPane providesare modal. To create a non-modal dialog, you must use the JDialog class

    directly.

    The JDialog class is a subclass of the AWTjava.awt.Dialog class. It

    adds to Dialog a root pane and support for a default close operation. These

    are the same features that JFrame has, and using JDialog directly is very

    similar to using JFrame directly. If youre going to use JDialog directly,

    2

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/progress.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/progress.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/progress.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/colorchooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/colorchooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/colorchooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/filechooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/filechooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/filechooser.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Toolkit.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Toolkit.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Toolkit.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/2d/printing/index.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#overviewhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#dialogdemohttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#featureshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#buttonhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#inputhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#stayuphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#apihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#eghttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/rootpane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/2d/printing/index.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Toolkit.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/progress.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/colorchooser.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/filechooser.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Toolkit.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/2d/printing/index.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#overviewhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#dialogdemohttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#featureshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#buttonhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#inputhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#stayuphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#apihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#eghttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/rootpane.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    3/177

    then you should understand the material in Using Top-Level Containers and

    How to Make Frames, especially Responding to Window-Closing Events.

    Even when you use JOptionPane to implement a dialog, youre still using a

    JDialog behind the scenes. The reason is that JOptionPane is simply a

    container that can automatically create a JDialog and add itself to the

    JDialogs content pane.

    The DialogDemo Example

    Heres a picture of an application that displays dialogs.

    JOptionPane Features

    Using JOptionPane, you can create and customize several different kinds of dialogs. JOptionPane

    provides support for laying out standard dialogs, providing icons, specifying the dialogs title and text, andcustomizing the button text. Other features allow you to customize the components the dialog displays and

    specify where the dialog should appear onscreen. You can even specify that an option pane put itself into

    aninternal frame(JInternalFrame) instead of a JDialog.

    Note: The internal frames that JOptionPane creates currently behave differently from modal dialogs.

    They dont behave modally, and in general seem more like frames than like dialogs. For this reason, we

    dont currently recommend their use.

    When you create a JOptionPane, look-and-feel-specific code adds

    components to the JOptionPane and determines the layout of those

    components.

    JOptionPanes icon support lets you easily specify which icon the dialog

    displays. You can use a custom icon, no icon at all, or any one of fourstandard JOptionPane icons (question, information, warning, and error).

    Each look and feel has its own versions of the four standard icons. The

    following figure shows the icons used in the Java Look & Feel.

    Icons provided by JOptionPane

    3

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.html#windoweventshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.html#windoweventshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    4/177

    (Java Look & Feel shown)

    question information warning error

    Creating and Showing Simple Dialogs

    For most simple modal dialogs, you create and show the dialog using one ofJOptionPanesshowXxxDialog methods. If your dialog should be aninternal frame, then add Internal aftershow

    for example, showMessageDialog changes to showInternalMessageDialog. If you need to

    control the dialogs window-closing behavior or if the dialog isnt modal, then you should directly

    instantiate JOptionPane and add it to a JDialog instance. Then invoke setVisible(true) on

    the JDialog to make it appear.

    The two most useful showXxxDialog methods are showMessageDialog and

    showOptionDialog. The showMessageDialog method displays a simple,

    one-button dialog. The showOptionDialog method displays a customized

    dialogit can display a variety of buttons with customized button text, and

    can contain a standard text message or a collection of components.

    The other two showXxxDialog methods are used less often. The

    showConfirmDialog method asks the user to confirm something, but has the

    disadvantage of having standard button text (Yes/No or the localized

    equivalent, for example), rather than button text customized to the userssituation (Start/Cancel, for example). A fourth method, showInputDialog,

    is designed to display a modal dialog that gets a string from the user, using

    either a text field or an uneditable combo box. However, input dialogs arentvery useful right now, since the text field doesnt let you perform validation

    before the dialog goes away, and combo boxes in dialogs dont yet work

    well.

    Here are some examples, taken from DialogDemo.java , of using

    showMessageDialog, showOptionDialog, and the JOptionPane

    constructor. For more example code, see DialogDemo.java and the other

    programs listed in Examples that Use Dialogs.

    showMessageDialog

    Displays a modal dialog with one button, which is labeled OK (or the localized equivalent). You can

    easily specify the message, icon, and title that the dialog displays. Here are some examples of usingshowMessageDialog:

    //default title and iconJOptionPane.showMessageDialog(frame,

    Eggs arent supposed to be green.);

    //custom title, warning iconJOptionPane.showMessageDialog(frame,

    Eggs arent supposed to be green.,Inane warning,JOptionPane.WARNING_MESSAGE);

    4

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#eghttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#eg
  • 8/6/2019 Creating a GUI With JFC or Swing

    5/177

    //custom title, error iconJOptionPane.showMessageDialog(frame,

    Eggs arent supposed to be green.,Inane error,JOptionPane.ERROR_MESSAGE);

    //custom title, no iconJOptionPane.showMessageDialog(frame,

    Eggs arent supposed to be green.,A plain message,JOptionPane.PLAIN_MESSAGE);

    //custom title, custom iconJOptionPane.showMessageDialog(frame,

    Eggs arent supposed to be green.,Inane custom dialog,JOptionPane.INFORMATION_MESSAGE,icon);

    showOptionDialog

    Displays a modal dialog with the specified buttons, icons, message, title, and so on. With this method, you

    can change the text that appears on the buttons of standard dialogs. You can also perform many other kinds

    of customization.

    //Custom button textObject[] options = {Yes, please,

    No, thanks,No eggs, no ham!};

    int n = JOptionPane.showOptionDialog(frame,Would you like some green eggs to go + with that ham?,A Silly Question,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);

    JOptionPane (constructor)

    Creates a JOptionPane with the specified buttons, icons, message, title, and so on. You must then add

    the option pane to a JDialog, register a property-change listener on the option pane, and show the

    dialog. SeeStopping Automatic Dialog Closing for details.

    final JOptionPane optionPane = new JOptionPane(The only way to close this dialog is by\n+ pressing one of the following buttons.\n+ Do you understand?,JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);

    5

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#stayuphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#stayuphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#stayup
  • 8/6/2019 Creating a GUI With JFC or Swing

    6/177

    The arguments to all of the showXxxDialog methods and JOptionPane constructors are

    standardized, though the number of arguments for each method and constructor varies. The following listdescribes each argument. To see the exact list of arguments for a particular method, seeThe Dialog API.

    ComponentparentComponent

    The first argument to each showXxxDialog method is always the parent component, which must be a

    frame, a component inside a frame, or null. If you specify a frame, then the dialog will appear over thecenter of the frame, and depend on that frame. If you specify a component inside a frame, then the dialog

    will appear over the center of that component, and depend on that components frame. If you specify null,

    then the look and feel picks an appropriate position for the dialoggenerally the center of the screen, and

    the dialog doesnt depend on any visible frame.

    The JOptionPane constructors do not include this argument. Instead, you

    specify the parent frame when you create the JDialog that contains the

    JOptionPane, and you use the JDialogsetLocationRelativeTo method

    to set the dialogs position.

    Objectmessage

    This required argument specifies what the dialog should display in its main area. Generally, you specify astring, which results the dialog displaying a label with the specified text. You can split the message over

    several lines by putting newline (\n) characters inside the message string. For example:

    Complete the sentence:\n \Green eggs and...\String title

    The title of the dialog.int optionType

    Specifies the set of buttons that appear at the bottom of the dialog. Choose from one of the following

    standard sets: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION,

    OK_CANCEL_OPTION.

    int messageType

    This argument determines the icon displayed in the dialog. Choose from one of the following values:PLAIN_MESSAGE (no icon), ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,

    QUESTION_MESSAGE.

    Icon icon

    The icon to display in the dialog.Object[] options

    Further specifies the option buttons to appear at the buttom of the dialog. Generally, you specify an array

    of strings for the buttons. SeeCustomizing Button Text in a Standard Dialogfor more information.

    Object initialValue

    Specifies the default value to be selected.

    You can either let the default icon be used or specify the icon using the

    message type or icon argument. By default, a dialog created withshowMessageDialog displays the information icon, and a dialog created

    with showConfirmDialog orshowInputDialog displays the question icon.

    An option pane created with a JOptionPane constructor displays no icon, by

    default. To specify that the dialog display a standard icon or no icon, specify

    the message type. To specify a custom icon, use the icon argument. The icon

    argument takes precedence over the message type; as long as the iconargument has a non-null value, the dialog displays the specified icon.

    6

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#apihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#apihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#buttonhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#buttonhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#buttonhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#apihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#button
  • 8/6/2019 Creating a GUI With JFC or Swing

    7/177

    Customizing Button Text

    When you use JOptionPane to create a dialog, you can choose either to use the standard button text

    (which might vary by look and feel) or to specify different text.

    The following code, taken fromDialogDemo.java , creates two Yes/No

    dialogs. The first dialog is implemented with showConfirmDialog, whichuses the look-and-feel wording for the two buttons. The second dialog usesshowOptionDialog so it can customize the wording. With the exception of

    wording changes, the dialogs are identical.

    //default icon, custom titleint n = JOptionPane.showConfirmDialog(

    frame,Would you like green eggs and ham?,An Inane Question,JOptionPane.YES_NO_OPTION);

    Object[] options = {Yes, please,No way!};

    int n = JOptionPane.showOptionDialog(frame,Would you like green eggs and ham?,A Silly Question,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, //dont use a custom Iconoptions, //the titles of buttonsoptions[0]); //default button title

    Getting the Users Input from a Dialog

    As the previous code snippets showed, the showMessageDialog, showConfirmDialog, and

    showOptionDialog methods return an integer indicating the users choice. The values for this integer

    are YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, and CLOSED_OPTION. Except for

    CLOSED_OPTION, each option corresponds to the button the user pressed. When CLOSED_OPTION isreturned, it indicates that the user closed the dialog window explicitly, rather than by choosing a button

    inside the option pane.

    Even if you change the strings that the standard dialog buttons display, the

    return value is still one of the pre-defined integers. For example, aYES_NO_OPTION dialog always returns one of the following values:

    YES_OPTION, NO_OPTION, orCLOSED_OPTION.

    If youre designing a custom dialog, on the other hand, then you need to

    design your dialogs API so that you can query the dialog about what the

    user chose. For example, the dialog implemented in CustomDialog.java

    has a getValidatedText method that returns the text the user entered.

    Stopping Automatic Dialog Closing

    By default, when the user clicks a JOptionPane-created button, the dialog closes. But what if you want

    to check the users answer before closing the dialog? In this case, you must implement your own property

    change listener so that when the user clicks a button, the dialog doesnt automatically close.

    7

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/DialogDemo.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.java
  • 8/6/2019 Creating a GUI With JFC or Swing

    8/177

    DialogDemo contains two dialogs that implement a property change listener.

    One of these dialogs is a custom modal dialog, implemented inCustomDialog , that uses JOptionPane both to get the standard icon and to

    get layout assistance. The other dialog, whose code is below, uses a standard

    Yes/No JOptionPane, Though this dialog is rather useless as written, its

    code is simple enough that you can use it as a template for more complexdialogs.

    Besides setting the property change listener, the following code also calls theJDialogs setDefaultCloseOperation method and implements a window

    listener that handles the window close attempt properly. If you dont care to

    be notified when the user closes the window explicitly, then ignore the boldcode.

    final JOptionPane optionPane = new JOptionPane(The only way to close this dialog is by\n+ pressing one of the following buttons.\n+ Do you understand?,JOptionPane.QUESTION_MESSAGE,

    JOptionPane.YES_NO_OPTION);final JDialog dialog = new JDialog(frame,Click a button,true);dialog.setContentPane(optionPane);dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);dialog.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent we) {setLabel(Thwarted user attempt to close window.);

    }});optionPane.addPropertyChangeListener(new PropertyChangeListener() {public void propertyChange(PropertyChangeEvent e) {String prop = e.getPropertyName();if (dialog.isVisible()

    && (e.getSource() == optionPane)&& (prop.equals(JOptionPane.VALUE_PROPERTY) ||prop.equals(JOptionPane.INPUT_VALUE_PROPERTY)))

    {//If you were going to check something//before closing the window, youd do//it here.dialog.setVisible(false);

    }}

    });dialog.pack();dialog.setVisible(true);

    int value = ((Integer)optionPane.getValue()).intValue();if (value == JOptionPane.YES_OPTION) {setLabel(Good.);

    } else if (value == JOptionPane.NO_OPTION) {setLabel(Try using the window decorations to close the non-auto-closing dialog. You cant!);}

    The Dialog API

    The following tables list the commonly used JOptionPane and JDialog constructors and methods.

    Other methods youre likely to call are defined by theDialog ,Window andComponent classes

    and include pack, setSize, and setVisible.

    8

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-swing/CustomDialog.javahttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Dialog.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    9/177

    The API is listed as follows:

    Showing Standard Modal Dialogs (using JOptionPane Class Methods

    Methods for Using JOptionPanes Directly

    Frequently Used JDialog Constructors and Methods

    Showing Standard Modal Dialogs (Using JOptionPane Class Methods)

    Method Purposestatic voidshowMessageDialog(Component,Object)static voidshowMessageDialog(Component,Object, String, int)static voidshowMessageDialog(Component,Object, String, int, Icon)

    Show a one-button, modal dialog that gives the

    user some information. The arguments specify

    (in order) the parent component, message, title,

    message type, and icon for the dialog. See

    Creating and Showing Simple Dialogsfor a

    discussion of the arguments and their effects.

    static intshowOptionDialog(Component, Object,

    String, int, int, Icon, Object[],Object)

    Show a customized modal dialog. The arguments

    specify (in order) the parent component,

    message, title, option type, message type, icon,

    options, and initial value for the dialog. SeeCreating and Showing Simple Dialogsfor a

    discussion of the arguments and their effects.

    static intshowConfirmDialog(Component,Object)static intshowConfirmDialog(Component,Object, String, int)static intshowConfirmDialog(Component,Object, String, int, int)static int

    showConfirmDialog(Component,Object, String, int, int, Icon)

    Show a modal dialog that asks the user a

    question. The arguments specify (in order) the

    parent component, message, title, option type,message type, and icon for the dialog. See

    Creating and Showing Simple Dialogsfor a

    discussion of the arguments and their effects.

    static StringshowInputDialog(Object)static StringshowInputDialog(Component, Object)static StringshowInputDialog(Component, Object,String, int)static StringshowInputDialog(Component, Object,String, int, Icon, Object[],Object)

    Show a modal dialog that prompts the user for

    input. The single-argument version specifies just

    the message, with the parent component assumed

    to be null. The arguments for the other versions

    specify (in order) the parent component,

    message, title, message type, icon, options, and

    initial value for the dialog. SeeCreating and

    Showing Simple Dialogsfor a discussion of the

    arguments and their effects.

    static voidshowInternalMessageDialog(...)static voidshowInternalOptionDialog(...)static voidshowInternalConfirmDialog(...)static StringshowInternalInputDialog(...)

    Implement a standard dialog as an internal frame.

    See theJOptionPaneAPI documentation

    for the exact list of arguments.

    Methods for Using JOptionPanes Directly

    9

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#showapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#showapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#showapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#joptionpaneapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#joptionpaneapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#joptionpaneapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#jdialogapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#jdialogapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#jdialogapihttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#showapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#joptionpaneapihttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#jdialogapihttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showConfirmDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalMessageDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalConfirmDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#showInternalInputDialog(java.awt.Component,%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    10/177

    Method or Constructor Purpose

    JOptionPane()JOptionPane(Object)JOptionPane(Object, int)JOptionPane(Object, int, int)JOptionPane(Object, int, int, Icon)

    JOptionPane(Object, int, int, Icon,Object[])JOptionPane(Object, int, int, Icon,Object[], Object)

    Creates a JOptionPane instance. See

    Creating and Showing Simple Dialogs

    for a discussion of the arguments andtheir effects.

    static FramegetFrameForComponent(Component)static JDesktopPanegetDesktopPaneForComponent(Component)

    Handy JOptionPane class methods

    that find the frameordesktop pane,

    respectively, that the specified

    component is in.

    Frequently Used JDialog Constructors and Methods

    Method or Constructor Purpose

    JDialog()JDialog(Dialog)

    JDialog(Dialog, boolean)JDialog(Dialog, String)JDialog(Dialog, String, boolean)JDialog(Dialog, String, boolean,GraphicsConfiguration)JDialog(Frame)JDialog(Frame, boolean)JDialog(Frame, String)JDialog(Frame, String, boolean)JDialog(Frame, String, boolean,GraphicsConfiguration)

    Creates aJDialog

    instance. TheFrameargument, if any, is the frame (usually a

    JFrame object) that the dialog depends on.

    Make the boolean argument true to specify

    a modal dialog, false or absent to specify a

    non-modal dialog. You can also specify the

    title of the dialog, using a string argument.

    The constructors taking thejava.awt.GraphicsConfiguration

    argument were introduced in 1.4.

    void setContentPane(Container)

    Container getContentPane()

    Get and set the content pane, which is usually

    the container of all the dialogs components.

    SeeUsing Top-Level Containers for moreinformation.

    void setDefaultCloseOperation(int)int getDefaultCloseOperation()

    Get and set what happens when the user tries

    to close the dialog. Possible values:DISPOSE_ON_CLOSE,

    DO_NOTHING_ON_CLOSE,

    HIDE_ON_CLOSE (the default). See

    Responding to Window-Closing Events for

    more information.

    void setLocationRelativeTo(Component) Centers the dialog over the specifiedcomponent.

    static void

    setDefaultLookAndFeelDecorated(boolean) static booleanisDefaultLookAndFeelDecorated()

    Set or get a hint as to whether the dialogs

    window decorations (such as borders, orwidgets to close the window) should be

    provided by the current look and feel.

    Otherwise the dialogs decorations will beprovided by the current window manager.

    Note that this is only a hint, as some look and

    feels do not support this feature. Introduced in

    1.4.

    [PENDING: We need a discussion on this

    10

    http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[])http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[])http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[])http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getDesktopPaneForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getDesktopPaneForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getDesktopPaneForComponent(java.awt.Component)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setContentPane(java.awt.Container)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setContentPane(java.awt.Container)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getContentPane()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getContentPane()http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getDefaultCloseOperation()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getDefaultCloseOperation()http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.html#windoweventshttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setLocationRelativeTo(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setLocationRelativeTo(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultLookAndFeelDecorated(boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultLookAndFeelDecorated(boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultLookAndFeelDecorated(boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#isDefaultLookAndFeelDecorated()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#isDefaultLookAndFeelDecorated()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#isDefaultLookAndFeelDecorated()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[])http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[])http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#JOptionPane(java.lang.Object,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#createhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getFrameForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getDesktopPaneForComponent(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JOptionPane.html#getDesktopPaneForComponent(java.awt.Component)http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Dialog,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#JDialog(java.awt.Frame,%20java.lang.String,%20boolean,%20java.awt.GraphicsConfiguration)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setContentPane(java.awt.Container)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getContentPane()http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#getDefaultCloseOperation()http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/frame.html#windoweventshttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setLocationRelativeTo(java.awt.Component)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultLookAndFeelDecorated(boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#setDefaultLookAndFeelDecorated(boolean)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#isDefaultLookAndFeelDecorated()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JDialog.html#isDefaultLookAndFeelDecorated()
  • 8/6/2019 Creating a GUI With JFC or Swing

    11/177

    subject.]

    11

  • 8/6/2019 Creating a GUI With JFC or Swing

    12/177

    Trail: Creating a GUI with JFC/SwingLesson: Using Swing Components

    How to Make Frames (Main Windows)A frame, implemented as an instance of the JFrame class, is a window that typically has decorations

    such as a border, a title, and buttons for closing and iconifying the window. Applications with a GUItypically use at least one frame. Applets sometimes use frames, as well.

    To make a window thats dependent on another window disappearingwhen the other window is iconified, for example use a dialog instead of a

    frame. To make a window that appears within another window, use an

    internal frame.

    Creating and Showing Frames

    Heres a picture of the extremely plain window created by an example called

    FrameDemo. You can find the source code in FrameDemo.java . You can

    run FrameDemo using Java Web Start .

    The following code, taken from FrameDemo, is a typical example of the code used to create and set up a

    frame.//1. Optional: Specify who draws the windowdecorations.JFrame.setDefaultLookAndFeelDecorated(true);//2. Create the frame.JFrame frame = new JFrame(FrameDemo);//3. Optional: What happens when the framecloses?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//4. Create components and put them in the frame.//...create emptyLabel...

    frame.getContentPane().add(emptyLabel,

    BorderLayout.CENTER);//5. Size the frame.frame.pack();

    //6. Show it.frame.setVisible(true);

    Here are some details about the code:

    12

    http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/dialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo.javahttp://java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/FrameDemo.jnlphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo.javahttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/dialog.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/internalframe.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo.javahttp://java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/FrameDemo.jnlphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    13/177

    1. Calling setDefaultLookAndFeelDecorated(true) requests that any subsequently created

    frames have window decorations provided by the look and feel, and not by the window system. Fordetails, seeSpecifying Window Decorations.

    2. The next line of code creates a frame using a constructor that lets you set the frames title. The other

    frequently used JFrame constructor is the no-argument constructor.

    3. Next the code specifies what should happen when the user closes the frame. The EXIT_ON_CLOSE

    operation, not surprisingly, makes the program exit when the user closes the frame. This behavior isappropriate for this program because the program has only one frame, and closing the frame makes

    the program useless. SeeResponding to Window-Closing Events for more information.

    4. The next bit of code adds a blank label to the frames content pane. If youre not already familiar

    with content panes and how to add components to them, please read Adding Components to the

    Content Pane.

    For frames that have menus, youd typically add the menu bar to the

    frame here using the setJMenuBar method. See How to Use Menus

    for details.

    5. The pack method sizes the frame so that all its contents are at or above their preferred sizes. An

    alternative to pack is to establish a frames size explicitly by calling setSize orsetBounds

    (which also sets the frames location). In general, using pack is preferable to calling setSize,

    since pack leaves the frames layout manager in charge of the frames size, and layout managers aregood at adjusting to platform dependencies and other factors that affect component size.

    This example doesnt set the frames location, but its easy to do sousing either the setLocationRelativeTo orsetLocation method.

    For example, the following code centers a frame onscreen:

    frame.setLocationRelativeTo(null);

    6. Calling setVisible(true) makes the frame appear onscreen. Sometimes you might see the

    show method used instead. The two usages are equivalent, but we use setVisible(true) for

    consistencys sake.

    Note: The pack and setVisible methods realize the frame and the components it contains. Once a

    component has been realized, any code that modifies or inspects it should be executed on the event-dispatching thread. For more information, refer to Threads and Swing .

    Specifying Window Decorations

    By default, window decorations are supplied by the native window system. However, you can request that

    the look and feel provide the decorations for a frame. You can even specify that the frame have no window

    decorations at all (using the setUndecorated method), a feature that is typically used withfull-screen

    exclusive mode . If you want a smaller window without decorations, then you probably want to use the

    JWindow orWindow class.

    The following snapshots show two frames that are identical except for their

    window decorations. As you can tell by the appearance of the Close windowbutton in each frame, they both use the Java look and feel. However, only

    the first frame uses window decorations provided by the Java look and feel.

    The second uses decorations provided by the window system, whichhappens to be Windows but could as easily be any other platform running

    the 1.4 version of the Java platform.

    13

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#setDefaultLookAndFeelDecoratedhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#setDefaultLookAndFeelDecoratedhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#windoweventshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#windoweventshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.html#contentpanehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.html#contentpanehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/menu.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/overview/threads.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/overview/threads.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JWindow.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JWindow.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JWindow.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/overview/threads.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#setDefaultLookAndFeelDecoratedhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#windoweventshttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.html#contentpanehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.html#contentpanehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/menu.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/overview/threads.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/extra/fullscreen/index.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JWindow.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/Window.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    14/177

    A frame with look-and-feel-provideddecorations

    A frame with window-system-provided decorations

    The difference in window decorations is caused by calls to the JFrame static

    method setLookAndFeelDecorated, which occur before each frame is

    created. To use decorations provided by the look and feel, the argument tosetLookAndFeelDecorated must be true. To switch back to window system

    (platform-specific) decorations, the argument must be false. Some look andfeels might not support this feature; in this case, the window system

    decorations are used.

    Version Note: Before 1.4, the decorations on a frame were always provided by the window system and

    could not be changed.

    The source code that creates these frames is inFrameDemo2.java . You can

    run FrameDemo2 using Java Web Start . Besides showing how to choosewindow decorations, FrameDemo2 also shows how to disable all window

    decorations and gives an example of positioning windows.

    Responding to Window-Closing Events

    By default, when the user closes a frame onscreen, the frame is hidden. Although invisible, the frame stillexists and the program can make it visible again. If you want different behavior, then you need to either

    register a window listener that handles window-closing events, or you need to specify default close

    behavior using the setDefaultCloseOperation method. You can even do both.

    The argument to setDefaultCloseOperation must be one of the following

    values, the first three of which are defined in the WindowConstants

    interface (implemented by JFrame, JInternalPane, and JDialog):

    DO_NOTHING_ON_CLOSE

    Dont do anything when the user requests that the window close. Instead, the program should probably use

    a window listener that performs some other action in its windowClosing method.

    HIDE_ON_CLOSE (the default forJDialog and JFrame)

    Hide the window when the user closes it. This removes the window from the screen but leaves itdisplayable.

    DISPOSE_ON_CLOSE (the default forJInternalFrame)

    Hide and dispose of the window when the user closes it. This removes the window from the screen and

    frees up any resources used by it.

    EXIT_ON_CLOSE (defined in theJFrame class)

    Exit the application, using System.exit(0). This is recommended for applications only. If used within

    an applet, a SecurityException may be thrown. Introduced in 1.3.

    14

    http://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo2.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo2.javahttp://java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/FrameDemo2.jnlphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/WindowConstants.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/WindowConstants.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo2.javahttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/example-1dot4/FrameDemo2.javahttp://java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/FrameDemo2.jnlphttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/information/javawebstart.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/WindowConstants.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html
  • 8/6/2019 Creating a GUI With JFC or Swing

    15/177

    Version Note: As of 1.4, DISPOSE_ON_CLOSE can have results similar to EXIT_ON_CLOSE if only

    one window is onscreen. More precisely, when the last displayable window within the Java virtual

    machine (VM) is disposed of, the VM may terminate if it is 1.4 or a compatible version. In earlier versions

    such as 1.3 the VM remains running even when all windows have been disposed of. Seebug 4030718

    for details.

    [PENDING: When definitive documentation of this issue isreleased, we will point to it instead of the bug.]

    The default close operation is executed after the any window listeners handle the window-closing event.

    So, for example, assume that you specify that the default close operation is to dispose of a frame. You also

    implement a window listener that tests whether the frame is the last one visible and, if so, saves some data

    and exits the application. Under these conditions, when the user closes a frame, the window listener will

    be called first. If it doesnt exit the application, then the default close operation disposing of the frame

    will then be performed.

    For more information about handling window-closing events, see How toWrite a Window Listener . Besides handling window-closing events,

    window listeners can also react to other window state changes, such asiconification and activation.

    The Frame API

    The following tables list the commonly used JFrame constructors and methods. Other methods you

    might want to call are defined by thejava.awt.Frame , java.awt.Window , and

    java.awt.Component classes, from which JFrame descends.

    Because each JFrame object has a root pane, frames have support for

    interposing input and painting behavior in front of the frames children,

    placing children on different layers, and for Swing menu bars. These

    topics are introduced in Using Top-Level Containersand explained in detailinHow to Use Root Panes.

    The API for using frames falls into these categories:

    Creating and Setting Up a Frame

    Setting the Window Size and Location

    Methods Related to the Root Pane

    Creating and Setting Up a Frame

    Method or Constructor Purpose

    JFrame()JFrame(String)

    Create a frame that is initially invisible.

    The String argument provides a title for

    the frame. You can also use setTitle to

    set a frames title. To make the frame

    visible, invoke setVisible(true) on

    it.

    void setDefaultCloseOperation(int)int getDefaultCloseOperation()

    Set or get the operation that occurs whenthe user pushes the close button on this

    frame. Possible choices are:

    DO_NOTHING_ON_CLOSE

    15

    http://developer.java.sun.com/developer/bugParade/bugs/4030718.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/events/windowlistener.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/events/windowlistener.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Frame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Frame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/rootpane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/rootpane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#creatinghttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#sizeplacehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#rootpanehttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#JFrame()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#JFrame()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#JFrame(java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#getDefaultCloseOperation()http://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Frame.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/events/windowlistener.htmlhttp://developer.java.sun.com/developer/bugParade/bugs/4030718.htmlhttp://developer.java.sun.com/developer/bugParade/bugs/4030718.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/events/windowlistener.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/events/windowlistener.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Frame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Window.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/java/awt/Component.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/toplevel.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/rootpane.htmlhttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#creatinghttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#sizeplacehttp://c/Documents%20and%20Settings/viniciuspereira/Vinicius/livros/Java/SUN/tut-swing/uiswing/components/#rootpanehttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#JFrame()http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#JFrame(java.lang.String)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.html#getDefaultCloseOperation()
  • 8/6/2019 Creating a GUI With JFC or Swing

    16/177

    HIDE_ON_CLOSE

    DISPOSE_ON_CLOSE

    EXIT_ON_CLOSE

    The first three constants are defined in theWindowConstants interface, which

    JFrame implements. The

    EXIT_ON_CLOSE constant is defined in

    theJFrame class, and was introduced

    in 1.3.

    void setUndecorated(boolean)boolean isUndecorated()

    (in Frame)

    Set or get whether the window systemshould provide decorations for this frame.

    Works only if the frame is not yet

    displayable (hasnt been packed or

    shown). Typically used withfull-screen

    exclusive mode .

    static voidsetDefaultLookAndFeelDecorated(boolean) static booleanisDefaultLookAndFeelDecorated()

    Determine whether subsequently createdJFrames should have their Window

    decorations (such as borders, widgets forclosing the window, title) provided by the

    current look and feel. Note that this is only

    a hint, as some look and feels may not

    support this feature. Introduced in 1.4.

    Setting the Window Size and Location

    Method Purpose

    void pack()

    (in Window)Size the window so that all its contents are at or

    above their preferred sizes.

    void setSize(int, int)

    void setSize(Dimension)Dimension getSize()

    (in Component)

    Set or get the total size of the window. The integer

    arguments to setSize specify the width andheight, respectively.

    void setBounds(int, int, int,int)void setBounds(Rectangle)Rectangle getBounds()

    (in Component)

    Set or get the size and position of the window. For

    the integer version ofsetBounds, the windows

    upper left corner is at thex, y location specified by

    the first two arguments, and has the width and

    height specified by the last two arguments.

    void setLocation(int, int)Point getLocation()

    (in Component)

    Set or get the location of the upper left corner of the

    window. The parameters are thex andy values,

    respectively.

    voidsetLocationRelativeTo(Component)

    (in Window)

    Position the window so that its centered over the

    specified component. If the argument is null, thewindow is centered onscreen. To properly center the

    window, you should invoke this method after thewindows size has been set.

    Methods Related to the Root Pane

    Method Purpose

    void setContentPane(Container)Set or get the frames content pane. The content panecontains the frames visible GUI components and should

    16

    http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/WindowConstants.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFrame.htmlhttp://java.sun.com/j2se/1.4.1/d