java 如何通过java中的按钮最小化、最大化和还原?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16591438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to minimize, maximize and restore down through buttons in java?
提问by Pratik Anand
I'm creating a JavaFX 2.2 program, and need to create custom UI controls (just those ever-present minimize-maximize/restore-close buttons on the top). I need to create custom buttons for that purpose, simple till just creating.
我正在创建一个 JavaFX 2.2 程序,并且需要创建自定义 UI 控件(只是顶部那些一直存在的最小化-最大化/恢复-关闭按钮)。我需要为此目的创建自定义按钮,简单到刚刚创建。
I just need the real code for minimize and the maximize/restore buttons (close button was fairly a child's play). The minimize button restores the app to the taskbar. The maximize button, well, maximizes it to fit the user's screen and it toggles to restore button while maximized. When the restored button is clicked, the window gets restored to its initial size (1200x600).
我只需要最小化和最大化/恢复按钮的真实代码(关闭按钮就像孩子的游戏)。最小化按钮将应用程序恢复到任务栏。最大化按钮,嗯,最大化它以适应用户的屏幕,并在最大化时切换到恢复按钮。单击恢复的按钮后,窗口将恢复为其初始大小 (1200x600)。
I tried stage.setSize(width, height);
in my fxml file for restore down, but it doesn't work (stage
is highlited as the error, while the stage name is stage.)
I used
我尝试stage.setSize(width, height);
在我的 fxml 文件中进行还原,但它不起作用(stage
被高亮显示为错误,而舞台名称是舞台。)
我用过
scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);
to set the default window as transparent.
将默认窗口设置为透明。
The program(main file, not the controller) is:
程序(主文件,不是控制器)是:
Parent root = FXMLLoader.load(getClass().getResource("fxmlfile.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setTitle("Nothing here");
stage.setScene(scene);
stage.show();
The controller file with close action is:
具有关闭操作的控制器文件是:
@FXML
private void exitProgramAction(ActionEvent exitProgramEvent) {
System.out.println("Killing program...");
System.exit(0);
}
回答by Huber
To minimize the stage into task bar set an action for your minimize button similar to this:
要将舞台最小化到任务栏,请为最小化按钮设置一个类似于以下的操作:
btnMinimize.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Stage stage = (Stage)((Button)event.getSource()).getScene().getWindow();
// is stage minimizable into task bar. (true | false)
stage.setIconified(true);
}
});
I use JavaFX 8 therefore I don't know if it will work with JavaFX 2.2
我使用 JavaFX 8 因此我不知道它是否适用于 JavaFX 2.2
回答by PhilippeVienne
You have to call your stage object in your main class. This only could be the stage object which could manipulate the Window.
您必须在主类中调用舞台对象。这只能是可以操作窗口的舞台对象。
See : http://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html
请参阅:http: //docs.oracle.com/javafx/2/api/javafx/stage/Stage.html
回答by SimplyMe
code for minimize
最小化代码
@FXML private void minimizeBut()
{
Stage stage=(Stage) minimize.getScene().getWindow();
stage.setIconified(true);
}
回答by Cesargv
In JavaFX8
在 JavaFX8 中
@Override
public void start(Stage stage) {
//Maximized
stage.setMaximized(true);
//Restore down
stage.setMaximized(false);
}