2013-07-30 76 views
0

我是GUI新手。 我正在嘗試爲已有的java程序創建一個gui。 我希望用戶輸入文件的端口號和位置,從那裏我想用我已經制作的程序來完成剩下的工作。 我很困惑我將如何從用戶輸入中獲取值並實現到程序中。java gui swing用戶輸入操作

這是我的計劃框架

public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener { 


    private Label lblPort; // declare component Label 
    private TextField tfPort; // declare component TextField  
    private int port;  // port number 



    /** WindowEvent handlers */ 
    // Called back upon clicking close-window button 
    @Override 
    public void windowClosing(WindowEvent e) { 
     System.exit(0); // terminate the program 
    } 




    //constructor for frame 
    public TcpServerCompareCSV() { 
     setLayout(new FlowLayout()); 
     // "this" Frame sets its layout to FlowLayout, which arranges the components 
     // from left-to-right, and flow to next row from top-to-bottom. 

     lblPort = new Label("Port"); // construct Label 
     add(lblPort);     // "this" Frame adds Label 

     tfPort = new TextField("0", 10); // construct TextField 
     tfPort.setEditable(true);  //edit text 
     add(tfPort);      // "this" Frame adds tfCount 



     tfPort.addActionListener(this); // for event-handling 

     setTitle("compare"); // "this" Frame sets title 
     setSize(250, 100);  // "this" Frame sets initial window size 
     setVisible(true);   // "this" Frame shows 


     addWindowListener(this); 
     // "this" Frame fires WindowEvent its registered WindowEvent listener 
     // "this" Frame adds "this" object as a WindowEvent listener 

    } 




    /** ActionEvent handler - Called back when user clicks the button. */ 
    @Override 
    public void actionPerformed(ActionEvent evt) { 
    // Get the String entered into the TextField tfPort, convert to int 
     port = Integer.parseInt(tfPort.getText()); 

    } 





    /** The entry main() method */ 

public static void main(String[] args) throws IOException{ 

     // Invoke the constructor to setup the GUI, by allocating an instance 
    TcpServerCompareCSV app = new TcpServerCompareCSV(); 

回答

1

你的代碼是不完整的,所以我不得不猜測的幾件事情,ESP。關於您的「已製作」程序的外觀。

假設你有一個類中的命令行程序,開始時是這樣的:

public class AlreadyMade 
{ 
    public static void main(String[] arguments) 
    { 
    AlreadyMade am = new AlreadyMade(); 
    am.goToIt(arguments[0], arguments[1]); 
    } 

    public void goToIt(String s1, String s2) 
    { 
    // insert logic here for what to do with your port number and location 
    } 
} 

在這種情況下,您可以從GUI調用goToIt()方法,可能從actionPerformed方法,以同樣的方式從主要在AlreadyMade類中調用它:

... 
AlreadyMade am = new AlreadyMade(); 
am.goToIt(tfPort.getText(), tfFileLocation.getText()); // assume tfFileLocation, etc. 
... 

我也有猜測,這是你想知道什麼;如果沒有,至少你知道它像你想知道的那樣,並且可以改進這個問題。