【问题标题】:How to display frame only when required & set frame to appear on top all the time?如何仅在需要时显示框架并将框架设置为始终显示在顶部?
【发布时间】:2012-05-10 19:00:29
【问题描述】:

当数据库中的数据(股票名称和价格)与雅虎财经的数据(股票名称和价格)匹配时,我的程序会提醒用户。在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)));

        } 
    }

【问题讨论】:

    标签: java swing loops frame toolkit


    【解决方案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);

    不一定所有平台都支持。

    【讨论】:

      【解决方案2】:

      对于最重要的问题,使用

          notificationPanel.requestFocus();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        相关资源
        最近更新 更多