📄 applet1.java
字号:
import java.awt.*;
import java.applet.*;
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class Applet1 extends Applet
{
String fontList[];
public void init()
{
initForm();
usePageParams();
//getToolkit()为Applet类的方法 获得Applet的Toolkit对象
//getFontList()为Toolkit对象的方法 获得Toolkit对象的字体名称列表(英文)
fontList=getToolkit().getFontList();
// TODO: Add any constructor code after initForm call.
}
public void paint (Graphics g){
//Dimension为一个尺寸类由高宽两个数据成员组成
//getSize()为Applet为类的一个方法获得当前Applet对象的尺寸
Dimension d=this.getSize();
int i;
//根据Applet对象的位置确定显示字体名称的位置
int x=d.width/3,y=d.height /fontList.length-5;
for ( i=0;i<fontList.length;i++){
Font f; //定义一个字体对象句柄
//字体对象实体由字体名 字体格式 字体大小三部分组成
f=new Font(fontList[i],Font.BOLD,18);
//设置当前使用的字体
g.setFont(f);
g.drawString(fontList[i],5,y*(i+1));
f=new Font(fontList[i],Font.ITALIC ,18);
g.setFont(f);
g.drawString(fontList[i],x,y*(i+1));
f=new Font(fontList[i],Font.PLAIN ,18);
g.setFont(f);
g.drawString(fontList[i],x+x,y*(i+1));
}
Font f;
f=new Font("黑体",Font.BOLD,28);
g.setFont(f);
g.drawString("黑体字体示例",5,y*(i+1));
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the <PARAM NAME="label" VALUE="some string">,
* <PARAM NAME="background" VALUE="rrggbb">,
* and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -