【问题标题】:What technology to use to write systray front-end to webapp?使用什么技术将系统托盘前端写入 webapp?
【发布时间】:2009-06-16 18:44:17
【问题描述】:

我们有一个内部 web 应用程序在 tomcat 上运行,构建在 Spring 之上。 Web 应用程序前端是使用 Flex 构建的。
我想创建一个跨平台的系统托盘应用程序,它允许进入应用程序的主页并在服务器中发生某些事情时显示警报。

您认为哪种技术最适合:

  • 系统托盘本身? Java Swing?
  • 服务器和系统托盘之间的通信?网络服务? RSS订阅?春季远程? JMX 通知?

问候,

维姆

【问题讨论】:

  • JRE 的目标版本是什么? v.6 具有广泛的系统托盘支持。我会让系统托盘应用程序通过抓取来轮询状态页面。 (因为它很简单而且可以工作):D

标签: java apache-flex swing spring


【解决方案1】:

如果您想继续使用 Java,您有两种选择:

  • 使用 Swing/AWT。确保您使用的是 Java 6 及更高版本(您可以将其与您的应用程序一起安装),因为它支持系统托盘(来自 API):

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        // get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();
        // load an image
        Image image = Toolkit.getDefaultToolkit.getImage("");
        // create a action listener to listen for default action executed on
        // the tray icon
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // execute default action of the application
                // ...
            }
        };
        // create a popup menu
        PopupMenu popup = new PopupMenu();
        // create menu item for the default action
        MenuItem defaultItem = new MenuItem("");
        defaultItem.addActionListener(listener);
        popup.add(defaultItem);
        // / ... add other items
        // construct a TrayIcon
        trayIcon = new TrayIcon(image, "Tray Demo", popup);
        // set the TrayIcon properties
        trayIcon.addActionListener(listener);
        // ...
        // add the tray image
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        // ...
    }
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        trayIcon.setImage(updatedImage);
    }
    // ...
    
  • 使用SWT/JFace。这是一个例子(取自here):

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        Image image = new Image(display, 16, 16);
        final Tray tray = display.getSystemTray();
        if (tray == null) {
            System.out.println("The system tray is not available");
        } else {
            final TrayItem item = new TrayItem(tray, SWT.NONE);
            item.setToolTipText("SWT TrayItem");
            item.addListener(SWT.Show, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("show");
                }
            });
            item.addListener(SWT.Hide, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("hide");
                }
            });
            item.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("selection");
                }
            });
            item.addListener(SWT.DefaultSelection, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("default selection");
                }
            });
            final Menu menu = new Menu(shell, SWT.POP_UP);
            for (int i = 0; i < 8; i++) {
                MenuItem mi = new MenuItem(menu, SWT.PUSH);
                mi.setText("Item" + i);
                mi.addListener(SWT.Selection, new Listener() {
                    public void handleEvent(Event event) {
                        System.out.println("selection " + event.widget);
                    }
                });
                if (i == 0)
                    menu.setDefaultItem(mi);
            }
            item.addListener(SWT.MenuDetect, new Listener() {
                public void handleEvent(Event event) {
                    menu.setVisible(true);
                }
            });
            item.setImage(image);
        }
        shell.setBounds(50, 50, 300, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        image.dispose();
        display.dispose();
    }
    

【讨论】:

  • 我有丰富的 Swing 经验,但没有 SWT 经验。为此转向 SWT 是否有意义?
  • 从技术角度来看,没有。据我了解,您只需要一个用于服务器的小型监控应用程序。我不相信仅仅为此而学习新技术会是你时间的最佳利用(除非你想把它用作一个教育项目)。
【解决方案2】:

使用 Adob​​e AIR 和 BlazeDS 或 LCDS,您可以轻松构建此类应用程序。

【讨论】:

  • 是的,但是我忘记了一个重要的要求。系统托盘还应该能够启动任意应用程序。我了解到由于沙盒限制,您无法从 AIR 启动应用程序。
  • 也许 Merapi 之类的东西会对此有所帮助?
  • 谢谢你,看起来很有趣。如果人们想知道,这是链接:merapiproject.net
【解决方案3】:

我会选择 FreePascal。它本机编译到 windows / mac / linux,因此您不依赖任何其他要安装的框架(.net、java、air)。只需一个可执行文件即可。

【讨论】:

  • 好吧,“您想要支持的每个平台只有一个可执行文件”并且可能只有一组源代码以及系统托盘图标的原生 API 都不是标准化的。
【解决方案4】:

我同意 James 的观点:如果您对 Flex 有投资和专业知识,那么使用 Air 扩展它是有意义的。

至于有效负载 - 如果您只是需要不时“弹出”通知,RSS 是要走的路。否则,使用您自己的类似 XML REST 的服务,因为它易于设置,并且从长远来看会给您带来灵活性。

【讨论】:

  • 感谢您的意见。我希望在服务器和系统托盘部分之间的通信上得到更多答案
  • 对我的回答有任何帮助吗?有关您的需求的其他信息会有所帮助。
  • 是的,您所说的类似 XML REST 的服务是什么意思? Spring 或任何其他库中是否有任何东西可以帮助解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 2017-12-05
  • 2012-04-21
  • 1970-01-01
  • 2013-10-16
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多