📄 applet1.java
字号:
import java.awt.*;
import java.applet.*;
/*Applet在浏览器环境中运行的Java程序代码,其安全机制P182
系统提供的Applet类属于一个从Panel派生出来的容器组件,一个
Applet程序类的对象就是一个容器(相当于一个框架),程序中的
所有内容都出现在该容器中(无需框架),P184.
将Applet程序嵌入到网页中,相当于在网页中插入一个该Applet程序对象.
VJ++为每个Applet程序生成一个Label组件*/
/*系统提供的Applet类预设了几个重要的方法,并在恰当的时机自动调用.
1.init()初始化方法,程序的入口点(最早的执行代码),
程序执行时,自动且只调用一次该方法
2.start() P185
3.stop() P185
4.paint(Graphics g)负责在Applet容器上文字输出与绘制图型,随
init()方法的执行而自动调用
*/
/*Applet应用程序类可以根据需要设计适当的数据成员,并在上述方法中
加入恰当的功能代码(重写上述方法)
*/
public class Applet1 extends Applet
{
String hw_text;//用户自定义数据
static int stop_count,start_count;//用户自定义数据
/**
* The entry point for the applet.
*/
public void paint(Graphics g)
{
g.drawString(hw_text,25,50);
g.drawString("start_count="+start_count,25,70);
g.drawString("stop_count="+stop_count,25,90);
}
public void init()
{
initForm();
hw_text="你好 世界!";
usePageParams();//在程序中使用通过网页传递的参数(标题 背景色 前景色)
}
public void start()
{
start_count+=1;
//paint(this.getGraphics());
}
public void stop()
{
stop_count+=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.
*/
//设置Applet容器的标题 背景色 前景色
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
*/
//初使化Applet容器的窗体
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());//默认布局管理器为FlowLayout
this.add("North",label1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -