0

我有一个程序可以像这样为学生客户端创建一个 RMI 服务器

姓名----地址-----班级组
汤姆-----低街-----网络 2
玛丽----高街----游戏 2
约翰----中街--网络2

我有一个使用 3 标题的 gui,但我想让 gui 在运行时自动更新,而不是通过单击按钮来显示更新的信息,而不是在此之后显示的上面的信息。

它只是我坚持的 GUI 位,我不知道如何在服务器和客户端运行之后然后在 GUI 运行时使用此信息填充 GUI。

到目前为止,我已经尝试过做一些不同的事情,但没有运气。

这是 Gui 代码

import java.awt.event.*;

import javax.swing.*;
import java.awt.GridLayout;

public class StudentGui extends JFrame
{
    private JTextField NameLabel= new JTextField("Name");
    private JTextField AddressLabel= new JTextField("Address");
    private JTextField GroupLabel= new JTextField("Group");
    private JTextField FirstName= new JTextField(" ");
    private JTextField SecondName= new JTextField(" ");
    private JTextField ThirdName= new JTextField(" ");
    private JTextField FirstAddress= new JTextField(" ");
    private JTextField SecondAddress= new JTextField(" ");
    private JTextField ThirdAddress= new JTextField(" ");
    private JTextField FirstGroup= new JTextField(" ");
    private JTextField SecondGroup= new JTextField(" ");
    private JTextField ThirdGroup= new JTextField(" ");

    StudentGui(String s){
        super(s);

            getContentPane().setLayout(new GridLayout(4,3));
            getContentPane().add(NameLabel);
            getContentPane().add(AddressLabel);
            getContentPane().add(GroupLabel);
            getContentPane().add(FirstName);
            getContentPane().add(FirstAddress);
            getContentPane().add(FirstGroup);
            getContentPane().add(SecondName);
            getContentPane().add(SecondAddress);
            getContentPane().add(SecondGroup);
            getContentPane().add(ThirdName);
            getContentPane().add(ThirdAddress);
            getContentPane().add(ThirdGroup);

            setSize(350,150);
            setVisible(true);

    }
      public static void main(String[] args){
         new StudentGui("RMI GUI Client");
      }
}

学生客户端代码

import java.rmi.*;

public class StudentClient
{
    public static void main(String args[]){
        String url = "rmi:///";

        try{
            StudentInterface aTom=(StudentInterface)Naming.lookup(url+"Tom");
            StudentInterface aMary=(StudentInterface)Naming.lookup(url+"Mary");
            StudentInterface aJohn=(StudentInterface)Naming.lookup(url+"John");

            System.out.println("Name    Address     Class Group");
            System.out.println(aTom.getName()+" "+aTom.getAddress()+"   "+aTom.getGroup());
            System.out.println(aMary.getName()+"    "+aMary.getAddress()+"  "+aMary.getGroup());
            System.out.println(aJohn.getName()+"    "+aJohn.getAddress()+"  "+aJohn.getGroup());

            System.out.println("\n" + "Changed too:" + "\n" );

            aTom.setAddress("Lower Street");
            aMary.setAddress("Higher Street");
            aMary.setGroup("Game 3");
            aJohn.setGroup("Web 3");

            System.out.println("Name    Address     Class Group");
            System.out.println(aTom.getName()+" "+aTom.getAddress()+"   "+aTom.getGroup());
            System.out.println(aMary.getName()+"    "+aMary.getAddress()+"  "+aMary.getGroup());
            System.out.println(aJohn.getName()+"    "+aJohn.getAddress()+"  "+aJohn.getGroup());

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

如果您需要代码的其余部分,可以提供。我并不是要为我做这件事,也许只是一个关于我将如何用这些信息填充 GUI 的指针。

先感谢您。

4

1 回答 1

0

以防万一有人做过这样的事情,这就是我最终弄清楚的方式很容易:)

Java 代码:

import java.awt.event.*;

import javax.swing.*;
import java.awt.GridLayout;
import java.rmi.Naming;

public class StudentGui extends JFrame
{
    private JTextField NameLabel= new JTextField("Name");
    private JTextField AddressLabel= new JTextField("Address");
    private JTextField GroupLabel= new JTextField("Group");
    private JTextField FirstName= new JTextField("");
    private JTextField SecondName= new JTextField("");
    private JTextField ThirdName= new JTextField("");
    private JTextField FirstAddress= new JTextField("");
    private JTextField SecondAddress= new JTextField("");
    private JTextField ThirdAddress= new JTextField("");
    private JTextField FirstGroup= new JTextField("");
    private JTextField SecondGroup= new JTextField("");
    private JTextField ThirdGroup= new JTextField("");

    String url = "rmi:///";

    StudentGui(String s){
        super(s);

            getContentPane().setLayout(new GridLayout(4,3));
            getContentPane().add(NameLabel);
            getContentPane().add(AddressLabel);
            getContentPane().add(GroupLabel);
            getContentPane().add(FirstName);
            getContentPane().add(FirstAddress);
            getContentPane().add(FirstGroup);
            getContentPane().add(SecondName);
            getContentPane().add(SecondAddress);
            getContentPane().add(SecondGroup);
            getContentPane().add(ThirdName);
            getContentPane().add(ThirdAddress);
            getContentPane().add(ThirdGroup);

            setSize(350,150);
            setVisible(true);    

            try{
                StudentInterface aTom=(StudentInterface)Naming.lookup(url+"Tom");
                FirstName.setText(aTom.getName());
                FirstAddress.setText(aTom.getAddress());
                FirstGroup.setText(aTom.getGroup());
                StudentInterface aMary=(StudentInterface)Naming.lookup(url+"Mary");
                SecondName.setText(aMary.getName());
                SecondAddress.setText(aMary.getAddress());
                SecondGroup.setText(aMary.getGroup());
                StudentInterface aJohn=(StudentInterface)Naming.lookup(url+"John");
                ThirdName.setText(aJohn.getName());
                ThirdAddress.setText(aJohn.getAddress());
                ThirdGroup.setText(aJohn.getGroup());
            }catch(Exception a){
                a.printStackTrace();
            }
    }
      public static void main(String[] args){
         new StudentGui("RMI GUI Client");
      }
}
于 2013-03-05T13:13:36.030 回答