Creating tray icon using JavaFX

后端 未结 5 585
南方客
南方客 2021-02-07 15:48

I want to write a tray icon via JavaFx, but I only find that can write by awt. Is there any way that can write it use JavaFx?

It w

5条回答
  •  长发绾君心
    2021-02-07 16:43

    i made it. JavaFX with AWT. I have one example of a application that shows and hides when you make left clic. i really hope works for you

    import java.awt.AWTException;
    import java.awt.Image;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.URL;
    import javafx.application.Application;
    import static javafx.application.Application.launch;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class MainApp2 extends Application {
    
    int stateWindow = 1;
    
    @Override
    public void start(final Stage stage) throws Exception {
    
        //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
    
        URL url = System.class.getResource("/image/yourImage.png");
        Image image = Toolkit.getDefaultToolkit().getImage(url);
    
        //image dimensions must be 16x16 on windows, works for me
        final TrayIcon trayIcon = new TrayIcon(image, "application name");
    
        final SystemTray tray = SystemTray.getSystemTray();
    
        //Listener left clic XD
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                if (event.getButton() == MouseEvent.BUTTON1) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            if (stateWindow == 1) {
                                stage.hide();
                                stateWindow = 0;
                            } else if (stateWindow == 0) {
                                stage.show();
                                stateWindow = 1;
                            }
                        }
                    });
                }
    
            }
        });
    
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
        }
    
        stage.setTitle("Hello man!");
        Button btn = new Button();
        btn.setText("Say 'Hello man'");
        btn.setOnAction(new EventHandler() {
    
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello man!");
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        stage.setScene(new Scene(root, 300, 250));
        Platform.setImplicitExit(false);
        stage.show();
    
    }
    
    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    }
    

提交回复
热议问题