JFrame
Java Swing Tutorial Explaining the
JFrame class. The components added to the frame are referred to as
its contents; these are managed by the contentPane. To add a component to a
JFrame, we must use its contentPane instead.JFrame is a Window with border,
title and buttons. When JFrame is set visible, an event dispatching thread
is started. JFrame objects store several objects including a Container object
known as the content pane. To add a component to a JFrame, add it to the
content pane.
JFrame
Features
It’s a window with title, border,
(optional) menu bar and user-specified components.
It can be moved, resized, iconified.
It is not a subclass of JComponent.
Delegates responsibility of managing user-specified components to a content pane, an instance of JPanel.
It can be moved, resized, iconified.
It is not a subclass of JComponent.
Delegates responsibility of managing user-specified components to a content pane, an instance of JPanel.
Centering
JFrame’s
By default, a Jframe is displayed in
the upper-left corner of the screen. To display a frame
at a specified location, you can use the setLocation(x, y) method in the JFrame class. This
method places the upper-left corner of a frame at location (x, y).
at a specified location, you can use the setLocation(x, y) method in the JFrame class. This
method places the upper-left corner of a frame at location (x, y).
The Swing API
keeps improving with abstractions such as the
setDefaultCloseOperation method
for the JFrame
for the JFrame
Crating
a JFrame Window
Step
1: Construct an object of the JFrame class.
Step
2: Set the size of the Jframe.
Step
3: Set the title of the Jframe to appear in the title bar (title bar will be
blank if no title is set).
Step
4: Set the default close operation. When the user clicks the close button, the
program stops running.
Step
5: Make the Jframe visible.
How to position
JFrame on Screen?
frame.setLocationRelativeTo( null );
JFrame Source Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFrameDemo {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Source
Demo");
// Add a window listner for close button
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(jlbempty,
BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
|
Output

Swing Replacements for AWT Components
Use the following table as a guide for choosing a Swing
replacement for each AWT component used in your program.
AWT Component
|
Closest Swing Equivalent
|
Notes
|
java.applet.Applet |
AWT applets and Swing applets differ in
several ways. See Converting
Applets.
|
|
Button |
A Swing button can include an image
and/or text.
|
|
Canvas |
Your choice depends on what the program
uses the canvas for. See Converting
Canvases for a discussion
of your conversion options.
|
|
Checkbox |
Note the `B' is capitalized in the Swing
class name but not in the AWT class name.
|
|
CheckboxMenuItem |
Note the `B' is capitalized in the Swing
class name but not in the AWT class name. Also, note that Swing menu
components are true components.
|
|
Choice |
Replace a
Choice with an uneditable (the default) JComboBox . You might have to re-write code that handles item events. Refer
to Converting
Choices. |
|
Dialog |
AWT-based programs add components
directly to a dialog and directly set its layout manager. In contrast,
Swing-based programs add components to and set the layout manager on a
JDialog 'scontent pane. |
|
FileDialog |
FileDialog is a dialog, while JFileChooser is a component that you can place in any top-level container. For
convenience, JFileChooser provides methods that make it easy to display a file chooser in a
dialog. |
|
Frame |
AWT-based programs add components
directly to a frame and directly set its layout manager. In contrast,
Swing-based programs add components to and set the layout manager on a
JFrame 'scontent pane. |
|
Label |
A Swing label can include an image and/or
text. To support accessibility, use
setLabelFor to associate each label with the component it describes (if any). |
|
List |
AWT lists and Swing lists differ in many
ways. See Converting
Lists for information and
examples. If you'd like your list to display multiple columns of information,
consider upgrading to a table.
If the list contains hierarchical informaiton, consider using a tree.
|
|
Menu |
Swing menu components are true
components.
|
|
MenuBar |
Swing menu components are true
components.
|
|
MenuItem |
Swing menu components are true
components.
|
|
Panel |
You can easily add borders to Swing
panels.
|
|
PopupMenu |
Swing menu components are true
components.
|
|
Scrollbar |
JScrollPane orJSlider orJProgressBar |
Although Swing has a
JScrollBar class, you don't usually use it directly. |
ScrollPane |
You can add custom decorations, including
row and column headers, to Swing scroll panes.
|
|
TextArea |
Typically requires some re-coding to
convert. See Converting
Text Components for
information and examples.
|
|
TextField |
For simple uses,
JTextField is source-compatible with TextField . If you use text listeners you need to modify your code to use a
custom document or document listener instead. If you need a password field,
use JPasswordField instead. See Converting
Text Components for
information about non-trivial conversions and examples. |
|
|
No comments:
Post a Comment