【问题标题】:How do I make my java program run in the background? [closed]如何让我的java程序在后台运行? [关闭]
【发布时间】:2012-12-23 01:09:24
【问题描述】:

我希望对程序进行编码以在后台运行,并在托盘中有一个图标。我该怎么做呢?操作系统是Windows,程序是JFrame。

【问题讨论】:

标签: java windows swing jframe


【解决方案1】:

你看,这里没有窗户,只有我们泰迪熊......

public class TestTrayIcon01 {

    public static void main(String[] args) {
        new TestTrayIcon01();
    }

    public TestTrayIcon01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TrayIcon icon = new TrayIcon(ImageIO.read(getClass().getResource("/SmallTeddy.png")));
                    SystemTray tray = SystemTray.getSystemTray();
                    tray.add(icon);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                JDialog dialog = new JDialog();
                dialog.setSize(100, 100);
                dialog.setVisible(true);
            }
        });
    }

}

JDialogJWindow(在 Windows 下)不会在任务栏上显示“图标”。我很确定在 Mac 下几乎不可能没有停靠图标...

【讨论】:

  • 非常感谢。有什么办法可以让它在关闭时在后台运行?
  • java.lang.NullPointerException。 :((当 JFrame 关闭时)
  • 异常消失了。但图标不是我想要的图标。 (我将图标图像设为 JFrame getIconImage)
  • 1.确保您已将图标替换为您自己的图标,它需要嵌入到应用程序中(在 Jar 中)。 2.不要将JFrame设置为自动关闭
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多