2012-03-25 44 views
17

我正在製作JavaFX destop應用程序。我想刪除默認的窗口邊框,我也想自定義最小化,最大化和關閉的3個標準圖標。JavaFX primaryStage刪除窗口邊框?

這種長相或定製的原始動機是新的卡巴斯基2012的用戶界面....我想設計類似的東西... :)

+1

當問題實際上是原始問題時,將問題標記爲重複是多麼的不合邏輯。 – Haggra 2016-09-22 06:41:22

+0

如果你確實設計了你自己的窗口,請保守一點,並且試圖堅持每個本地平臺的設計。這很容易搞砸,使它看起來像一個便宜的噱頭。 – RecursiveExceptionException 2016-12-02 18:26:20

回答

29

這個例子可能是一個很好的起點。所有窗戶裝飾被刪除。延伸HBox的類可用於爲標準窗口操作放置自定義按鈕。

package javafxdemo; 

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ToolBar; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

public class JavaDemo extends Application { 

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

    class WindowButtons extends HBox { 

     public WindowButtons() { 
      Button closeBtn = new Button("X"); 

      closeBtn.setOnAction(new EventHandler<ActionEvent>() { 

       @Override 
       public void handle(ActionEvent actionEvent) { 
        Platform.exit(); 
       } 
      }); 

      this.getChildren().add(closeBtn); 
     } 
    } 

    @Override 
    public void start(Stage primaryStage) { 
     //remove window decoration 
     primaryStage.initStyle(StageStyle.UNDECORATED); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setStyle("-fx-background-color: green;"); 

     ToolBar toolBar = new ToolBar(); 

     int height = 25; 
     toolBar.setPrefHeight(height); 
     toolBar.setMinHeight(height); 
     toolBar.setMaxHeight(height); 
     toolBar.getItems().add(new WindowButtons()); 

     borderPane.setTop(toolBar); 

     primaryStage.setScene(new Scene(borderPane, 300, 250)); 
     primaryStage.show(); 
    } 
} 

您還可以下載JavaFX Samples,在那裏您可以找到許多更有用的示例。

+0

感謝上面的例子。 – dhroove 2012-03-26 04:33:56

+1

很好的答案。 +1!但是如果一個人想要仍然能夠在屏幕上移動窗口呢? – 735Tesla 2014-02-14 18:33:14

+2

沒關係,我在這裏找到答案:http://stackoverflow.com/questions/11780115/moving-an-undecorated-stage-in-javafx-2 – 735Tesla 2014-02-14 18:35:33