简体   繁体   English

如何使用Java在Windows中设置弹出窗口?

[英]How to set pop-up in windows using java?

I have created an application using Swing. 我已经使用Swing创建了一个应用程序。 I am trying to create a pop-up notification(not with JOptionPane) with JWindow so that it looks like a windows pop-up notification, and I made that with no error. 我正在尝试使用JWindow创建弹出通知(不使用JOptionPane),使其看起来像Windows弹出通知,并且我做到了没有错误。

The thing is when I link this window to my mainFrame(JFrame) (a mainframe for other purposes and contains the code which will cause this pop-up window to fire), the pop-up notification window looses its position and position become relevant to the mainFrame. 问题是,当我将此窗口链接到我的mainFrame(JFrame)(用于其他目的的大型机,并且包含将导致该弹出窗口触发的代码)时,弹出通知窗口将松开其位置,并且位置与mainFrame。

How can I set it to not become relevant to the mainFrame and its postion remain independent ? 如何设置它与mainFrame不相关并且其位置保持独立?

Below is the code for pop-up window. 以下是弹出窗口的代码。

popWindow= new JWindow();
popWindow.setLayout(null);

Toolkit tool = Toolkit.getDefaultToolkit();
Dimension dim= tool.getScreenSize();

int xPos= dim.width - (WIDTH+60);
int yPos= dim.height - (HEIGHT+60);

popWindow.setBounds(xPos, yPos, HEIGHT, WIDTH);

msgLabel= new JLabel("PopUp window");
msgLabel.setBounds(10,0,200,100);

buttonIcon = new ImageIcon("C:\\Users\\tt\\Desktop\\close_icon.png");
closeButton = new JButton(buttonIcon);
closeButton.setBounds(275,0,25,25);
closeButton.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent ae){
       popWindow.dispose();
   } 
});

popWindow.add(closeButton);
popWindow.add(msgLabel);

popWindow.setVisible(true); 

I have saved this code in other java file and use its constructor to link this to my mainFrame. 我已将此代码保存在其他Java文件中,并使用其构造函数将其链接到我的mainFrame。 When I run it alone, the positioning is perfect. 当我独自运行时,定位是完美的。

//Check the SystemTray is supported if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); //如果(!SystemTray.isSupported()){System.out.println(“不支持SystemTray”); //检查是否支持SystemTray。 return; 返回; } final PopupMenu popup = new PopupMenu(); 最终的PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon")); 最后的TrayIcon trayIcon =新的TrayIcon(createImage(“ images / bulb.gif”,“托盘图标”))); final SystemTray tray = SystemTray.getSystemTray(); 最终的SystemTray托盘= SystemTray.getSystemTray();

    // Create a pop-up menu components
    MenuItem aboutItem = new MenuItem("About");
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    Menu displayMenu = new Menu("Display");
    MenuItem errorItem = new MenuItem("Error");
    MenuItem warningItem = new MenuItem("Warning");
    MenuItem infoItem = new MenuItem("Info");
    MenuItem noneItem = new MenuItem("None");
    MenuItem exitItem = new MenuItem("Exit");

    //Add components to pop-up menu
    popup.add(aboutItem);
    popup.addSeparator();
    popup.add(cb1);
    popup.add(cb2);
    popup.addSeparator();
    popup.add(displayMenu);
    displayMenu.add(errorItem);
    displayMenu.add(warningItem);
    displayMenu.add(infoItem);
    displayMenu.add(noneItem);
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM