我有一个为学生客户端创建RMI服务器的程序,如下所示
名称——地址类组
Tom-----Low Street-----Web 2
玛丽----高街----游戏2
John----Middle Street—Web 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");
}
}
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与这些信息。
提前谢谢你
以防有人做过这样的事情,这就是我最终发现它很容易的方法:)
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");
}
}