2012-05-10 19 views
4

當數據庫中的數據(股票名稱&價格)與Yahoo Finance的數據(股票名稱和價格)匹配時,我的程序將提醒用戶。在HarryJoy的幫助下,我可以實現彈出式通知。Dispose()不適用於每一幀

現在的問題是,所有的功能只適用於最後一幀(YHOO)。它不會在5秒後處理(),或者我點擊closeButton。謝謝!

PopUp

if (stockPriceDB == popStockValue) 
    {      

     String header = "Stock: " + stock.getTicker() + " is now @ " + stock.getPrice();   
     String message = ""; 

     popUpFrame = new JFrame(); 
     popUpFrame.setSize(320,90); 
     popUpFrame.setUndecorated(true);          
     popUpFrame.getContentPane().setLayout(new GridBagLayout()); 

     GridBagConstraints constraints = new GridBagConstraints(); 
     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.weightx = 1.0f; 
     constraints.weighty = 1.0f; 
     constraints.insets = new Insets(5, 5, 5, 5); 
     constraints.fill = GridBagConstraints.BOTH; 
     JLabel headingLabel = new JLabel(header); 

     ImageIcon headingIcon = new ImageIcon("images/alert.gif");   
     headingLabel.setIcon(headingIcon);   

     popUpFrame.getContentPane().add(headingLabel, constraints); 
     constraints.gridx++; 
     constraints.weightx = 0f; 
     constraints.weighty = 0f; 
     constraints.fill = GridBagConstraints.NONE; 
     constraints.anchor = GridBagConstraints.NORTH;       

     closeButton = new JButton(); 

     closeButton = new JButton(new AbstractAction("x") 
     { 
     private static final long serialVersionUID = 1L; 

     public void actionPerformed(final ActionEvent e) 
      { 
       popUpFrame.dispose(); 
      } 
     }); 

     closeButton.setMargin(new Insets(1, 4, 1, 4)); 
     closeButton.setFocusable(false); 
     popUpFrame.getContentPane().add(closeButton, constraints); 
     constraints.gridx = 0; 
     constraints.gridy++; 
     constraints.weightx = 1.0f; 
     constraints.weighty = 1.0f; 
     constraints.insets = new Insets(5, 5, 5, 5); 
     constraints.fill = GridBagConstraints.BOTH;     

     JLabel messageLabel = new JLabel(message); 
     popUpFrame.getContentPane().add(messageLabel, constraints); 
     popUpFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     popUpFrame.setVisible(true); 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(popUpFrame.getGraphicsConfiguration()); 
     popUpFrame.setLocation(screenSize.width - popUpFrame.getWidth(), screenSize.height - toolHeight.bottom - (popUpFrame.getHeight() * (x+1))); 

     new Thread() 
     {       
     public void run() 
      { 
       try 
       { 
        Thread.sleep(5000); 
        popUpFrame.dispose(); 
       } 
       catch (InterruptedException e) 
       { 
        e.printStackTrace(); 
       } 
      }; 

     }.start(); 
    } 
} 

回答

5

我懷疑,你所得到的問題是要傳遞到外部代碼(在不同的線程在這種情況下),的popUpFrame變量隨後再爲其分配新對象的每循環的實例。

此方法容易出錯,因爲您丟失了您通過的參考。因爲每次創建新對象時都會覆蓋它。 因此,我認爲你可能只能接近最新的一個。

爲了避免這樣的事情傳遞給外部代碼的變量應該總是final或實例化一個外部程序商店的外部代碼中對它的引用時。

+0

感謝Boro!我已將變量更改爲final,現在工作正常! :) –

+0

popUpFrame.dispose();應該被包裹到invokeLater中,因爲被稱爲put EDT,用於休息+1 – mKorbel

+0

非常好聽。我認爲是這樣:)無論如何,如果您只是使用單個線程來更新所有可見框架,則更好的方法是。這樣你就可以避免爲每一幀創建一個單獨的線程。更新線程可以有一個可見的框架列表,這些框架可以有其到期時間,並且如果它們過期或沒有過期,它將每30毫秒檢查一次,並相應地採取行動。這樣你總是有2個線程。你也可以稍後重用更新線程,如果你喜歡,動畫等。 – Boro

4

正如Boro所注意到的,您正在爲所有幀重複使用相同的popupFrame變量。當然,它只能存儲一個框架,即您創建的最後一個框架。所有其他人都失去了。所以當你調用popupFrame.dispose()時,你實際上獨立於你所按下的「X」按鈕來處理最後創建的JFrame

但是我認爲製作這麼多幀的選擇實際上不是一個好主意。您應該有一個包含一組JPanel的JFrame,您將在5秒後或按下'X'按鈕時將其刪除。

+0

Guillaume Polet,感謝您的建議和詳細的解釋,我現在明白:) –

+0

+1好用,重新使用一個框架,並相應地調整大小。 – Boro

+0

有沒有辦法去創建不會共享相同名稱的新框架,在這種情況下?是的,我同意jpanels可能是你最好的選擇,但是當試圖移除面板時,同名問題仍然存在嗎? –

相關問題