如何使java程序在后台运行?

如何使java程序在后台运行?,java,windows,swing,jframe,Java,Windows,Swing,Jframe,我希望该程序被编码为在后台运行,并在托盘中有一个图标。我该怎么做?操作系统是Windows,程序是JFrame。瞧,这里没有Windows,只有我们泰迪熊 public class TestTrayIcon01 { public static void main(String[] args) { new TestTrayIcon01(); } public TestTrayIcon01() { EventQueue.invokeLate

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

瞧,这里没有Windows,只有我们泰迪熊

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);
            }
        });
    }

}


JDialog
JWindow
(在Windows下)不会在任务栏上显示“图标”。我敢肯定,在Mac电脑下,几乎不可能没有对接图标……

Mac、Windows或Linux?它需要一个用户界面吗?你应该花更多的精力学习如何提问。这是一个非常不完整的问题。请阅读帮助您改进您的问题,以免它被关闭。我相信您正在寻找
JFrame.setVisible(布尔)的组合
系统托盘
()非常感谢。有什么方法可以让它在关闭时在后台运行吗?java.lang.NullPointerException.:((当JFrame关闭时)异常消失。但图标不是我想要的图标。(我将图标图像设置为JFrame getIconImage)1.确保已将图标替换为自己的图标,它需要嵌入应用程序(在Jar内)。2.不要将
JFrame
设置为自动关闭