📄 例7-14源程序.txt
字号:
例7-14 有Apple1和Apple2, Apple2发送数据给Apple1,并显示出来,
同时利用Apple1控制Apple1的背景颜色。
//Applet1类源程序清单
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import com.borland.jbcl.layout.*;
//import com.borland.jbcl.control.*;
public class Applet1 extends Applet
{
FlowLayout blayout=new FlowLayout();
boolean isStanda1one=false;
Label label1=new Label( ); //标签成员变量
TextArea textArea1=new TextArea(); //多行文本框成员变量
Label label2=new Label(); //标签成员变量
Button button1=new Button(); //按钮成员变量
Button button2=new Button(); //按钮成员变量
Button button3=new Button(); //按钮成员变量
AppletContext ac=null; //AppletContext类成员变量,该对象反映了Applet 运行环境的特征。
Applet appletcolor=null; //AppletContext类成员变量
public Applet1() // Applet1构造方法
{
}
public void init() //初始化Applet
{
try
{
jbInit(); //调用jbInit()
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setBackground(new Color(192,192,192)); //设置背景色
label1.setText("从Applet1接收到的数据:");
label2.setText("控制Applet2的背景颜色:");
button1.setLabel ("红色");
button1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
button2.setLabel("白色");
button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
});
button3.setLabel("恢复");
button3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button3_actionPerformed(e);
}
});
this.setLayout(blayout);
this.add(label1);
this.add(textArea1);
this.add(label2);
this.add(button1);
this.add(button2);
this.add(button3);
ac=this.getAppletContext();
appletcolor=ac.getApplet("Applet2");
}
public String getAppletInfo() //获取 Appet特征信息
{
return "Applet Information";
}
//Get parameter info
public String[][] getParmeterInfo() //获取 Appet参数
{
return null;
}
void button1_actionPerformed(ActionEvent e)
{
appletcolor.setBackground(Color.red);
}
void button2_actionPerformed(ActionEvent e)
{
appletcolor.setBackground(Color.white);
}
void button3_actionPerformed(ActionEvent e)
{
appletcolor.setBackground(new Color(192,192,192));
}
}
//Applet2源程序清单
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Applet2 extends Applet
{
FlowLayout flayout=new FlowLayout();
boolean isStandalone=false;
Button button1=new Button();
AppletContext ac=null;
TextField textField1=new TextField();
Label label1=new Label();
Applet appletcontrol=null;
public Applet2(){}
public void init() //Initialize the Applet
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setBackground(new Color(192,192,192));
button1.setLabel("发送数据到左面的Applet");
button1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
label1.setText("请输入你所需要发送的数据");
this.setLayout(flayout);
this.add(button1);
this.add(textField1);
this.add(label1);
ac=this.getAppletContext();
appletcontrol=ac.getApplet("Applet1");
}
public String getAppletInfo() //Get Applet information
{
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo()
{
return null;
}
void button1_actionPerformed(ActionEvent e)
{
//String msg=textField1.getText();
//if (msg==null||msg.equals(" "))
// return;
String msg = "dgdfgdfg";
TextArea textArea=(TextArea)appletcontrol.getComponent(2);
textArea.append(msg+"\n");
appletcontrol.setBackground(Color.red);
}
}
//源程序清单
<HTML>
<HEAD>
<TITLE>Applet之间通信</TITLE>
</HEAD>
<BODY>
<table width=100%>
<tr>
<td width=50%>第一个Applet</td>
<td width=50%>第二个Applet</td>
</tr>
<tr>
<td width=50%>
<APPLET
CODEBASE=″.″
CODE =″Applet1.class″
NAME =″Applet1″
WIDTH =280
HEIGHT =150
HSPACE =0
VSPACE =0
ALIGN =middle
>
</APPLET>
</td>
<td width=50%>
<APPLET
CODEBASE=″.″
CODE =″Applet2.class″
NAME =″Applet2″
WIDTH =280
HEIGHT =150
HSPACE =0
VSPACE =0
ALIGN =middle
>
</APPLET>
</td>
</tr>
</table>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -