2014-05-17 34 views
-2

如何在按下JButton後停止程序生成JFrame?我試圖讓它在第一次按下按鈕後停止生成新的JFrames,但可悲的是,這沒有發生。任何幫助將不勝感激。謝謝。(Java)不管你按了多少次,你如何讓一個程序只創建一個框架?

list.addActionListener(new ActionListener() { 
    public void actionPerformed (ActionEvent e) { 
     for (int counter=0; counter<1; counter++){ 
        if (counter<1){ 
         Newbox fresh = new Newbox(); 
          } 
           if (counter >1) { 
           break; 
        } 
       } 
      } 
     }); 

回答

1

您可以使用布爾變量來阻止它創建一個新的框架。像

boolean pressed = false; 
list.addActionListener(new ActionListener() { 
public void actionPerformed (ActionEvent e) { 
    for (int counter=0; counter<1; counter++){ 
       if (counter<1 && pressed == false){ 
        Newbox fresh = new Newbox(); 
        pressed = true; 
         } 
          if (counter >1) { 
          break; 
       } 
      } 
     } 
    }); 
+0

我認爲for循環可以被刪除,因爲它只會運行一次,我認爲OP試圖解決他這樣的問題。 – Kimmax

0

Trx添加布爾值。

Boolean pressed = false; 

list.addActionListener(new ActionListener() { 
    public void actionPerformed (ActionEvent e) { 
     if (!pressed){ 
      Newbox fresh = new Newbox(); 
      pressed = true; 
     } 
    } 
}); 
+0

Whups。有人可以添加代碼塊嗎?沒有從移動工作.. – Kimmax

+0

注:布爾必須_outside_方法,一個全局變量。 – Kimmax

0

我試過使用布爾值,但這種方法結束了更好的工作,所以記錄,這是解決這個問題的一種方法。謝謝大家的反饋意見。

  list.addActionListener(new ActionListener() { 
        public void actionPerformed (ActionEvent e) { 
         Newbox fresh = new Newbox(); 
          ((AbstractButton)e.getSource()).setEnabled(false); 

        } 
       }); 
相關問題