當數據庫中的數據(股票名稱&價格)與Yahoo Finance的數據(股票名稱和價格)匹配時,我的程序將提醒用戶。在HarryJoy的幫助下,我可以實現彈出式通知。Dispose()不適用於每一幀
現在的問題是,所有的功能只適用於最後一幀(YHOO)。它不會在5秒後處理(),或者我點擊closeButton。謝謝!
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();
}
}
感謝Boro!我已將變量更改爲final,現在工作正常! :) –
popUpFrame.dispose();應該被包裹到invokeLater中,因爲被稱爲put EDT,用於休息+1 – mKorbel
非常好聽。我認爲是這樣:)無論如何,如果您只是使用單個線程來更新所有可見框架,則更好的方法是。這樣你就可以避免爲每一幀創建一個單獨的線程。更新線程可以有一個可見的框架列表,這些框架可以有其到期時間,並且如果它們過期或沒有過期,它將每30毫秒檢查一次,並相應地採取行動。這樣你總是有2個線程。你也可以稍後重用更新線程,如果你喜歡,動畫等。 – Boro