//create and display a label containing icon and a string
//JLabel and ImageIcon
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JLabel.*;
public class JLabelDemo extends JApplet {
public void init() {
try {
SwingUtilities.invoke(
new Runnable() {
public void run() {
makeGUI();
}
}
);
}
catch(Exception e) {
System.out.println("Cannot happen due to exception " + e);
}
}
private void makeGUI() {
//create an icon
ImageIcon ii=new ImageIcon("The Big Trip.png");
//create a label
JLabel jl=new JLabel("India",ii,JLabel.CENTER);
add(jl);//add label to content pane
}
}
/*<applet code="JLabelDemo" height=250 width=150>*/
此代码使用以下代码编译:
javac JLabelDemo.java
但是使用以下命令通过 cmd 运行是行不通的(不显示任何小程序)!!
appletviewer JLabelDemo.java