client.java

来自「用java实现的投票系统」· Java 代码 · 共 105 行

JAVA
105
字号
import javax.swing.*;
import java.awt.*;
import java.rmi.*;
import java.rmi.server.*;
import java.awt.event.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client extends JApplet implements Callbackinterface{
	    private Serverinterface h;
	    private boolean isStandalone; // Is applet or application
	    public int myturn=1;   //everyone only have one vote
	    private JTextArea jtextarea=new JTextArea();
		private Label label1=new Label("the choices of the votes");
		private Label label2=new Label("1 yes");
		private Label label3=new Label("2 no");
		private Label label4=new Label("3 do not care");
		private Label label5=new Label("submit the answer(press the number)");
		private JTextField jtextfield =new JTextField();
		private Label label6=new Label("if be interested,press:");
		private Label label7=new Label("the votes will be displayed below");
		private JButton Button=new JButton("request");
	Client() {
		   initializeRMI();
		
		// constructor
       // System.setSecurityManager(new RMISecurityManager());
       
 //try{
        // export this object as a remote object
       // UnicastRemoteObject.exportObject(this);
       // Registry registry = LocateRegistry.getRegistry("localhost");
        //h = (Serverinterface)registry.lookup("obj");
           //h.addCallback(this);
          //}
      //catch(Exception ex){}
      //finally{
    	    JFrame frame = new JFrame();
    	    frame.setTitle("client");
    	    frame.add(this,BorderLayout.CENTER);
    	    frame.setSize(650, 300);
    	    this.ini();
    	    frame.setLocationRelativeTo(null);
    	    frame.setVisible(true);
    	    frame.setDefaultCloseOperation(3);
      }
   // end constructor
	
	protected void initializeRMI() {
	    String host = "";
	    //if (!isStandalone) host = getCodeBase().getHost();
	    try {
	      Registry registry = LocateRegistry.getRegistry("localhost");
	      h = (Serverinterface)registry.lookup("Server");
	      System.out.println("Server object " + h + " found");
	    }
	    catch(Exception ex) {
	      System.out.println(ex);
	    }
	  }

	public void ini(){
		JPanel p1=new JPanel();
		p1.setLayout(new GridLayout(8,1));
		p1.add(label1);
		p1.add(label2);
		p1.add(label3);
		p1.add(label4);
		p1.add(label5);
		p1.add(jtextfield);
		p1.add(label6);
		p1.add(Button);
		add(p1,BorderLayout.EAST);
		add(jtextarea,BorderLayout.CENTER);
		add(label7,BorderLayout.NORTH);
		jtextarea.setEditable(false);
		jtextfield.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent evt) {
	    try{
	       if(myturn==1)
	    	h.update(jtextfield.getText().trim());
            myturn--;	  
	    }
	    catch(Exception e){	
	    }
	   
	      }
	    });
	}// end ini
  
	// call back method - this displays the message sent by the server
   public void callMe (String message) {
      System.out.println( "Call back received: " + message );
   }
   public static void main(String args[]) {  // ?
         Client client  = new Client();  // ?
           
   } // end main
} // end Client class

	


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?