2013-02-11 40 views
0

我正在構建一個包,我打算在很多應用程序中重用。 該軟件包包含一個「串口管理器」:無論我將要構建的應用程序如何,我總是需要設置通信端口和相關參數。舊C++程序員,Java新手,需要關於包中的Swing JFrames的幫助

有一種方法可以在包中包含所有必需的代碼,因此無論何時應用程序需要設置端口時,它都會調用包中的方法,以及模態窗體會出現?

......我希望已經明確....

+0

當然,這是可能的。我們該怎樣幫助你? – 2013-02-11 09:54:30

+1

那麼你面臨的挑戰是什麼?您需要幫助創建軟件包或構建swing圖形用戶界面? – Apurv 2013-02-11 09:54:36

+2

從你的comms包(某種通用庫)中依賴Swing聽起來像一個壞主意。如果有的話,我建議它應該是另一種方式。 – Ash 2013-02-11 09:56:03

回答

1

GUI例如:

public class PortConfiguration extends JDialog() { 
    private int baudrate; 
    private String moreStuff; 

    //show window, events and more 
    public PortConfiguration() { 
     super(null,true); 
     this.setVisible(false); 
     //GUI creation... 
    } 

    //get configuration: 
    public int getBaudrate() {} 
    public String getStuff() {} 
} 

端口實現:

public class Port { 
    public Port(int baudrate, String stuff) { 
     //create, open port... 
    } 
} 

,然後在需要的時候可以使用它們:

PortConfiguration portGUI = new PortConfiguration(); 
portGUI.setVisible(true); 
Port p = new Port(portGUI.getBaudrate(), portGUI.getStuff()); 

關於包: 您可以創建一個主包(serialport)與'subpackage'dialog在裏面e GUI類。 Port類將在主包中serialport

0

肯定的:

public class SerialPortManagerFrame extends JFrame 
{ 
    private final JTextField baudRateTextField; 
    // More controls here 

    public SerialPortManagerFrame (int baudRate /* Other parameters here */) 
    { 
     super ("Serial Port Manager"); 

     baudRateTextField.setText (String.valueOf (baudRate)); 
     // Initialize other fields here 

     getContentPane.setLayout (new BorderLayout()); 
     getContentPane.add (baudRateTextField, BorderLayout.NORTH); 
     // Other GUI initialization here including 
    } 

    public int getBaudRate() 
    { 
     return Integer.parseInt (baudRateTextField.getText()); 
    } 

    // Other methods here 
}