书本Applet程序练习------同一页Applet之间的通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013907419/article/details/27233575
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Week13  extends Applet
{
	Panel panel1=new Panel();
	TextArea textArea1=new TextArea();
	Label label1=new Label("我在511,向510发消息:");
	TextField textField1=new TextField();
	Button button1=new Button("发送");
	GridLayout gridLayout1=new GridLayout();
	Panel Panel2=new Panel();
	TextArea textArea2=new TextArea();
	Label label2=new Label("我在510,向511发消息:");
	TextField textField2=new TextField();
	Button button2=new Button("发送");
	GridLayout gridLayout2=new GridLayout();
	public void init()
	{
		this.setLayout(gridLayout1);
		textArea1.setColumns(20);
		textArea1.setRows(1);
		textArea1.setText("从510收到的消息:\n");
		button1.addActionListener(new java.awt.event.ActionListener()
		{public void actionPerformed(ActionEvent e)
		{button1_actionPerformed(e);}});

		textField1.setColumns(20);
		gridLayout1.setColumns(1);
		gridLayout1.setRows(2);
		this.add(panel1,null);
		panel1.add(label1,null);
		panel1.add(textField1,null);
		panel1.add(button1,null);
		this.add(textArea1,null);
		this.setLayout(gridLayout2);
		textArea2.setColumns(20);
		textArea2.setRows(1);
		textArea2.setText("从511收到信息:\n");
		button2.addActionListener(new java.awt.event.ActionListener()
		{public void actionPerformed(ActionEvent e)
		{button2_actionPerformed(e);}});
		textField2.setColumns(20);
		gridLayout2.setColumns(1);
		gridLayout2.setRows(2);
		this.add(Panel2,null);
		Panel2.add(label2,null);
		Panel2.add(textField2,null);
		Panel2.add(button2,null);
		this.add(textArea2,null);
	}
	public String getAppletInfo()
	{
		return "我是" + this.getParameter("name");
	}
	void button1_actionPerformed(ActionEvent e)
	{
		textArea2.append(textField1.getText());
	}
	void button2_actionPerformed(ActionEvent e)
	{

		textArea1.append(textField2.getText());
	}
	public void receive(String msg)
	{
		textArea1.append(msg +"\n");
		textArea2.append(msg +"\n");
	}
}
 
 


      经过亲身运行,书本上的这段代码的有误,其中panel2应为Panel2

猜你喜欢

转载自blog.csdn.net/u013907419/article/details/27233575