2014-07-15 34 views
2

我正在研究一个新的GUI客户端,我们正在考虑使用JavaFX。我想知道如果任何人有关于制作可拖动工具窗口的建议:在本例中JavaFX:如何制作一个类似于工具窗口的NetBeans?

toolwindow

(客户窗口)。这个小窗口应该能够最小化并拖出父窗口,并停靠在它上面。我真的不想使用netbeans平台(或者eclipse或swing)。

回答

1

你只需要创建一个自定义面板为:

enter image description here

完整的例子可以用gist

CustomPane.java

import java.net.URL; 
import java.util.ResourceBundle; 

import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Node; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.shape.Rectangle; 


public class CustomPane extends AnchorPane implements Initializable { 
    @FXML Button closePane; 
    @FXML Button minPane; 
    @FXML Label title; 
    @FXML Pane contentPane; 
    @FXML Rectangle separator; 
    private boolean restoreFlag = false; 
    private double prefX; 
    private double prefY; 
    public CustomPane(String title){ 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"CustomPane.fxml")); 
     fxmlLoader.setRoot(this); 
     fxmlLoader.setController(this); 
     try { 
      fxmlLoader.load(); 
     } catch (Exception exception) { 
      throw new RuntimeException(exception); 
     } 
     this.title.setText(title); 
     setButtonsLayout(); 
     setStandardLayout(); 
    } 
    public CustomPane(String title, double y, double x){ 
     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"CustomPane.fxml")); 
     fxmlLoader.setRoot(this); 
     fxmlLoader.setController(this); 
     try { 
      fxmlLoader.load(); 
     } catch (Exception exception) { 
      throw new RuntimeException(exception); 
     } 
     this.title.setText(title); 
     this.setPrefSize(x, y); 
     setButtonsLayout(); 
     setStandardLayout(); 
     restoreRoot(); 
    } 
    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     closePane.setOnAction(new EventHandler<ActionEvent>(){ 
      @Override 
      public void handle(ActionEvent event) { 
       setInvisible(); 
      } 
     }); 
     minPane.setOnAction(new EventHandler<ActionEvent>(){ 
      @Override 
      public void handle(ActionEvent event) { 
       if(restoreFlag){ 
        restoreFlag = false; 
        minPane.setText("_"); 
        restoreRoot(); 
       } 
       else{ 
        restoreFlag = true; 
        minPane.setText("▭"); 
        minimizeRoot(); 
       } 
      } 
     }); 

    } 
    protected void minimizeRoot() { 
     this.setPrefSize(prefX/2, title.getPrefHeight() + 5.0); 
     separator.setWidth(prefX/2 + 5); 
     this.setLayoutX(0); 
     this.setLayoutY(prefY-40); 
     setButtonsLayout(); 
     contentPane.setVisible(false); 
    } 
    protected void restoreRoot() { 
     this.setPrefSize(prefX, prefY); 
     separator.setWidth(prefX); 
     contentPane.setVisible(true); 
     this.setLayoutX(0); 
     this.setLayoutY(0); 
     setButtonsLayout(); 
    } 
    private void setStandardLayout() { 
     prefX = this.getPrefHeight(); 
     prefY = this.getPrefWidth()-50; 
    } 
    public void setButtonsLayout(){ 
     closePane.setLayoutX(this.getPrefWidth()-30); 
     minPane.setLayoutX(this.getPrefWidth()-55); 
    } 
    public void setInvisible(){ 
     this.setVisible(false); 
    } 
    public ObservableList<Node> getContent(){ 
     return contentPane.getChildren(); 
    } 
} 
<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.shape.*?> 

customPane.fxml

<fx:root prefHeight="400.0" prefWidth="400.0" 
    type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" 
    xmlns:fx="http://javafx.com/fxml/1"> 
    <children> 
     <Rectangle fx:id="separator" arcHeight="5.0" arcWidth="5.0" fill="WHITE" 
      height="33.0" layoutY="1.0" stroke="BLACK" strokeType="INSIDE" width="600.0" /> 
     <Button fx:id="closePane" layoutX="573.0" layoutY="6.0" 
      mnemonicParsing="false" text="X" /> 
     <Button fx:id="minPane" layoutX="545.0" layoutY="6.0" 
      mnemonicParsing="false" text="__" /> 
     <Label fx:id="title" layoutX="14.0" layoutY="12.0" text="Title" /> 
     <Pane fx:id="contentPane" layoutY="34.0" prefHeight="366.0" 
      prefWidth="600.0" /> 
    </children> 
</fx:root> 
+0

好办法,但这CustomPane也不能拖动。你怎么能把它拖出停靠的窗格? – user3111525

相关问题