1

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

问题是屏幕上的弹出位置基于数据库循环(int db)。即使数据不匹配,框架也会弹出。

仅当数据库数据=雅虎财经数据时,如何设置计数器或某种方法来弹出框架?此外,我的通知面板似乎位于主框架后面。如何使面板出现在我的主框架顶部?

我希望我已经提供了足够的信息,如果仍然不清楚,请询问。任何指导将不胜感激!谢谢!

在数据库中(使用代码示例仅按顺序显示我的解释,源代码如下)。

Object 1 = match
Object 2 = doesn't match (hence there's a gap)
Object 3 = match
Object 4 = match

截屏:

弹出窗口

代码:

 for (int db=0; db<= rowCount; db++) 
    {                   
        Object popSymbol = table.getModel().getValueAt(db, 0); 
        String popupSymbol = popSymbol.toString(); 

        Object popValue = table.getModel().getValueAt(db, 1);                          
        String str = popValue.toString(); 
        double popStockValue = Double.valueOf(str).doubleValue(); 

        String stockNameDB = popupSymbol;       
        StockPara stock = YahooStock.getInstance().getStockPrice(stockNameDB);
        double stockPriceDB = Math.round(stock.getPrice()); 

        final JFrame popUpFrame;
        final JPanel popupPanel;                    

        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(null);    

            popupPanel = new JPanel();
            popupPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
            popupPanel.setBounds(0, 0, 320, 90);
            getContentPane().add(popupPanel);
            popupPanel.setBackground(new Color(255, 255, 224));
            popupPanel.setLayout(null);
            popUpFrame.add(popupPanel);                 

            // Other labels, images, etc. 

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

        } 
    }
4

2 回答 2

1

要快速解决您的问题,只需创建一个额外的计数器 ( int counter = 0;)。然后使用该计数器而不是db定位您的 popUpFrame 并在该增量计数器之后。

int counter = 0; // Initiate outside de loop
...
for(int db=0; db<= rowCount; db++) {
    ...
    if (stockPriceDB == popStockValue) {
         ...
         popUpFrame.setLocation(screenSize.width - popUpFrame.getWidth(), 
            screenSize.height - toolHeight.bottom - (popUpFrame.getHeight() * (counter+1)));
         counter++;
    }
}

要将您的框架放在前面,请使用:

popUpFrame.toFront()

并强制它始终位于顶部(尽管我真的不建议这样做,因为它对用户来说比其他任何事情都更烦人):

popUpFrame.setAlwaysOnTop(true);

这不一定是所有平台都支持的。

于 2012-05-10T20:57:57.643 回答
0

对于最重要的问题,使用

    notificationPanel.requestFocus();
于 2012-05-10T21:12:38.390 回答