Академический Документы
Профессиональный Документы
Культура Документы
JFrame
JFrame is a Swing’s top-level container that renders a window on screen. A frame is a base
window on which other components rely, such as menu bar, panels, labels, text fields, buttons,
etc. Almost every Swing application starts with JFrame window.
To create a JFrame object, we can use the constructors of the JFrame class.
Constructor Description
JFrame() It constructs a new frame that is initially invisible.
It creates a Frame in the specified GraphicsConfiguration
JFrame(GraphicsConfiguration gc)
of a screen device and a blank title.
It creates a new, initially invisible Frame with the
JFrame(String title)
specified title.
JFrame(String title, It creates a JFrame with the specified title and the
GraphicsConfiguration gc) specified GraphicsConfiguration of a screen device.
Ex
JFrame frame = new JFrame("Swing"); // Create a JFrame object
In the above code we use the constructor which takes a string. The string value will be
displayed as the title for the JFrame.
Classes representing Swing components are in the javax.swing package, so is the JFrame class.
Show a JFrame
When we create a JFrame object, by default, it is not visible. We have to call its
setVisible(boolean visible) method to make it visible. true parameter makes the frame visible,
to hide the frame pass false to the setVisible method.
frame.setVisible(true);
Exit a Swing application
We can define one of the four behaviors of a JFrame to determine what happens when the
JFrame is closed.
The JFrame class implements the WindowsConstants interface. We can reference all these
constants using JFrame.CONSTANT_NAME syntax or we can use the
WindowsConstants.CONSTANT_NAME syntax.
DO_NOTHING_ON_CLOSE
do not do anything when the user closes a JFrame.
HIDE_ON_CLOSE
hides a JFrame when the user closes it. This is the default behavior. The JFrame is
invisible but the program is still running.
DISPOSE_ON_CLOSE
hides and disposes of the JFrame when the user closes it. Disposing a JFrame releases
any resources used by it.
EXIT_ON_CLOSE
exits the application. This option will exit the application.
We can set the default close behavior of a JFrame by passing one of the four constants to its
setDefaultCloseOperation() method.
The following code shows how to exit the application when the JFrame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame Size
We need to set the size and position of your JFrame to show its content area.
The size of a frame is defined by its width and height in pixels and we can set them using
setSize(int width, int height) method.
The position is defined by the (x, y) coordinates in pixels of the top-left corner of the JFrame
with respect to the top-left corner of the screen.
By default, its position is set to (0, 0) and this is the reason the JFrame was displayed at the top-
left corner of the screen.
We can set the (x, y) coordinates of the JFrame using its setLocation(int x, int y) method.
To set its size and position in one step, use its setBounds(int x, int y, int width, int height)
method
EX
import java.awt.*;
import javax.swing.*;
public class JFrameDemo
{
public static void main(String args[])
{
JFrame f1=new JFrame("example frame");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(500,500);
f1.setVisible(true);
}
}
Output:
2.JButton:
A JButton is also known as a push button. The user presses or clicks a JButton to perform an
action.
Create JButton
JButton can display text and an icon. We can use constructors listed in the following table to
create a JButton.
Constructor Description
JButton() Creates a JButton without any label or icon.
JButton(String text) Creates a JButton and sets the specified text as its label.
JButton(Icon icon) Creates a JButton with an icon and no label.
JButton(String text, Icon icon) Creates a JButton with the specified label and icon.
JButton(Action action) Creates a JButton with an Action object.
import java.awt.event.*;
import java.awt.*;
testswing()
setVisible(true);
new testswing();
}
Output:
3.JLabel:
A JLabel can display both text and images. It can even render HTML tags so that you can create
a JLabel that displays multicolors or multiline text.
Constructor Description
Creates a JLabel instance with no image and with an
JLabel()
empty string for the title.
JLabel(String s) Creates a JLabel instance with the specified text.
JLabel(Icon i) Creates a JLabel instance with the specified image.
JLabel(String s, Icon i, int Creates a JLabel instance with the specified text, image,
horizontalAlignment) and horizontal alignment.
EX
import javax.swing.*;
class LabelExample
JLabel l1,l2;
l1.setBounds(50,50, 100,30);
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
Output:
4.JTextField:
A JTextField can handle one line of plain text. Its constructors accept a combination of the
following three values.
ID Constructor/Description
JTextField()
1 Creates a JTextField with default values for initial text, number of
columns, and document.
JTextField(Document document, String text, int columns)
2 Creates a JTextField with the specified document as its model, text as its
initial text, and columns as its number of columns.
JTextField(int columns)
3
Creates a JTextField with the specified columns as its number of columns.
JTextField(String text)
4
Creates a JTextField with the specified text as its initial text.
JTextField(String text, int columns)
5 Creates a JTextField with the specified text as its initial text and columns
as its number of columns.
Ex
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public MyTextField()
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new MyTextField();
Output:
4.JComboBox:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public Test()
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new Test();
Output:
5.JTextArea
A JTextArea does not provide scrolling by itself, we need to put JTextField to a JScrollPane to
have scrolling capability.
We can set the number of rows and columns for a JTextArea to set its preferred size.
The following code shows how to create JTextArea using different initial values.
To Create a JTextArea with 10 rows and 50 columns with an initial text of "Enter resume here"
6.JPasswordField:
We can set our own echo character by using its setEchoChar(char newEchoChar) method.
The JPasswordField class has the same set of constructors as the JTextField class.
We combine the initial text, the number of columns, and a Document object to create a
JPasswordField object.
import javax.swing.*;
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
Output:
7.JScrollPane
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JtextArea;
final JFrame frame = new JFrame("Scroll Pane Example"); // Create and set up the window.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout()); // set flow layout for the frame
JTextArea textArea = new JTextArea(20, 20);
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALW
AYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollableTextArea);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
createAndShowGUI();
);
Output:
8. JPanel:
A panel in swing is similar to Panel in AWT, is a lightweight container that is designed to group a
set of components, including other panels. It is visually represented as window that does not
contain a title bar, menu bar or border. It is simplest of all the containers. Its default layout
manager is FlowLayout.
Panels are represented by objects created from JPanel class by calling the constructor.
public JPanel ()
After the panel has been created, other components can be added to JPanel object by calling its
add (component) method inherited from the Container class. The following code fragment
demonstrates creating a panel and adding two buttons to it.
import java.awt.*;
import javax.swing.*;
public class PanelExample {
PanelExample()
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new PanelExample();
}
}
Output: