带有RMI服务器的学生GUI

时间:2013-03-04 11:28:25

标签: user-interface rmi

我有一个为学生客户端创建RMI服务器的程序,如此

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

我有一个gui使用3标题,但我想让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");
      }
}

StudentClient代码

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的指针。

提前谢谢你。

1 个答案:

答案 0 :(得分: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");
      }
}