📄 timeclient.java
字号:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.rmi.*;
public class timeClient extends UnicastRemoteObject implements timeClientInterface{
myTime mytime = new myTime(new Date()) ;
timeClient timeclient;
public timeClient()throws RemoteException{
super();
}
public void setClient(timeClient timeclient){
this.timeclient = timeclient;
}
public void updateTime(Date date)throws RemoteException{
System.out.println("the current time is: "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
mytime.reDraw(date);
mytime.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class myTime extends JFrame {
private JTextField textField1,textField2;
private JButton button1,button2;
private JLabel label1 = new JLabel("间隔");
private JLabel label2 = new JLabel("秒显示");
private JLabel label3 = new JLabel("当前时间");
private JPanel panel,panel2;
//String registryURL;
private timeServerInterface h;
ClockCanvas clock;
public myTime(Date date){
super("time");
Container container = getContentPane();
container.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
textField1 = new JTextField(15);
textField2 = new JTextField("1",2);
button1 = new JButton("连接");
button1.addActionListener(new Connect());
button2 = new JButton("断开");
button2.addActionListener(new Disconnect());
panel.add(label1);
panel.add(textField2);
panel.add(label2);
panel.add(button1);
panel.add(button2);
panel2.add(label3);
panel2.add(textField1);
container.add(panel,BorderLayout.NORTH);
clock = new ClockCanvas(date);
container.add(clock,BorderLayout.CENTER);
container.add(panel2,BorderLayout.SOUTH);
setSize(450,200);
setVisible(true);
}
class ClockCanvas extends JPanel{
Date dat ;
int xcenter = 200, ycenter = 50;
int Radius = ycenter-5;
public ClockCanvas(Date date){
setSize(400,125);
dat = date;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int xh,yh,xm,ym,xs,ys,s,m,h;
String today;
s=dat.getSeconds(); //获得时间秒
m=dat.getMinutes(); //获得时间分
h=dat.getHours();
today=dat.toLocaleString(); //获得字符串时间格式
textField1.setText(today);
//计算秒的坐标
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*(Radius-5)+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*(Radius-5)+ycenter); //计算分钟的坐标
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*(Radius-10)+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*(Radius-10)+ycenter); //计算小时的坐标
xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*(Radius-20)+xcenter);
yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*(Radius-20)+ycenter);
g.setColor(Color.darkGray); //设置颜色
g.drawString("9",xcenter-(Radius-5),ycenter+3); //显示时钟上的数字‘9’
g.drawString("3",xcenter+(Radius-10),ycenter+3); //显示时钟上的数字‘3’
g.drawString("12",xcenter-5,ycenter-(Radius-13)); //显示时钟上的数字'12'
g.drawString("6",xcenter-3,ycenter+(Radius-10)); //显示时钟上的数字'6'
g.drawString(today,0,125); //显示字符串时钟
g.drawLine(xcenter,ycenter,xs,ys); //画秒针
g.setColor(Color.blue); //设置颜色
g.drawArc(xcenter-Radius,ycenter-Radius,2*Radius,2*Radius,0,360); //画钟
g.drawLine(xcenter,ycenter-1,xm,ym); //画分针
g.drawLine(xcenter-1,ycenter,xm,ym); //画分针
g.drawLine(xcenter,ycenter-1,xh,yh); //画时针
g.drawLine(xcenter-1,ycenter,xh,yh); //画时针 //setVisible(true);
}
public void redraw(Date date){
dat = date;
repaint();
}
}
class Connect implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
String timeDuration = textField2.getText();
int time = Integer.parseInt(timeDuration);
System.out.println(time);
String registryURL = "rmi://localhost"+":"+2345+"/callback";
h = (timeServerInterface)Naming.lookup(registryURL);
System.out.println("Lookup complete");
Date date = h.getTime();
System.out.println("the current time is: "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
String today=date.toLocaleString(); //获得字符串时间格式
textField1.setText(today);
h.register(timeclient,time);
}
catch(Exception re){
System.out.println("Exception in CallbackClient: "+re);
}
}
}
class Disconnect implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
h.unregister(timeclient);
}catch(RemoteException re){
System.out.println("RemoteException in myTime's Disconnect of timeClient :"+re);
}
}
}
public void reDraw(Date date){
clock.redraw(date);
}
}
public static void main(String[] args){
try{
timeClient timeclient = new timeClient();
timeclient.setClient(timeclient);
}catch(RemoteException re){
System.out.println("RemoteException of timeClientImp: "+re);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -