// LabelTest.java
// Demonstrating the JLabel class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LabelTest extends JFrame {
private JLabel label1, label2, label3;
public LabelTest()
{
super( "Testing JLabel" );
Container c = getContentPane();
c.setLayout( new FlowLayout() );
// JLabel constructor with a string argument
label1 = new JLabel( "Label with text" );
label1.setToolTipText( "This is label1" );
c.add( label1 );
// JLabel constructor with string, Icon and
// alignment arguments
Icon bug = new ImageIcon( "bug1.gif" );
label2 = new JLabel( "Label with text and icon",
bug, SwingConstants.LEFT );
label2.setToolTipText( "This is label2" );
c.add( label2 );
// JLabel constructor no arguments
label3 = new JLabel();
label3.setText( "Label with icon and text at bottom" );
label3.setIcon( bug );
label3.setHorizontalTextPosition(
SwingConstants.CENTER );
label3.setVerticalTextPosition(
SwingConstants.BOTTOM );
label3.setToolTipText( "This is label3" );
c.add( label3 );
setSize( 275, 170 );
show();
}
public static void main( String args[] )
{
LabelTest app = new LabelTest();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HtmlLabel {
static String html = "<html>\n" +
"<p>Color and font test:\n" +
"<ul>\n" +
"<li><font color=red>red</font>\n" +
"<li><font color=blue>blue</font>\n" +
"<li><font color=green>green</font>\n" +
"<li><font size=-2>small</font>\n" +
"<li><font size=+2>large</font>\n" +
"<li><i>italic</i>\n" +
"<li><b>bold</b>\n" +
"</ul>\n";
public static void main(String args[]) {
JFrame f = new JFrame("HtmlDemo");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(200,300);
Container c = f.getContentPane();
JLabel theLabel = new JLabel(html);
theLabel.setVerticalAlignment(SwingConstants.CENTER);
theLabel.setHorizontalAlignment(SwingConstants.CENTER);
c.add(theLabel);
//f.pack();
//f.setVisible(true);
f.show();
}
}
Maintained by John Loomis, last updated 1 June 2000