2012-03-13 66 views
1

我目前有一個按鈕,單擊它時,會執行一個方法,該方法使用加載多個圖像的面板創建一個jframe。如果多次點擊該按鈕,圖像將不斷添加到加載到jframe上的預先存在的圖像上。我應該使用哪些代碼,以便在jframe和元素加載後點擊按鈕,點擊一次後,將不會添加任何其他代碼。jframe actionperformed jframe

非常感謝

回答

0
  1. 不會產生大量的JFrames上運行,因爲這些對象從來沒有從Used JVM Memory直到目前JVM例如不見了存在

  2. 你看看CardLayout是非常confortly到用多個視圖解決你的問題(在這種情況下,在一個JFrame

  3. 把圖像作爲Icon/ImageIconJLabel

+0

它只是創建一個JFrame的,並與每個按鈕的點擊添加圖片到一個JFrame的 – user1060187 2012-03-13 18:50:01

+0

將圖片作爲jlabels在這裏不是問題 – user1060187 2012-03-13 18:51:58

+0

我在這裏談論錯誤的經歷,空白的過道和解決方案該怎麼辦......,一切都由你決定 – mKorbel 2012-03-13 19:02:59

2

什麼:

public void actionPerformed(ActionEvent evt) { 
    button.setEnabled(false); 

    // the other code that creates imgFrame 

    imgFrame.addWindowListener(new WindowAdapter() { 
     @Override public void windowClosing(WindowEvent evt) { 
      button.setEnabled(true); 
     }}); 

    imgFrame.setVisible(true); 
} 
+0

您只需要記住在其他JFrame關閉時重新啓用它。 – 2012-03-13 19:05:04

+0

@jschoen是的,你這樣做。我爲這個例子添加了一個方法。謝謝。 – 2012-03-14 09:23:01

0

禁用時,幀顯示的按鈕,當框架關閉,啓用按鈕。

public void actionPerformed(ActionEvent evt) { 

    final JButton finalButton = button; 
    button.setEnabled(false); 
    JFrame frame = new JFrame() 
    { 
     protected void processWindowEvent(WindowEvent e) 
     { 
      if (e.getID() == WindowEvent.WINDOW_CLOSING) 
      { 
       finalButton.setEnabled(true); 
      } 
      super.processWindowEvent(e); 
     } 
    }; 
    frame.setVisible(true); 
} 
0

我建議一個布爾值在程序啓動時爲false,然後當單擊該按鈕時,它會測試布爾值是否爲false。如果它是假的,那麼創建你想要的東西,然後使其成真。如果這是真的,什麼也不做,或提醒用戶不要點擊該按鈕多於一次,或該事項的東西

0
boolean isAlreadyCreated = false; 

yourButton.addActionListener(new ActionListener() 
{ 
    if(!isAlreadyCreated) 
    { 
     //CREATE YOUR NEW FRAME 
     isAlreadyCreated = true; 
    } 
}); 
+5

請解釋如何解決OP的問題。 – 2017-02-09 17:01:17

+0

對不起,我錯過了一行代碼 – 2017-02-10 07:59:43