2013-05-01 44 views
-2

这里有两类显示我想“端口”中的JTextField显示,由于我怎么能要求一个信息在JTextField中()

1级

System.out.println("using port "+portId.getName()); 

2级

textField = new JTextField(); 
frame.getContentPane().add(textField, "5, 3, left, default"); 
textField.setColumns(10); 
+1

将文本框更新方法添加到类2 – Reimeus 2013-05-01 14:55:03

回答

0

我想 “端口” 中的JTextField

01所示

您是否希望将端口显示在JTextField中,或者是否想要获取显示在JTextField中的端口,这并不令人困惑。我在这里提供了实现这两项任务的方式。在class 2 添加方法如下:

pubic String getPort()//to get port shown in JTextField 
{ 
    return textField.getText(); 
} 
public void setPort(String port)//to show the port in JTextField 
{ 
    textField.setText(port); 
} 

并在class 1你可以写如下:

Class2 obj = new Class2(); 
String port = obj.getPort();//to get Port from JTextField 
obj.setPort(port);//to set port in JTextField 
+0

THANKS ALOT GUYS :) – 2013-05-01 23:36:58

1

添加以下方法,以你的2类,并从类调用它1.

public void updatePort(final String port) { 
    // SwingUtilities.invokeLater is only needed if the method is called from outside the EDT 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      textField.setText(port); 
     } 
    }); 
}