package com.mindprod.example;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* demonstrate the use of javax.swing.JLabel.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-01-01 initial version
* @since 2009-01-01
*/
public final class TestJLabel
{
private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 );
private static final Color PALE_CYAN = new Color( 200, 255, 200 );
private static final Font FONT_FOR_LABELS = new Font( "Dialog", Font.PLAIN, 15 );
/**
* track whether showing false=short or true=long string.
*/
private static boolean toggle = false;
/**
* Debugging harness for a Frame
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
SwingUtilities.invokeLater( new Runnable()
{
/**
* fire up a JFrame on the Swing thread
*/
public void run()
{
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
contentPane.setLayout( new FlowLayout( FlowLayout.LEADING ) );
final JLabel plainLabel = new JLabel( "this is a TEST" );
plainLabel.setBackground( Color.BLACK );
plainLabel.setForeground( Color.YELLOW );
plainLabel.setOpaque( true );
plainLabel.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
plainLabel.setHorizontalAlignment( JTextField.RIGHT );
plainLabel.setEnabled( true );
final JLabel htmlLabel =
new JLabel( "<html lang=\"en-CA\"><u>look! underlined!</u></html>" );
htmlLabel.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
htmlLabel.setHorizontalAlignment( JTextField.LEFT );
htmlLabel.setEnabled( true );
final JLabel fancyLabel = new JLabel( "Hello" );
fancyLabel.setHorizontalAlignment( JLabel.RIGHT );
fancyLabel.setHorizontalTextPosition( JLabel.LEFT );
fancyLabel.setBackground( Color.YELLOW );
fancyLabel.setForeground( FOREGROUND_FOR_LABEL );
fancyLabel.setOpaque( true );
fancyLabel.setFont( FONT_FOR_LABELS );
final ImageIcon raspberry = new ImageIcon( TestJLabel.class.getResource(
"raspberry.png" ) );
fancyLabel.setIcon( raspberry );
Border border = BorderFactory.createEmptyBorder( 2, 2, 2, 2);
fancyLabel.setBorder( border );
fancyLabel.setText( "adios" );
final JLabel varyingLabel = new JLabel( "a short string" );
varyingLabel.setHorizontalAlignment( JLabel.CENTER );
varyingLabel.setHorizontalTextPosition( JLabel.CENTER );
varyingLabel.setBackground( PALE_CYAN );
varyingLabel.setForeground( FOREGROUND_FOR_LABEL );
varyingLabel.setOpaque( true );
varyingLabel.setFont( FONT_FOR_LABELS );
final JButton toggleButton = new JButton( "toggle" );
toggleButton.setHorizontalAlignment( JLabel.CENTER );
toggleButton.setHorizontalTextPosition( JLabel.CENTER );
toggleButton.setBackground( PALE_CYAN );
toggleButton.setForeground( FOREGROUND_FOR_LABEL );
toggleButton.setOpaque( true );
toggleButton.setFont( FONT_FOR_LABELS );
toggleButton.setBorder( BorderFactory.createRaisedBevelBorder() );
toggleButton.setFocusPainted( false );
toggleButton.setToolTipText( "Toggle long and short label text." );
toggleButton.addActionListener( new ActionListener()
{
/**
* Invoked when button pressed
*/
public void actionPerformed( ActionEvent e )
{
toggle = !toggle;
varyingLabel.setText( toggle ? "a long string of text to display" : "a short string" );
}
} );
contentPane.add( plainLabel );
contentPane.add( htmlLabel );
contentPane.add( fancyLabel );
contentPane.add( varyingLabel );
contentPane.add( toggleButton );
jFrame.setSize( 560, 70 );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.validate();
jFrame.setVisible( true );
}
} );
}
}