www.pudn.com > jCorba.rar > HelloClientFrame.java
package jcorba;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class HelloClientFrame extends JFrame {
private JPanel contentPane;
private Label label1 = new Label();
private Button button1 = new Button();
//Construct the frame
public HelloClientFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(HelloClientFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
label1.setBounds(new Rectangle(145, 121, 170, 33));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Corba客户端");
button1.setLabel("连接主机");
button1.setBounds(new Rectangle(149, 201, 111, 33));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
contentPane.add(label1, null);
contentPane.add(button1, 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) {
label1.setText("正在连接主机...");
try{
String[] pm = {};
ORB orb = ORB.init(pm, null); //创建和初始化一个ORB实例
//以下两条语句实现取得命名服务初始情况的对象引用并传入参数的功能
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
label1.setText("连接主机成功..."); //若能获取到根命名上下文则说明连接CORBA主机成功
//以下三条语句实现用关联名从命名服务取得所需要的CORBA对象的对象引用的功能
NameComponent nc = new NameComponent("Hello", "");
NameComponent path[] = {nc};
Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
String Hello = helloRef.sayHello(); //调用远程服务对象并打印结果
label1.setText(Hello);
}
catch (Exception e1)
{
System.out.println("ERROR : " + e1) ; //捕捉异常情况
}
}
}