Display a Label Contaning Both an Icon and A String

The following example illustrates how to create and display a label containing both an
icon and a string. The applet begins by getting its content pane. Next, an ImageIcon
object is created for the file france.gif. This is used as the second argument to the
JLabel constructor. The first and last arguments for the JLabel constructor are the label
text and the alignment. Finally, the label is added to the content pane.

import java.awt.*;
import javax.swing.*;
/* */
public class SwingProgram extends JApplet{
public void init()
{
//get content pane
Container cp= getContentPane();
// Create an Icon

ImageIcon ii=new ImageIcon("Indian-Flag-4.JPEG");
//Create a label
JLabel jl=new JLabel("India", ii, JLabel.CENTER);
//Add Label to the Content pane
cp.add(jl);
}
}

Output from this applet is shown here:



No comments:

Post a Comment