helloclientframe.java

来自「建立一个基于RMI设计的远程调用程序, 可以用于学习RMI技术」· Java 代码 · 共 71 行

JAVA
71
字号
package rmisocket;

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class helloClientFrame extends JFrame{
	private JPanel contentPane;
	private Button button1 = new Button();
	private Label label1 = new Label();
	
	public helloClientFrame(){
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		try{
			jbInit();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void jbInit() throws Exception{
		contentPane = (JPanel)this.getContentPane();
		button1.setLabel("Connect host");
		button1.setBounds(new Rectangle(114,181,112,33));
		button1.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent e){
				button1_actionPerformed(e);
			}
		});
		
		contentPane.setLayout(null);
		this.setSize(new Dimension(338,272));
		this.setTitle("RMI SOCKET CLIENT");
		
		label1.setBounds(new Rectangle(103,102,190,32));
		contentPane.add(button1, null);
		contentPane.add(label1, null);
		RMISocketFactory.setSocketFactory(new RMISocketFoc());
		
	}
	
	protected void processWindowEvent(WindowEvent e){
		super.processWindowEvent(e);
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
			System.exit(0);
		}
	}
	
	void button1_actionPerform(ActionEvent e){
		String serverName = "";
		
		System.setSecurityManager(new RMISeurityManager());
		
		try{
			label1.setText("Connecting...");
			serverName = InetAddress.getLocalHost().getHostName();
			Hello myHello = (Hello)Naming.lookup("//" + serverName + "/HelloWorld");
			String d = myHello.sayHello();
			lable1.setText(d);
		}catch(Exception e1){
			System.out.println("Error:" + e1);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?