2014-12-19 29 views
0

我有連接到JTextFieldActionListener,想輸入的東西,這樣它會退出方法ActionListener如何退出方法的ActionListener

代碼:

main() { 
    Security(x,x,x); 
} 
public void Security(JTextArea out, JTextField in) { 
     in.setText(""); 
     in.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       if (in.getText().contains("exitsys")) { 
        out.append("Security:Security System Deactivated\n"); 
        return; 
       } 
       in.setText(""); 
      } 
     }); 
     out.append("Security:Security System Activated\n"); 
     fileWrite(":[email protected]" + time(), out); 
    } 

我想鍵入"exitsys"並返回到主類方法"main()"

fileWrite方法使用PrintWriter來輸出數據。

問題概要:我嘗試調用return;但它不返回到main()方法,我該如何解決這個問題?

+0

*請參閱How to Make Dialogs「..並想輸入的東西,它退出方法的ActionListener是」 *等待..what?如果用戶正在'輸入內容',那麼代碼流通常不會在'actionPerformed(..)'方法中。 – 2014-12-19 06:21:32

+0

你看過使用某種對話框嗎?看看[如何製作對話框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer 2014-12-19 06:21:49

+0

用戶在向JTextField輸入文本時按下Enter鍵,我從此獲得了此代碼一本oracle書,所以我認爲它是正確的。我只想返回,因爲我打電話回來;它didesent退出方法安全() – 2014-12-19 06:23:38

回答

3

基本上你需要的是某種模態對話框,它可以讓你有效地停止程序的執行,直到對話框關閉爲止,執行結束繼續...

import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JDialog dialog = new JDialog(); 
       dialog.setTitle("Testing"); 
       dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       dialog.add(new TestPane()); 
       dialog.pack(); 
       dialog.setLocationRelativeTo(null); 
       dialog.setVisible(true); 

       System.out.println("Now back in the main..."); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JTextField field; 

     public TestPane() { 

      setLayout(new GridBagLayout()); 

      field = new JTextField(10); 
      field.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        if ("exitsys".equals(field.getText())) { 
         SwingUtilities.getWindowAncestor(field).dispose(); 
        } 
       } 
      }); 

      add(field); 

     } 

    } 

} 

更多細節

+0

謝謝!這花了我一些時間來理解,我通常把invokeLater放在main方法中,然後在Test()方法中創建我的組件和框架。一個簡單的問題是Test()考慮了一種方法或其他方法,因爲我不知道該怎麼稱呼它? – 2014-12-19 06:37:14

+1

'Test()'是一個構造函數,我儘可能快地擺脫了'main'的'static'限制,因爲它減少了與'static'和'non-static'有關的問題,但這只是我 – MadProgrammer 2014-12-19 06:39:10

+0

我不明白我的應用程序的對話框的目的,但我仍然認爲他們很酷。 – 2014-12-19 06:39:26