【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-activatableRMIClientFrame.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package activatablermi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
public class activatableRMIClientFrame extends JFrame {
private JPanel contentPane;
private Button button1 = new Button();
private Label label1 = new Label();
//Construct the frame
public activatableRMIClientFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane(); //设计布局
button1.setLabel("连接主机");
button1.setBounds(new Rectangle(128, 183, 96, 29));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
contentPane.setLayout(null);
this.setSize(new Dimension(352, 262));
this.setTitle("可激活RMI客户端");
label1.setBounds(new Rectangle(124, 110, 147, 25));
contentPane.add(button1, null);
contentPane.add(label1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void button1_actionPerformed(ActionEvent e) {
String serverName = "";
System.setSecurityManager(new RMISecurityManager()); //建立安全管理器
try {
label1.setText("正在连接RMI主机...");
serverName = InetAddress.getLocalHost().getHostName(); //得到RMI服务器端的主机名
activatableRMI myHello = (activatableRMI)Naming.lookup("//" + serverName + "/HelloWorld"); //寻找RMI主机的相应服务
String d = myHello.sayHello(); //调用远程函数
label1.setText(d); //显示结果
}
catch(Exception e1) {
System.out.println("Error: " + e1); //捕捉异常情况
}
}
}
上一篇:
activatableRMIClient.java
下一篇:
法律专业开题报告范文