2013-05-01 71 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); 
     } 
    }); 
}