2010-11-09 66 views
0

我寫了一個方法來創建一個窗體(3個按鈕和一個文本框),然後我在主要調用它。 但是當我運行程序之前,我在窗體中輸入信息(方法form6), 其他執行的命令! 「s4和ontname以表格形式」。 這是我的代碼:::::::::::java窗體按鈕順序執行

////////////////////////////////////////////////////////////////////////// 
public static void main(String[] args) { 
System.out.println("*begin main*"); // call form method 

字符串S4 = form6的一部分(); // s4是由方法返回的。 System.out.println(「s3333 * 」+ s4);
System.out.println(「ont
:」+ ontname); //它是全局的} /////////////////////////// ///////////////////////////

我有2個問題:::

1 ---當窗體運行時,執行其他命令! 他們的訂單執行是什麼? 2. ---我想定義一個按鈕,當我點擊它時,它會關閉窗體。

謝謝大家。

回答

0

您不能在此方法中返回值,因爲ActionListenerInterface不允許這樣做。但是你可以在actionPerformed()方法中調用另一個方法,並將ontname傳遞給它。

您也可以關閉新方法中的第三個按鈕。或者將第三個按鈕定義爲final,並在actionPerformed()方法中使用它。

E.g.

button2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
      String filename = File.separator+"c:"; 
      JFileChooser fc = new JFileChooser(new File(filename)); 
      fc.showOpenDialog(null); 
      File selFile = fc.getSelectedFile(); 
      ontname=selFile.getPath(); 
      System.out.println("filepath: "+ontname); //it works correctly. 
      anotherMethod(ontname); 
     } 
     }); 

private void anotherMethod(String path) { 
     //doSomething with the path 

     //close third button here 
} 
+0

謝謝大家。我明白。我還有一個問題:我寫一個方法來創建一個表單,然後我把它稱爲主表單。 public static void main(String [] args){System.out.println(「* begin main *」); //調用表單方法String s4 = form6(); //打印全局變量System.out.println(「s3333 **」+ s4); System.out.println(「ont *:」+ ontname); //它是全局的}但是當我運行程序之前,我在窗體中輸入信息(方法form6),執行其他命令!在窗體運行時,其他命令被執行!他們的訂單執行是什麼? - fateme.hosseini – 2010-11-09 09:17:19

1

如果我正確獲取代碼,ontname是:(1)一個類的成員(方法之外聲明)或(2)的局部變量,這是在包含此代碼段的方法聲明。

在這兩種情況下,都不需要「返回」ontname,只是因爲它沒有在匿名ActionListener實例內聲明。

下面的例子說明了這個問題的典型模式:

public void someMethod() { 
    // ... 
    button2.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     String filename = File.separator+"c:"; 
     JFileChooser fc = new JFileChooser(new File(filename)); 
     fc.showOpenDialog(null); 
     File selFile = fc.getSelectedFile(); 
     setOntName(selFile.getPath()); // <-- here we call another method 
    } 
    }); 
    // ... 
} 

void setOntName(String ontName) { 
    // do something with ontName 
} 

Alternativly:(只)宣佈ontName作爲類的靜態成員:

private static String ontName = ""; // <-- accessible from main method 
public static void main(String[] args) { 
    // ... 
} 
// more methods. 
0

你也許可以定義變量ontname爲全球,您的功能之外:

var ontname = null; 
button2.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
// ... 
    ontname=selFile.getPath(); 
    } 
}); 

// ... 

System.out.println("filepath: "+ontname); 
+0

無論如何。但是當表單正在運行時,其他命令被執行! 「System.out.println(」filepath:「+ ontname);」打印「null」。 – 2010-11-15 04:56:17

0

如果您想要記住這些值,那麼它們應該是班級變量。

但是,通常情況下,您會希望將這些傳遞給其他方法,以對它們進行一些處理(或者說,將它們保留在文件中)。您可以將這些參數作爲參數傳遞給其他方法。

(第二個是在大多數情況下更好的,我不知道你的應用程序很多,所以我無法給出一個答案)

還有其他問題,您的代碼:

  1. 您需要檢查使用是否在打開的對話框中單擊了「確定」或「取消」按鈕來決定是否獲取文件。

  2. String filename = File.separator+"c:";確實沒有意義。也許你的意思是String filename = "c:"+File.separator;但即使這樣也不是很有用。 File.separator用於獲取特定於平臺的文件分隔符char(Windows中的\,linux上的/),但由於您的硬編碼爲c:,因此您無論如何都將您的應用限制在Windows中。您可能希望有一個更好的平臺獨立方式(從「默認」路徑開始,new JFileChooser()不帶參數,然後記住用戶上次使用的路徑,並從那裏繼續)

  3. 如果參數showOpenDialog方法是你的父框架,那麼對話框將以父框架爲中心,並且在大多數情況下,看起來會更好。

您可能還想重新查看變量名稱。

button2.addActionListener(new ActionListener() 
{  
    public void actionPerformed(ActionEvent e) 
    { 
     String filename = File.separator+"c:"; 
     JFileChooser fc = new JFileChooser(new File(filename)); 
     int option = fc.showOpenDialog(null); 
     if(option = JFileChooser.APROVE_OPTION) 
     { 
     File selFile = fc.getSelectedFile(); 
     String ontname=selFile.getPath(); 
     System.out.println("filepath: "+ontname); //it works correctly. 
     doSomeOperation(ontname); //Or, declare ontname as a class level variable. 
     } 
    } 
}); 
+0

謝謝大家。我明白。我還有一個問題:我寫一個方法來創建一個表單,然後我把它稱爲主表單。 public static void main(String [] args){ \t \t System.out.println(「* begin main *」); \t \t //調用形式方法 \t \t String s4 = form6(); \t \t //打印全局變量 \t \t System.out.println(「s3333 **」+ s4);但是當我運行程序之前,我在窗體(方法form6)中輸入信息之前,其他正在執行的命令! – 2010-11-09 09:04:43

+0

窗體運行時,執行其他命令!他們的訂單執行是什麼? – 2010-11-09 09:14:07