0% found this document useful (0 votes)
3 views

Java Swing

The document provides an overview of Java Swing, focusing on UI components such as JFrame, JLabel, JTextField, and JButton. It details the constructors and methods for these components, including how to set properties like size, visibility, and layout. Additionally, it includes example code demonstrating the creation of a JFrame with various components and a dialog box with multiple elements.

Uploaded by

smaugqwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Swing

The document provides an overview of Java Swing, focusing on UI components such as JFrame, JLabel, JTextField, and JButton. It details the constructors and methods for these components, including how to set properties like size, visibility, and layout. Additionally, it includes example code demonstrating the creation of a JFrame with various components and a dialog box with multiple elements.

Uploaded by

smaugqwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Java Swing

JAVA PROGRAMMING
Swing Components
• Computer programs usually are more user friendly (and more fun to
use) when they contain user interface (UI) components.
• UI components are buttons, text fields, and other components with
which the user can interact.
• Swing components are UI elements such as dialog boxes and
buttons; you can usually recognize their names because they
begin with J.
• To use the Swing UI components and methods, insert the following
statement at the beginning of your program:

import javax.swing.*
JFrame Class
• A container is a type of component that holds other components so
you can treat a group of them as a single entity.

• A container takes the form of a window that you can drag, resize,
minimize, restore, and close.

• The Swing component JFrame, both allow you to create more useful
objects.
JFrame Class constructors
• JFrame() constructs a new frame that initially is invisible and has no title.

• JFrame(String title) creates a new, initially invisible JFrame with the


specified title.

• JFrame(GraphicsConfiguration gc) creates a JFrame in the


specified GraphicsConfiguration of a screen device with a blank
title.
• JFrame(String title, GraphicsConfiguration gc) creates a JFrame
with the specified title and the specified GraphicsConfiguration of
a screen.
JFrame Class methods
setTitle(String) Sets a JFrame’s title using the String argument

setSize(int, int) Sets a JFrame’s size in pixels with the width


and height as arguments

getTitle() Returns a JFrame’s title

setResizable(boolean) Sets the JFrame to be resizable by passing true


to the method, or sets the JFrame not to be
resizable by passing false to the method
JFrame Class methods
isResizable() Returns true or false to indicate whether the
JFrame is resizable
setVisible(boolean) Sets a JFrame to be visible using the boolean
argument true and invisible using the boolean
argument false

setBounds(int, int, int, int) Overrides the default behavior for the JFrame
to be positioned in the upper-left corner of the
computer screen’s desktop; the first two
arguments are the horizontal and vertical
positions of the JFrame’s upper-left corner on
the desktop, and the final two arguments set
the width and height
JFrame usage:

JFrame myFrame = new JFrame();


myFrame.setBounds(480,120,320,200);
myFrame.setResizeable(true);
myFrame.setVisible(true);
Layout Manager
• A class that controls component positioning.

• To place multiple components at specified positions in a container


so they do not hide each other, you must explicitly use a layout
manager
• The flow layout manager places components in a row, and
when a row is filled, components automatically spill into the
next row.

• Three constants are defined in the FlowLayout class that specify how
components are positioned in each row of their container. These
constants are FlowLayout.LEFT, FlowLayout.RIGHT, and
FlowLayout.CENTER.
FlowLayout usage:

To create a FlowLayout object.

FlowLayout flow = new FlowLayout();


myFrame.SetLayout(flow);

or

myFrame.SetLayout(new FlowLayout();
Example:
import javax.swing.*;
import java.awt.*;
public class JFrame6 {
public static void main(String[] args) {
final int FRAME_WIDTH = 250;
final int FRAME_HEIGHT = 100;

JFrame aFrame = new JFrame("Sixth frame");


aFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel greeting = new JLabel("Hello");
JLabel greeting2 = new JLabel("Who are you?");
aFrame.setLayout(new FlowLayout());
aFrame.add(greeting);
aFrame.add(greeting2);
}
}
JLabel Class
• Is a built-in Java Swing class that holds text you can display.

The JLabel class inheritance hierarchy


JLabel Constructors
• JLabel() creates a JLabel instance with no image and with an empty
string for the title.
• JLabel(Icon image) creates a JLabel instance with the specified image.

• JLabel(Icon image, int horizontalAlignment) creates a JLabel instance


with the specified image and horizontal alignment.
• JLabel(String text) creates a JLabel instance with the specified text.

• JLabel(String text, Icon icon, int horizontalAlignment) creates a JLabel


instance with the specified text, image, and horizontal alignment.
• JLabel(String text, int horizontalAlignment) creates a JLabel instance
with the specified text and horizontal alignment.
JLabel usage:

JFrame myFrame = new JFrame();


myFrame.setBounds(480,120,320,200);
myFrame.setResizeable(true);
myFrame.setVisible(true);

JLabel label = new JLabel("Hello World");


myFrame.add(label);
Font Class
To change the font of a Jlabel, use a Font class from which you
create an object that holds typeface information.

To give a JLabel object a new font, you can create a Font object, as
in the following:

Font myFont = new Font("Monospaced", Font.BOLD, 36);

You can use the setFont() method to assign the Font to a JLabel with a
statement such as:

myFrame.setFont(myFont);
JTextField Class
• A JTextField is a component into which a user can type a single line
of text data.

• Typically, a user types a line into a JTextField and then presses Enter
on the keyboard or clicks a button with the mouse to enter the
data
JTextField Constructors

• JTextField() constructs a new JTextField.

• JTextField(int columns) constructs a new, empty JTextField with


a specified number of columns.
• JTextField(String text) constructs a new JTextField initialized with the
specified text.

• JTextField(String text, int columns) constructs a new JTextField


initialized with the specified text and columns.
JTextField Usage:

To provide a JTextField that allows enough room for a user to enter


approximately 10 characters, you can code the following:

JTextField response = new JTextField(10);

To add the JTextField named response to a JFrame named myFrame,


you write:

myFrame.add(response);
Some Methods of JTextField

setText() - allows you to change the text in a JTextField (or other


Component) that has already been created

response.setText("Thank you");

getText() - allows you to retrieve the String of text in a JTextField.

String whatUserTyped = response.getText();

setEditable() - change the JTextField’s editable status.

if(attempts > LIMIT)


response.setEditable(false);
JButton Class
• A JButton is a Component the user can click with a mouse to make
a selection.
• A JButton is even easier to create than a JTextField.
JButton Constructors
JButton() creates a button with no set text.

JButton(Icon icon) creates a button with an icon of type Icon or


ImageIcon.

JButton(String text) creates a button with text.

JButton(String text, Icon icon) creates a button with initial text and an icon
of type Icon or ImageIcon.

JButton(Action a) creates a button in which properties are taken from


the Action supplied.
Making a dialog box with multiple components
import javax.swing.*;
import java.awt.*;
public class DialogBox extends JFrame {
final int w = 300;
final int h = 200;
public DialogBox() {
super("Demonstrating many components");
setSize(w, h);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel heading = new JLabel("This frame has many components");
heading.setFont(new Font("Arial", Font.BOLD, 16));
JLabel namePrompt = new JLabel("Enter your name:");
JTextField nameField = new JTextField(12);
JButton button = new JButton("Click to continue");
setLayout(new FlowLayout());
add(heading);
add(namePrompt);
add(nameField);
add(button);
}
}
public class Swing {

public static void main(String[] args) {

DialogBox frame = new DialogBox();


frame.setVisible(true);
}

} Output:

You might also like