2016-09-23 39 views
1

我在JavaFX中制作了一个GUI,并且我剥离了Windows所做的装饰窗口,并且我已经为closeminimizemaximizeJavaFX按钮在按下邻居后会以一个像素增长

这些控件工作得很好,但是当我按下其中一个时,它的邻居会改变垂直尺寸,增加一个或两个像素,就像您在此图像中看到的那样。

enter image description here

这究竟是为什么? 我也附上我目前在start()方法中的代码。

public void start(Stage primaryStage) throws Exception { 
    window = primaryStage; 
    window.setTitle("Menu Example"); 

    MenuBar file = new MenuBar(); 
    file.setId("file"); 

    Menu fileMenu = new Menu("File"); 
    fileMenu.getItems().addAll(
      new MenuItem("New File..."), 
      new MenuItem("Open file..."), 
      new MenuItem("Save file")); 
    fileMenu.setId("#fileMenu"); 

    Menu editMenu = new Menu("Edit"); 
    editMenu.getItems().addAll(
      new MenuItem("Undo"), 
      new MenuItem("Redo"), 
      new MenuItem("Cut"), 
      new MenuItem("Copy"), 
      new MenuItem("Paste") 
    ); 

    Button closeButton = new Button("X"); 
    closeButton.setId("closeButton"); 
    closeButton.setOnAction(event -> { 
     window.close(); 
    }); 

    Button minimizeButton = new Button("_"); 
    minimizeButton.setId("minimizeButton"); 
    minimizeButton.setOnAction(event -> { 
     window.setIconified(true); 
    }); 

    Button maximizeButton = new Button("?"); 
    maximizeButton.setId("maximizeButton"); 
    maximizeButton.setOnAction(event -> { 
     if(!window.isMaximized()) 
      window.setMaximized(true); 
     else 
      window.setMaximized(false); 
    }); 

    file.getMenus().addAll(
      fileMenu, 
      editMenu 
    ); 

    HBox.setHgrow(file, Priority.ALWAYS); 
    HBox.setHgrow(closeButton, Priority.NEVER); 
    HBox.setHgrow(minimizeButton, Priority.NEVER); 
    HBox.setHgrow(maximizeButton, Priority.NEVER); 
    VBox.setVgrow(closeButton, Priority.NEVER); 

    hBox = new HBox(); 
    buttonBox = new HBox(); 
    buttonBox.getChildren().addAll(minimizeButton, maximizeButton, closeButton); 
    hBox.getChildren().addAll(file, buttonBox); 

    layout = new BorderPane(); 
    layout.setTop(hBox); 

    Scene scene = new Scene(layout, 400, 400); 
    scene.getStylesheets().add("Viper.css"); 
    window.setScene(scene); 
//  window.setMaximized(true); 
    window.initStyle(StageStyle.UNDECORATED); 
    window.show(); 
} 

这是我的样式表:

.root{ 
    -fx-background-color: #2D2E32; 
    -fx-font-size: 11px; 
} 

#file{ 
    -fx-background-color: #3E3F43; 
} 

#file .label{ 
    -fx-text-fill: #EAEAEA; 
} 

.context-menu{ 
    -fx-background-color: #3E3F43; 

} 

#closeButton, #minimizeButton, #maximizeButton{ 
    -fx-background-radius: 0; 
    -fx-background-color: #3E3F43; 
    -fx-text-fill: #ffffff; 
    -fx-font-weight: bold; 
} 

#closeButton:hover{ 
    -fx-background-color: #E46458; 
} 

#minimizeButton:hover{ 
    -fx-background-color: #80B1E0; 
} 

#maximizeButton:hover{ 
    -fx-background-color: #80E089; 
} 
+0

发布您的样式表将非常有帮助。 – DVarga

+0

完成,尽管我认为样式表没有任何影响。 – Justplayit94

回答

3

根据您的CSS样式表,把下面的选择会解决问题:

#closeButton:armed, #closeButton:focused, 
#minimizeButton:armed, #minimizeButton:focused, 
#maximizeButton:armed, #maximizeButton:focused { 
    -fx-background-insets: 0 0 -1 0, 0, 1, 2; 
} 

原因:

默认样式表modena.css定义了backgro Button-fx-background-insets: 0 0 -1 0, 0, 1, 2;,但如果Button处于focusedarmed的伪状态中的一个,则将插入修改为-fx-background-insets: -0.2, 1, 2, -1.4, 2.6;

所以实际上不会增加​​邻居的垂直尺寸,但是armed或的“垂直尺寸”将会减小。

这是上述选择器阻止的内容。

注意:如果您创建一个新的CSS类像

.windowbutton:armed, .windowbutton:focused { 
    -fx-background-insets: 0 0 -1 0, 0, 1, 2; 
} 

然后在你的代码这个类分配给每个Button

closeButton.getStyleClass().add("windowbutton"); // Similar to other buttons 

的CSS结构将是一点点,但更干净。

+0

其实,我想要的是'-fx-background-insets:0 0 -1 0,0,1,2;',但你的答案是解决我的问题。 – Justplayit94