2014-01-25 46 views
0

我正在處理此代碼,它是最小化按鈕的JavaFX面板。然後,我想按下按鈕縮小面板的大小,然後再按按鈕恢復原始大小。更改大小的按鈕

public class MainApp extends Application 
{ 

    private static BorderPane bp; 

    @Override 
    public void start(Stage stage) throws Exception 
    { 

     FlowPane flow = new FlowPane(); 
     flow.setPadding(new Insets(50, 5, 5, 5)); 
     flow.setVgap(15); 
     flow.setHgap(15); 
     flow.setAlignment(Pos.CENTER); 

     Button button = new Button("Minimize"); 

     HBox thb = new HBox(); 
     thb.getChildren().add(button); 
     thb.setAlignment(Pos.CENTER_RIGHT); 

     boolean flag = true; 


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

      @Override 
      public void handle(ActionEvent t) 
      { 
       if (flag == true) 
       { 
        flag = false; 
        bp.setPrefSize(600, 40); 
        bp.setMaxSize(600, 40); 

       } 
       else 
       { 
        bp.setPrefSize(600, 500); 
        bp.setMaxSize(600, 500); 
       } 

      } 
     }); 






     thb.setPadding(new Insets(13, 13, 13, 13)); 
     thb.setStyle("-fx-background-color: gray"); 

     DropShadow ds = new DropShadow(); 
     ds.setOffsetY(3.0); 
     ds.setOffsetX(3.0); 
     ds.setColor(Color.GRAY); 

     bp = new BorderPane(); 
     bp.setEffect(ds); 
     bp.setPrefSize(600, 500); 
     bp.setMaxSize(600, 500); 

     bp.setStyle("-fx-background-color: white;"); 
     bp.setTop(thb); 
     flow.getChildren().add(bp); 
     Scene scene = new Scene(flow, 1200, 800); 
     scene.getStylesheets().add("/styles/Styles.css"); 

     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

由於某些原因,我使用布爾標誌位代碼的邏輯不正確。你能幫我解決這個問題嗎?

回答

1

使用flag在其他部分值爲true

  if (flag == true) 
      { 
       flag = false; 
       bp.setPrefSize(600, 40); 
       bp.setMaxSize(600, 40); 

      } 
      else 
      { 
       flag = true; 
       bp.setPrefSize(600, 500); 
       bp.setMaxSize(600, 500); 
      }