웹 사이트 용 클라이언트 응용 프로그램을 개발하고 싶습니다.시스템 트레이에 JavaFx fxml 또는 JavaFx 스윙 응용 프로그램을 숨기려면
최소화 할 때 시스템 트레이에 앱을 저장하고 싶습니다.
이 작업을 수행하는 방법은 알지 못합니다.
이러한 유형의 조작에 대한 예제입니다.
웹 사이트 용 클라이언트 응용 프로그램을 개발하고 싶습니다.시스템 트레이에 JavaFx fxml 또는 JavaFx 스윙 응용 프로그램을 숨기려면
최소화 할 때 시스템 트레이에 앱을 저장하고 싶습니다.
이 작업을 수행하는 방법은 알지 못합니다.
이러한 유형의 조작에 대한 예제입니다.
여기서 핵심은 암시 적 종료를 거짓으로 설정하는 것입니다. Platform.setImplicitExit(false);
새 스레드에서 스테이지를 표시하거나 숨기는 것도 중요합니다.
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.show();
}
});
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.hide();
}
});
다음으로, 전체 코드 :
import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.imageio.ImageIO;
/**
*
* @author alvaro
*/
public class TrayTest extends Application {
private boolean firstTime;
private TrayIcon trayIcon;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
createTrayIcon(stage);
firstTime = true;
Platform.setImplicitExit(false);
Scene scene = new Scene(new Group(), 800, 600);
stage.setScene(scene);
stage.show();
}
public void createTrayIcon(final Stage stage) {
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// load an image
java.awt.Image image = null;
try {
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
image = ImageIO.read(url);
} catch (IOException ex) {
System.out.println(ex);
}
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
hide(stage);
}
});
// create a action listener to listen for default action executed on the tray icon
final ActionListener closeListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
};
ActionListener showListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.show();
}
});
}
};
// create a popup menu
PopupMenu popup = new PopupMenu();
MenuItem showItem = new MenuItem("Show");
showItem.addActionListener(showListener);
popup.add(showItem);
MenuItem closeItem = new MenuItem("Close");
closeItem.addActionListener(closeListener);
popup.add(closeItem);
/// ... add other items
// construct a TrayIcon
trayIcon = new TrayIcon(image, "Title", popup);
// set the TrayIcon properties
trayIcon.addActionListener(showListener);
// ...
// add the tray image
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
// ...
}
}
public void showProgramIsMinimizedMsg() {
if (firstTime) {
trayIcon.displayMessage("Some message.",
"Some other message.",
TrayIcon.MessageType.INFO);
firstTime = false;
}
}
private void hide(final Stage stage) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (SystemTray.isSupported()) {
stage.hide();
showProgramIsMinimizedMsg();
} else {
System.exit(0);
}
}
});
}
}
내가 아는 한 JFX 8에서 가능합니다. 지금 가장 좋은 해결책은 AWT에 응용 프로그램을 포함시키고 AWT 창을 숨기는 것입니다.
당신이 나에게 이것의 작동 예를 줄 수 당신이 설명하는 타입을 ... –
@ alscu의 대답은 당신이 설명했던 것입니다. 감사합니다. –
JavaFx 8이 이미 있습니다. AWT없이 systray를 사용할 수 있습니까? JavaFx는 훨씬 멋지게 보입니다. – qed
정답은 [여기] (http://stackoverflow.com/a/12571924/1624376) – Aubin