简体   繁体   中英

How would I make a GUI for my RMI client/server application?

So I am new to java RMI and making a GUI, but I got the RMI working. Now I have an Interface class called MessageService, a Server class called MessageServer and a Client class called MessageClient. I'd like to make a GUI with a field, where I can write a message, that will then be displayed on the server side. How is this achieved?

EDIT: I have now done some kind of GUI. I also added a function to the Server. I also edited the Client code, so that when I run it, then it would launch my GUI class.

How would I implement it so I could write text into the input field and when sent, it would be seen on the server?

Here is the code for GUI:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class TheGUI extends Application{

    Button sendButton;
    TextField input;

    public static void main(String[] args) throws RemoteException, NotBoundException {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("GUI");

        input = new TextField();
        sendButton = new Button();

        sendButton.setText("send");

        AnchorPane layout = new AnchorPane();

        HBox hbox = new HBox(5, input, sendButton);
        layout.getChildren().addAll(hbox);
        AnchorPane.setTopAnchor(hbox, 10d);

        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                String message = input.getText();
                input.setText("");

            }
        };

        input.setOnAction(event);
        sendButton.setOnAction(event);

        Scene scene = new Scene(layout, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

This is my Edited Interface:

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MessageService extends Remote {
    public void newMessage (String clientID, String message) throws RemoteException;
}

This is my edited server class:

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class MessageServer extends UnicastRemoteObject implements MessageService {

    protected MessageServer() throws RemoteException {
        super();
    }

    @Override
    public void newMessage(String clientID, String message) throws RemoteException {
        System.out.println(clientID + " " + message);
    }

    public static void main (String[] argv)
    {
        try
        {
            Registry registry = LocateRegistry.getRegistry(1099);
            MessageServer messageServer = new MessageServer();
            registry.rebind("MessageService", messageServer); //register with naming service(bind with registry)
            System.out.println("Server is Ready");
        }
        catch (RemoteException e)
        {
            System.out.println("ERROR: Could not create registry");
            e.printStackTrace();
        }


    }
}

This is my Edited Client class:

import javafx.application.Application;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class MessageClient
{
    public static void main (String[] argv) throws RemoteException, NotBoundException {
        try
        {
            Registry registry = LocateRegistry.getRegistry("127.0.0.1");
            MessageService messageService= (MessageService) registry.lookup("MessageService");
            Application.launch(TheGUI.class);
        }
        catch (Exception e)
        {
            System.out.println ("MessageClient exception: " + e);
        }
    }
}

You are very close. Your code is almost complete.

RMI means communication between two, separate JVMs. The first JVM is your server process - which you have completed the coding for, hence I will not repeat it here. The second JVM is your client process. You can combine the GUI code with the RMI client code. And the GUI code you posted can be simplified.

Here is my version of your desired GUI to act as your RMI client.

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MessageClientGui extends Application {
    private static final String CLIENT_ID = "George";

    private static MessageService msgService;

    private TextField message;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label prompt = new Label("Message");
        message = new TextField();
        Button send = new Button("_Send");
        send.setOnAction(this::sendMessage);
        send.setTooltip(new Tooltip("Sends message to [RMI] server."));
        HBox root = new HBox(5.0D, prompt, message, send);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void sendMessage(ActionEvent actnEvnt) {
        try {
            msgService.newMessage(CLIENT_ID, message.getText());
        }
        catch (RemoteException x) {
            x.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry("127.0.0.1");
            msgService = (MessageService) registry.lookup("MessageService");
            launch(args);
        }
        catch (Exception x) {
            x.printStackTrace();
        }
    }
}

Here is an image of the running client.

RMI GUI 客户端

In method main() , I initialize the MessageService client and save it in a static class member variable. When the user clicks on the Send button, method sendMessage() is called. In this method the remote method newMessage() is called with the contents of the TextField and some arbitrary client ID, namely George. As a result, in the console (ie standard output) of the server JVM, a string is printed that starts with: George

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM