JavaFX:如何使netbeans像工具窗口?

时间:2014-07-15 10:51:36

标签: javafx

我正在开发一个新的GUI客户端,我们正在考虑使用JavaFX。我想知道是否有人建议制作可拖动的ToolWindow:

toolwindow

示例中的

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

1 个答案:

答案 0 :(得分: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>