4

当数据库中的数据(股票名称和价格)与雅虎财经的数据(股票名称和价格)匹配时,我的程序会提醒用户。在HarryJoy的帮助下,我能够实现弹出通知。

现在的问题是,所有功能仅适用于最后一帧(YHOO)。5 秒后或当我单击 closeButton 时,它不会 dispose()。谢谢!

弹出窗口

    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();
    }
}
4

2 回答 2

5

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

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

为避免此类情况,您传递给外部代码的变量应该始终是final,或者在实例化外部进程时将对其的引用存储在外部代码中。

于 2012-05-10T09:27:02.803 回答
4

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

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

于 2012-05-10T09:39:19.320 回答