📄 sender.java
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Enumeration;
public class Sender extends Applet implements ActionListener
{
private String myName;
private TextField nameField;
private TextArea status;
private String newline;
public void init()
{
GridBagLayout gridBag=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
setLayout(gridBag);
Label receiverLabel=new Label("Receiver name:",Label.RIGHT);
gridBag.setConstraints(receiverLabel,c);
add(receiverLabel);
nameField=new TextField(getParameter("RECEIVERNAME"),10);
c.fill=GridBagConstraints.HORIZONTAL;
gridBag.setConstraints(nameField,c);
add(nameField);
nameField.addActionListener(this);
Button button=new Button("Send message");
c.gridwidth =GridBagConstraints.REMAINDER ;//行结束
c.anchor =GridBagConstraints.WEST;
c.fill =GridBagConstraints.NONE;
gridBag.setConstraints(button,c);
add(button);
button.addActionListener(this);
status=new TextArea(5,60);
status.setEditable(false);
c.anchor =GridBagConstraints.CENTER ;
c.fill =GridBagConstraints.BOTH ;
c.weightx =1.0;
c.weighty =1.0;
gridBag.setConstraints(status,c);
add(status);
myName=getParameter("NAME");
Label senderLabel=new Label("(My name is"+myName+".)",Label.CENTER );
//c.weightx =0;//为什么必须设
//c.weighty =0;
gridBag.setConstraints(senderLabel,c);
add(senderLabel);
newline=System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event){
Applet receiver=null;
String receiverName=nameField.getText();
receiver=getAppletContext().getApplet(receiverName);//从网页中获取指定名称的小应用程序
if(receiver !=null){
if(!(receiver instanceof Receiver)){
status.append("Found applet named"+receiverName+","+"but it's not a Receiver objesct"+newline);
}else{
status.append("Found applet named"+receiverName+newline+" sending message to it."+newline);
((Receiver)receiver).processRequestFrom(myName);
}
}else{
status.append("Couldn't find any applet named"+receiverName+"."+newline);
}
}
public Insets getInsets(){
return new Insets(3,3,3,3);
}
public void paint(Graphics g){
g.drawRect(0,0,getSize().width-1,getSize().height-1);
}
public String getAppletInfo(){
return "Sender by Dong,li";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -