0% found this document useful (0 votes)
231 views9 pages

Swings JLabel, Image Icon, JTextField

This document discusses some Swing component classes including JLabel and JTextField. JLabel can display text and icons, and does not respond to user input. JLabel defines constructors to set the text, icon, and alignment. Icons are specified using the ImageIcon class. JTextField allows editing one line of text and gets/sets the text. Examples are provided to demonstrate using JLabel and JTextField.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
231 views9 pages

Swings JLabel, Image Icon, JTextField

This document discusses some Swing component classes including JLabel and JTextField. JLabel can display text and icons, and does not respond to user input. JLabel defines constructors to set the text, icon, and alignment. Icons are specified using the ImageIcon class. JTextField allows editing one line of text and gets/sets the text. Examples are provided to demonstrate using JLabel and JTextField.

Uploaded by

Shobha Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SWINGS

Exploring Swing
Some of the Swing component classes:

Allare lightweight and are derived


from JComponent.
JLabel and Image Icon
 JLabel can be used to display text and/or an icon.

 It does not respond to user input.

 JLabel defines several constructors:


JLabel(Icon icon)
JLabel(String str)
JLabel(String str, Icon icon, int align)

 The align argument specifies the horizontal


alignment of the text. i.e; LEFT, RIGHT, CENTER,
LEADING, or TRAILING.
 icons are specified by objects of type Icon, which is an
interface defined by Swing. The easiest way to obtain an
icon is to use the ImageIcon class. ImageIcon implements
Icon and encapsulates an image.

 ImageIcon constructor : ImageIcon(String filename)

 The icon and text associated with the label can be


obtained by the following methods:
Icon getIcon( )
String getText( )

 The icon and text associated with a label can be set by


these methods:
void setIcon(Icon icon)
void setText(String str)
// Demonstrate JLabel and ImageIcon.

import java.awt.*;
import javax.swing.*;
/*
<applet code="JLabelDemo" width=250 height=150>
</applet>
*/
public class JLabelDemo extends JApplet {
public void init() {
try
{
SwingUtilities.invokeAndWait( new Runnable() {
public void run() {
makeGUI();
}
}
);
} catch (Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI() {
// Create an icon.
ImageIcon ii = new ImageIcon("france.gif");
// Create a label.
JLabel jl = new JLabel("France", ii, JLabel.CENTER);
// Add the label to the content pane.
add(jl);
}
}
JTextFiel

d
Is the simplest Swing text component.
• Allows us to edit one line of text.
• Derived from JTextComponent.

Three of JTextField’s constructors:

JTextField(int cols)
JTextField(String str , int cols)
JTextField(String str)
String getText() - obtain the text currently in the text
field.
// Demonstrate JTextField.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="JTextFieldDemo" width=300 height=50>
</applet>
*/
public class JTextFieldDemo extends JApplet {
JTextField jtf;
public void init() {
try {
SwingUtilities.invokeAndWait(
new Runnable() { public void run() { makeGUI();
}
}
);
} catch (Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI() {
// Change to flow layout.
setLayout(new FlowLayout());
// Add text field to content pane.
jtf = new JTextField(15);
add(jtf);
jtf.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent
ae) {
// Show text when user presses ENTER.
showStatus(jtf.getText());
}
});
}
}

You might also like