📄 sender.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Enumeration;
public class Sender extends JApplet
implements ActionListener {
private String myName;
private JTextField nameField;
private JTextArea status;
private String newline;
public void init() {
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setLayout(gridBag);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(10,10,10,10),
BorderFactory.createLineBorder(Color.black )));
JLabel receiverLabel = new JLabel("Receiver name:",
JLabel.RIGHT);
gridBag.setConstraints(receiverLabel, c);
panel.add(receiverLabel);
nameField = new JTextField(getParameter("receiverName"),30);
c.fill = GridBagConstraints.HORIZONTAL ;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
gridBag.setConstraints(nameField, c);
panel.add(nameField);
nameField.addActionListener(this);
JButton button = new JButton("Send message");
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.CENTER ; //stick to the
//text field
c.fill = GridBagConstraints.NONE; //keep the button
//small
gridBag.setConstraints(button, c);
panel.add(button);
button.addActionListener(this);
status = new JTextArea(5, 60);
status.setEditable(false);
JScrollPane scrollPane = new JScrollPane(status);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weightx = 1.0;
c.weighty = 1.0;
gridBag.setConstraints(scrollPane, c);
panel.add(scrollPane);
myName = getParameter("NAME");
JLabel senderLabel = new JLabel("(My name is " + myName + ".)",
JLabel.CENTER);
c.weightx = 0.0;
c.weighty = 0.0;
gridBag.setConstraints(senderLabel, c);
panel.add(senderLabel);
newline = System.getProperty("line.separator");
this.setContentPane(panel);
}
public void actionPerformed(ActionEvent event) {
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to
//search for.
receiver = getAppletContext().getApplet(receiverName);
if (receiver != null) {
if (!(receiver instanceof Receiver)) {
status.append("Found applet named "
+ receiverName + ", "
+ "but it's not a Receiver object."
+ newline);
} else {
status.append("Found applet named "
+ receiverName + newline
+ " Sending message to it."
+ newline);
//Cast the receiver to be a Receiver object
//(instead of just an Applet object) so that the
//compiler will let us call a Receiver method.
((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 String getAppletInfo() {
return "Sender by Kathy Walrath";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -