/********************************************************************/
/**
* 类名:Client
* 方法:Client(), initial(), showAnalogueTime(), showDigitalTime(),
* getInterrupt(), getAnalogueInterval(), getDigitalInterval(),
* nominaltostring(), actionPerformed(), getServerIP()
*
* 说明:此类用来具体实现创建客户端,其中包含了连接服务器、设置间隔、显示数字时钟、
* 显示模拟时钟的方法
*
* last modified 2011-11-14
**
*********************************************************************/
package time;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client extends UnicastRemoteObject implements ClientInterface, ActionListener, Serializable{
private Clock displayClock;
private int intervalDigital;
private int intervalAnalogue;
static private JTextField jServerIPText;
private JLabel space_0;
private JLabel nominalText;
private JButton jgetServerIP;
private JButton jnominal;
private JButton jmoni;
public JPanel panel_0;
public JPanel panel_1;
public JPanel panel_2;
boolean moni = false;
boolean nominal = false;
static boolean flag = false;
static private TimeInterface remotetime = null;
public static JFrame frame = new JFrame("客户端");
/**
* 创建一个客户端
* */
public Client() throws RemoteException {
displayClock = new Clock();
intervalDigital = 1;
intervalAnalogue = 60;
initial();
this.jnominal.addActionListener(this);
this.jmoni.addActionListener(this);
this.jgetServerIP.addActionListener(this);
}
/**
* 对客户端进行初始化
* */
private void initial(){
this.jServerIPText = new JTextField("localhost");
this.jServerIPText.setHorizontalAlignment(4);
this.nominalText = new JLabel();
this.space_0 = new JLabel();
JLabel space_1 = new JLabel();
JLabel space_2 = new JLabel();
JTextField jServerIPDis = new JTextField("服务器ip地址");
jServerIPDis.setEditable(false);
this.jgetServerIP = new JButton("连接服务器");
this.jnominal = new JButton("数字时钟(按秒显示)");
this.jmoni = new JButton("模拟时钟(按分钟显示)");
this.panel_1 = new JPanel();
this.panel_1.setLayout(new GridLayout(5, 2));
this.panel_1.add(jServerIPDis);
this.panel_1.add(this.jServerIPText);
this.panel_1.add(this.jgetServerIP);
this.panel_1.add(this.space_0);
this.panel_1.add(space_1);
this.panel_1.add(space_2);
this.panel_1.add(this.jnominal);
this.panel_1.add(this.nominalText);
this.panel_1.add(this.jmoni);
this.panel_1.setSize(400, 100);
this.panel_1.setVisible(true);
this.panel_2 = new JPanel();
this.panel_2.setLayout(new FlowLayout());
this.panel_2.add(displayClock);
this.panel_2.setSize(400, 250);
this.panel_2.setVisible(false);
}
/**
* 显示时间
* 若选定的是模拟时钟,则使用模拟时钟显示时间
* */
@SuppressWarnings("deprecation")
public void showAnalogueTime(long time) throws RemoteException{
Date date = new Date(time);
int hour = date.getHours();
int minute = date.getMinutes();
int second = date.getSeconds();
displayClock.setDate(date);
displayClock.setHour(hour);
displayClock.setMinute(minute);
displayClock.setSecond(second);
if(this.moni){
displayClock.repaint();
}
frame.repaint();
}
/**
* 显示时间
* 选定的是数字时钟,则使用数字时钟显示时间
* */
public void showDigitalTime(long time) throws RemoteException{
Date date = new Date(time);
int hour = date.getHours();
int minute = date.getMinutes();
int second = date.getSeconds();
displayClock.setDate(date);
displayClock.setHour(hour);
displayClock.setMinute(minute);
displayClock.setSecond(second);
if(this.nominal){
this.nominalText.setText(nominaltostring(hour, minute, second));
}
frame.repaint();
}
public int getAnalogueInterval(){
return intervalAnalogue;
}
public int getDigitalInterval(){
return intervalDigital;
}
/**
* 将模拟时间转化为字符串的表示形式
* */
public String nominaltostring(int hour, int minute, int second){
String time = null;
if (hour < 10){
time = "0" + hour + ":";
}
else
time = hour + ":";
if (minute < 10){
time = time + "0" + minute + ":";
}
else
time = time + minute + ":";
if (second < 10){
time = time + "0" + second;
}
else time = time + second;
return time;
}
/**
* 客户端响应点击的操作
* */
public void actionPerformed(ActionEvent event){
if (event.getSource() == this.jnominal && flag){
this.nominal = true;
this.jnominal.setEnabled(false);
}
else
if (event.getSource() == this.jmoni && flag){
this.moni = true;
this.panel_2.setVisible(true);
displayClock.repaint();
this.jmoni.setEnabled(false);
}
else
if(event.getSource() == this.jgetServerIP){
try{
System.out.println(jServerIPText.getText());
remotetime = (TimeInterface)Naming.lookup("rmi://" + jServerIPText.getText() + ":2046/TimeInterface");
this.jgetServerIP.setEnabled(false);
this.space_0.setText("连接服务器成功!");
this.space_0.setVisible(true);
frame.repaint();
flag = true;
}
catch (RemoteException ex){
this.space_0.setText("不能连接到服务器!");
this.space_0.setVisible(true);
frame.repaint();
System.err.println();
}
catch (Exception e){
System.err.println("不能连接到服务器!");
System.exit(0);
}
}
}
/**
* 获取服务器ip地址
* */
private String getServerIP(){
return this.jServerIPText.getText();
}
public static void main(String[] args){
try{
Client client = new Client();
frame.setLayout(new BorderLayout());
frame.add(client.panel_1, "North");
frame.add(client.panel_2, "Center");
frame.setDefaultCloseOperation(3);
frame.setSize(400, 450);
frame.setResizable(false);
frame.setVisible(true);
while(true){
if(flag){
remotetime.addClient(client);
remotetime.notifyClient();
break;
}
}
System.out.println("连接服务器成功");
}
catch (Exception e){
System.err.println("不能连接到服务器");
System.exit(0);
}
}
}
- 1
- 2
前往页