Dispose()不适用于每一帧

时间:2012-05-10 09:18:43

标签: java swing notifications popup dispose

当数据库中的数据(股票名称和价格)与Yahoo Finance的数据(股票名称和价格)匹配时,我的程序将提醒用户。在HarryJoy的帮助下,我可以实现弹出通知。

现在的问题是,所有功能仅适用于最后一帧(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();
    }
}

2 个答案:

答案 0 :(得分:5)

我怀疑你得到的问题是你正在传递一个外部代码(在这种情况下是一个不同的线程),一个popUpFrame的变量,然后你可以为每个循环实例分配新对象。

此方法容易出错,因为您丢失了传递的引用。因为每次创建新对象时都会覆盖它。 因此,我认为你可能只能接近最新的一个。

要避免这样的事情,传递给外部代码的变量应始终为final,或者在实例化外部进程时,在外部代码中存储对它的引用。

答案 1 :(得分:4)

正如Boro所注意到的,您正在为所有帧重复使用相同的popupFrame变量。当然它只能存储一个帧,也就是你创建的最后一帧。所有其他人都丢失了。所以当你调用popupFrame.dispose()时,你实际上是独立于你按下的“X”按钮处理最后创建的JFrame

但我认为制作这么多帧的选择实际上并不是一个好主意。您应该使用一个JFrame,其中包含一组JPanel,您将在5秒后或按下“X”按钮时将其删除。

相关问题