📄 receiveclient.java
字号:
package client;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import both.CallBack;
import both.Stock;
public class ReceiveClient extends JFrame implements CallBack, ActionListener{
private static final long serialVersionUID = 1L;
private JLabel lm = new JLabel("Receive Client");
private Font f=new Font("TimesNewRoman",Font.BOLD,24);
private JLabel l1=new JLabel("Stock Symbol");
private JLabel l2= new JLabel("Value");
private JTextField t1 = new JTextField("");
private JTextField t2 = new JTextField("");
private JButton b1 = new JButton("Callback");
private JButton b2 = new JButton("interrupt");
CallBack c = null;
Stock a = null;
public ReceiveClient(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
lm.setFont(f);
this.add(lm); lm.setBounds(50,10,200,30);
this.add(l1); l1.setBounds(30,50,100,30);
this.add(t1); t1.setBounds(30,90,200,30);
this.add(l2); l2.setBounds(30,130,100,30);
this.add(t2); t2.setBounds(30,170,200,30);
this.add(b1); b1.setBounds(30,220,90,30);
b1.addActionListener(this);
this.add(b2); b2.setBounds(150,220,90,30);
b2.addActionListener(this);
setSize(280,300);
setVisible(true);
try{
Registry registry = LocateRegistry.getRegistry(null);
a = (Stock) registry.lookup("Stock");
c = (CallBack) UnicastRemoteObject.exportObject(ReceiveClient.this, 0);
} catch (Exception ex) {
System.err.println("Client exception: " + ex.toString());
ex.printStackTrace();}
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == b1){
try {
a.register(c);
}catch (RemoteException e1){
e1.printStackTrace();}
}
else if (source == b2){
try {
a.interrupt(c);
}catch (Exception ex){
System.err.println("Client exception: " + ex.toString());
ex.printStackTrace();}
}
}
public void callBack(String stockSymbol, int value) throws RemoteException {
t1.setText(stockSymbol);
t2.setText(Integer.toString(value));
}
public static void main(String[] args) {
System.out.println("Receive Ready");
new ReceiveClient();}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -