继续,程序:
CORBAClockClient.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import CLOCKSYS.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class CORBAClockClient extends JFrame implements Runnable {
private JButton exitButton = new JButton("Exit");
private JButton timeButton = new JButton("Time");
private JLabel time = new JLabel("Missing server connection");
private JLabel name = new JLabel();
private JLabel lt = new JLabel("Time: ");
private JLabel ln = new JLabel("Location: ");
private JLabel lz = new JLabel("Time Zone: ");
private Choice choice = new Choice();
private Container pane = getContentPane();
private Panel p1 = new Panel();
private Panel p2 = new Panel();
private Panel p3 = new Panel();
org.omg.CORBA.ORB orb;
org.omg.CORBA.Object obj;
NamingContext nc;
CORBATimeServer cts;
String timezoneid[], tz;
public CORBAClockClient(String[] args) throws Exception {
super("CORBA Clock System Client");
orb = org.omg.CORBA.ORB.init(args, null);
obj = orb.resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(obj);
NameComponent path[] = {new NameComponent("CLOCKSYS","")};
obj = nc.resolve(path);
cts = CORBATimeServerHelper.narrow(obj);
p1.setLayout(new GridLayout(3,1));
p1.add(lt);
p1.add(ln);
p1.add(lz);
p2.setLayout(new GridLayout(3,1));
p2.add(time);
p2.add(name);
p2.add(choice);
p3.setLayout(new GridLayout(1,2));
p3.add(timeButton);
p3.add(exitButton);
pane.setLayout(new FlowLayout());
pane.add(p1);
pane.add(p2);
pane.add(p3);
timeButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
}
);
timezoneid = cts.available_ids();
for(int i=0; i<timezoneid.length; i++) {
choice.addItem(timezoneid[i]);
}
choice.select("Australia/Sydney");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,200);
setResizable(false);
setVisible(true);
show();
}
public void run() {
while(true) {
tz = timezoneid[choice.getSelectedIndex()];
time.setText(cts.time(tz));
name.setText(cts.name(tz));
System.out.println(cts.time(tz));
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
public static void main(String args[]) throws Exception {
CORBAClockClient cInter = new CORBAClockClient(args);
Thread thread = new Thread(cInter);
thread.start();
}
}
-------------------------------------------------------------------------
Clock_sys.java:
import java.util.TimeZone;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
public class Clock_sys {
String time[] = new String[2];
Locale curLocale = Locale.US;
TimeZone tz = TimeZone.getDefault();
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
curLocale);
Date curDate = new Date();
public Clock_sys() {
}
public String _time(String tz_id) {
TimeZone temp = TimeZone.getTimeZone(tz_id);
df.setTimeZone(temp);
return df.format(curDate);
}
public String _name(String tz_id) {
TimeZone tz = TimeZone.getTimeZone(tz_id);
Locale localeTime = Locale.US;
return tz.getDisplayName(localeTime);
}
public long _offset(String tz_id) {
TimeZone t = TimeZone.getTimeZone(tz_id);
return t.getRawOffset()/3600000;
}
public String[] _ids() {
return tz.getAvailableIDs();
}
}
CORBAClockClient.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import CLOCKSYS.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
public class CORBAClockClient extends JFrame implements Runnable {
private JButton exitButton = new JButton("Exit");
private JButton timeButton = new JButton("Time");
private JLabel time = new JLabel("Missing server connection");
private JLabel name = new JLabel();
private JLabel lt = new JLabel("Time: ");
private JLabel ln = new JLabel("Location: ");
private JLabel lz = new JLabel("Time Zone: ");
private Choice choice = new Choice();
private Container pane = getContentPane();
private Panel p1 = new Panel();
private Panel p2 = new Panel();
private Panel p3 = new Panel();
org.omg.CORBA.ORB orb;
org.omg.CORBA.Object obj;
NamingContext nc;
CORBATimeServer cts;
String timezoneid[], tz;
public CORBAClockClient(String[] args) throws Exception {
super("CORBA Clock System Client");
orb = org.omg.CORBA.ORB.init(args, null);
obj = orb.resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(obj);
NameComponent path[] = {new NameComponent("CLOCKSYS","")};
obj = nc.resolve(path);
cts = CORBATimeServerHelper.narrow(obj);
p1.setLayout(new GridLayout(3,1));
p1.add(lt);
p1.add(ln);
p1.add(lz);
p2.setLayout(new GridLayout(3,1));
p2.add(time);
p2.add(name);
p2.add(choice);
p3.setLayout(new GridLayout(1,2));
p3.add(timeButton);
p3.add(exitButton);
pane.setLayout(new FlowLayout());
pane.add(p1);
pane.add(p2);
pane.add(p3);
timeButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
}
);
timezoneid = cts.available_ids();
for(int i=0; i<timezoneid.length; i++) {
choice.addItem(timezoneid[i]);
}
choice.select("Australia/Sydney");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,200);
setResizable(false);
setVisible(true);
show();
}
public void run() {
while(true) {
tz = timezoneid[choice.getSelectedIndex()];
time.setText(cts.time(tz));
name.setText(cts.name(tz));
System.out.println(cts.time(tz));
try {
Thread.sleep(1000);
}catch(InterruptedException ie) {}
}
}
public static void main(String args[]) throws Exception {
CORBAClockClient cInter = new CORBAClockClient(args);
Thread thread = new Thread(cInter);
thread.start();
}
}
-------------------------------------------------------------------------
Clock_sys.java:
import java.util.TimeZone;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
public class Clock_sys {
String time[] = new String[2];
Locale curLocale = Locale.US;
TimeZone tz = TimeZone.getDefault();
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
curLocale);
Date curDate = new Date();
public Clock_sys() {
}
public String _time(String tz_id) {
TimeZone temp = TimeZone.getTimeZone(tz_id);
df.setTimeZone(temp);
return df.format(curDate);
}
public String _name(String tz_id) {
TimeZone tz = TimeZone.getTimeZone(tz_id);
Locale localeTime = Locale.US;
return tz.getDisplayName(localeTime);
}
public long _offset(String tz_id) {
TimeZone t = TimeZone.getTimeZone(tz_id);
return t.getRawOffset()/3600000;
}
public String[] _ids() {
return tz.getAvailableIDs();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货