The JFrame
class provides windows for
applets and applications. GUI based applications generally uses at
least one frame.
public class FrameWindow extends JFrame { boolean inAnApplet; JTextArea output; public FrameWindow() { // Calls the Frame constructor and adds components to the window addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (inAnApplet) dispose(); else System.exit(0); } }); } . . . public static void main(String args[]) { FrameWindow window = new FrameWindow(); window.inAnApplet = false; window.setTitle("FrameWindow Application"); window.setSize(450, 200); window.setVisible(true); } }
String getTitle()
and void
setTitle(String)
Returns or sets (respectively) the title of the frame's window.
Image getIconImage()
and
void setIconImage(Image)
Returns or sets (respectively) the image displayed when the window is iconified.
JMenuBar getJMenuBar()
and void
setJMenuBar(JMenuBar)
Returns or sets (respectively) the menu bar for this frame.
The JPanel
class is a general-purpose
Container
subclass. You can use it as-is to
hold components, or you can define a subclass to perform special
functionality, such as event handling for the objects the
JPanel
contains.
![]() | Note |
---|---|
The |
JPanel p = new JPanel(); p.add(new JButton("Button 1")); p.add(new JButton("Button 2")); p.add(new JButton("Button 3"));
A JPanel
subclass that draws a frame
border around its contents.
class FramedArea extends JPanel { public FramedArea() { . . . // Set the layout manager. // Add any components to this panel. } // Ensure that no component is placed on top of the frame. // The inset values were determined by trial and error. public Insets getInsets() { return new Insets(4, 4, 5, 5); } //Draw the frame at this Panel's edges. public void paint(Graphics g) { Dimension d = getSize(); Color bg = getBackground(); g.setColor(bg); g.draw3DRect(0, 0, d.width - 1, d.height - 1, true); g.draw3DRect(3, 3, d.width - 7, d.height - 7, false); } }
Swing provides support for custom dialogs with the
JDialog
class. It also provides a useful class,
JOptionPane
, that enables the creation of a
variety of standard dialogs through its class methods.
class SimpleDialog extends JDialog implements ActionListener { JFrame parent; JTextField field; JButton setButton; SimpleDialog(JFrame parent, String title) { super(parent, title, false); ... // Create and add components, such as the set button. // Initialize this dialog to its preferred size. pack(); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == setButton) parent.setTitle(field.getText()); } } // Here's the code that brings up the dialog: SimpleDialog dialog = new SimpleDialog(this, "A Simple Dialog"); dialog.setVisible(true);
The JOptionPane
class displays standard
dialogs that prompts the user for input or shows messages. Some of
it's main methods are:
Method Name | Description |
showConfirmDialog | Asks a confirming question, showing the buttons yes,no or cancel. |
showInputDialog | Prompt the user for some input. |
showMessageDialog | Tells the user about something. |
showOptionDialog | The unification of the above three. |
The example below cretes a dialog that promps user to choose an option in a list.
Object[] options = { "Option 0", "Option 1", "Option 2", "Option 3" }; JOptionPane.showInputDialog(this, "Message", "Title", JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
Copyright © 1998-2009 Dilvan Moreira