2013-03-04 59 views
0

我有一個學生客戶端創建一個RMI服務器,像這樣學生GUI與RMI服務器

名稱----地址的程序-------類組
湯姆-----低街-----網絡2
瑪麗----高街----第2場
約翰----中街 - 網頁2

,我有哪些使用3 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的指針。

謝謝您提前。

回答

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"); 
     } 
}