简体   繁体   English

是否可以将窗格的高度/宽度绑定到 BorderPane 的中心节点?

[英]Is it possible to bind the height/width of a pane to the center node of a BorderPane?

I have an assignment that requires me to extend the pane class, then add a rectangle on it.我有一个任务需要我扩展窗格 class,然后在其上添加一个矩形。 I then fill the rectangle and use some buttons to change the colors of the rectangle.然后我填充矩形并使用一些按钮来更改矩形的 colors。 I added my extended Pane class to the center node of the border pane, but I envisioned the rectangle taking up the whole pane and binging to the Pane's height and width, then in turn the Pane taking up the whole center Pane of the BorderPane and bound to the centerPane's height and width.我将扩展的窗格 class 添加到边框窗格的中心节点,但我设想矩形占据整个窗格并与窗格的高度和宽度相结合,然后窗格占用整个边框窗格的中心窗格并绑定到 centerPane 的高度和宽度。 Should I be making a Pane, then adding the Extended Pane to the Pane, then adding the Pane to the BorderPane's Center Pane?我是否应该制作一个窗格,然后将扩展窗格添加到窗格,然后将窗格添加到 BorderPane 的中心窗格? Here's the entirety of my code.这是我的全部代码。 Image of what I get so far below as well.我在下面得到的图像也是如此。

在此处输入图像描述

package homework;

import javafx.scene.shape.*;

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ColorSample extends Application{
    @Override
    public void start (Stage primaryStage) {
        //Create border pane
        BorderPane bp = new BorderPane();
        
        //Create saturation buttons
        Button btnMSat = new Button("More Saturated");
        Button btnLSat = new Button("Less Saturated");
        
        //Create hue buttons
        Button btnHueU = new Button("Hue Up");
        Button btnHueD = new Button("Hue Down");
        
        //Create Darker/Brighter buttons
        Button btnDark = new Button("Darker");
        Button btnBright = new Button("Brighter");
        
        //Create Top and bottom HBoxes
        HBox THBox = new HBox(10);
        THBox.setAlignment(Pos.CENTER);
        HBox BHBox = new HBox(10);
        BHBox.setAlignment(Pos.CENTER);
        
        //Create ColorPane
        Color color = Color.MEDIUMBLUE;
        ColorPane cp = new ColorPane(color);
        cp.setColor(color);
        //Set Events
        //More Saturation Clicked
        btnMSat.setOnAction(e->{
            cp.setColor(cp.moreSaturation(cp.getColor()));
        });
        //Less Saturation Clicked
        btnLSat.setOnAction(e->{
            cp.setColor(cp.lessSaturation(cp.getColor()));
        });
        //Hue Up Clicked
        btnHueU.setOnAction(e->{
            cp.setHue(cp.getColor(), 30);
        });
        //Hue Down Clicked
        btnHueD.setOnAction(e->{
            cp.setHue(cp.getColor(), -30);
        });
        //Darker Clicked
        btnDark.setOnAction(e->{
            cp.darker(cp.getColor());
        });
        //Brighter Clicked
        btnDark.setOnAction(e->{
            cp.brighter(cp.getColor());
        });
        
        //Add Saturation Buttons to Top HBox
        THBox.getChildren().addAll(btnMSat, btnLSat);
        bp.setTop(THBox);
        //Add Darker/Lighter buttons to Bottom HBox
        BHBox.getChildren().addAll(btnDark, btnBright);
        bp.setBottom(BHBox);
        //Create right side VBox
        VBox RVBox = new VBox(10);
        RVBox.setAlignment(Pos.CENTER);
        //Add Hue buttons to right side VBox
        RVBox.getChildren().addAll(btnHueU, btnHueD);
        bp.setPrefSize(450, 325);
        bp.setRight(RVBox);
        //Add ColorPane to Border Panel
        bp.setCenter(cp);
        BorderPane.setAlignment(cp, Pos.CENTER);
        
        
        //Create scene and add border pane to it
        Scene scene = new Scene(bp, 525, 450);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    class ColorPane extends Pane {
        Rectangle rectangle = new Rectangle();
            
    
        ColorPane() {
            //Set properties
            rectangle.setX(0);
            rectangle.setY(0);
            //Bind width and height
            rectangle.setWidth(USE_COMPUTED_SIZE);
            rectangle.setHeight(USE_COMPUTED_SIZE);
            getChildren().add(rectangle);
        }
        
        ColorPane(Color c) {
            //Set properties
            rectangle.setX(0);
            rectangle.setY(0);
            rectangle.setWidth(450);
            rectangle.setHeight(450);
            //Bind width and height
            rectangle.widthProperty().bind(widthProperty());
            rectangle.heightProperty().bind(heightProperty());
            setColor(c);
            rectangle.setStroke(c);
            rectangle.setFill(c);
            getChildren().add(rectangle);
        }   
        
        @Override
        public void setWidth(double w) {
            super.setWidth(w);
            
        }
        
        @Override
        public void setHeight(double h) {
            super.setWidth(h);
            
        }
        
        public Color setHue(Color color, double h) {
            double newHue = 0;
            
            if(color.getHue() + h >= 360)
                newHue = 360;
            else if (color.getBlue() + h <= 0)
                newHue = 0;
            else
                newHue+=h;
            
            Color output = Color.hsb(newHue, color.getSaturation(),
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color moreSaturation(Color color) {
            double newSat = 0;
            
            if (Math.pow(color.getSaturation(), 2) > 1)
                newSat = 1;
            else
                newSat = Math.pow(color.getSaturation(), 2);
            
            Color output = Color.hsb(color.getHue(), newSat,
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color lessSaturation(Color color) {
            double newSat = 0;
            
            if (Math.sqrt(color.getSaturation()) > 0)
                newSat = Math.sqrt(color.getSaturation());
            
            Color output = Color.hsb(color.getHue(), newSat,
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color darker(Color color) {
            double brightness = 0;
            if(Math.pow(color.getBrightness(), 2) >=1)
                brightness =1;
            else
                brightness = Math.pow(color.getBrightness(), 2);
            Color output = Color.hsb(color.getHue(), color.getSaturation(),
                    brightness, color.getOpacity());
            return output;
        }
        
        public Color brighter(Color color) {
            double brightness = 0;
            if(Math.sqrt(color.getBrightness())>0)
                brightness = Math.sqrt(color.getBrightness());
            
            Color output = Color.hsb(color.getHue(), color.getSaturation(),
                    brightness, color.getOpacity());
            
            return output;
        }
        
        public Color getColor() {
            return (Color) rectangle.getFill();
        }
        
        public void setColor(Color c) {
            rectangle.setStroke(c);
            rectangle.setFill(c);
        }
    }
    
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Actually set the addition of the ColorPane to the top left position of the center node of the BorderPane with the following then added it to the center:实际上将ColorPane的添加设置到BorderPane的中心节点的左上角position,然后将其添加到中心:

bp.setAlignment(cp, Pos.TOP_LEFT);
bp.setCenter(cp);

Still trying to figure out how to deal with the behavior when it resizes, but this is much better.仍在尝试弄清楚如何在调整大小时处理该行为,但这要好得多。

Ok, so in the ColorPane constructors I commented out the lines that bind the height and width, so now the rectangle appears, but while the rectangle starts at coordinates 0,0 of the Pane (See constructors), The ColorPane Itself is not filling the center pane of the Border Pane.好的,所以在 ColorPane 构造函数中,我注释掉了绑定高度和宽度的行,所以现在出现了矩形,但是当矩形从窗格的坐标 0,0 开始时(参见构造函数),ColorPane 本身并没有填充边框窗格的中心窗格。 Screenshot and code below.截图和代码如下。

矩形需要填充中心窗格,而不是与底部窗格重叠

package homework;

import javafx.scene.shape.*;

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ColorSample extends Application{
    @Override
    public void start (Stage primaryStage) {
        //Create border pane
        BorderPane bp = new BorderPane();
        
        //Create saturation buttons
        Button btnMSat = new Button("More Saturated");
        Button btnLSat = new Button("Less Saturated");
        
        //Create hue buttons
        Button btnHueU = new Button("Hue Up");
        Button btnHueD = new Button("Hue Down");
        
        //Create Darker/Brighter buttons
        Button btnDark = new Button("Darker");
        Button btnBright = new Button("Brighter");
        
        //Create Top and bottom HBoxes
        HBox THBox = new HBox(10);
        THBox.setAlignment(Pos.CENTER);
        HBox BHBox = new HBox(10);
        BHBox.setAlignment(Pos.CENTER);
        
        //Create ColorPane
        Color color = Color.MEDIUMBLUE;
        ColorPane cp = new ColorPane(color);
        cp.setColor(color);
        //Set Events
        //More Saturation Clicked
        btnMSat.setOnAction(e->{
            cp.setColor(cp.moreSaturation(cp.getColor()));
        });
        //Less Saturation Clicked
        btnLSat.setOnAction(e->{
            cp.setColor(cp.lessSaturation(cp.getColor()));
        });
        //Hue Up Clicked
        btnHueU.setOnAction(e->{
            cp.setHue(cp.getColor(), 30);
        });
        //Hue Down Clicked
        btnHueD.setOnAction(e->{
            cp.setHue(cp.getColor(), -30);
        });
        //Darker Clicked
        btnDark.setOnAction(e->{
            cp.darker(cp.getColor());
        });
        //Brighter Clicked
        btnDark.setOnAction(e->{
            cp.brighter(cp.getColor());
        });
        
        //Add Saturation Buttons to Top HBox
        THBox.getChildren().addAll(btnMSat, btnLSat);
        bp.setTop(THBox);
        //Add Darker/Lighter buttons to Bottom HBox
        BHBox.getChildren().addAll(btnDark, btnBright);
        bp.setBottom(BHBox);
        //Create right side VBox
        VBox RVBox = new VBox(10);
        RVBox.setAlignment(Pos.CENTER);
        //Add Hue buttons to right side VBox
        RVBox.getChildren().addAll(btnHueU, btnHueD);
        //bp.setPrefSize(450, 325);
        bp.setRight(RVBox);
        //Add ColorPane to Center Border Panel
        //Insets inset = new Insets(5,5,5,5);
        //bp.setPadding(inset);
        //bp.setAlignment(cp, Pos.CENTER);
        bp.setCenter(cp);
        
        
        //Create scene and add border pane to it
        Scene scene = new Scene(bp, 525, 450);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    class ColorPane extends Pane {
        Rectangle rectangle = new Rectangle();
            
    
        ColorPane() {
            //Set properties
            rectangle.setX(0);
            rectangle.setY(0);
            //Bind width and height
            /*rectangle.setWidth(USE_COMPUTED_SIZE);
            rectangle.setHeight(USE_COMPUTED_SIZE);*/
            getChildren().add(rectangle);
        }
        
        ColorPane(Color c) {
            //Set properties
            rectangle.setX(0);
            rectangle.setY(0);
            rectangle.setWidth(450);
            rectangle.setHeight(450);
            //Bind width and height
            /*rectangle.widthProperty().bind(widthProperty());
            rectangle.heightProperty().bind(heightProperty());*/
            setColor(c);
            rectangle.setStroke(c);
            rectangle.setFill(c);
            getChildren().add(rectangle);
        }   
        
        @Override
        public void setWidth(double w) {
            super.setWidth(w);
            
        }
        
        @Override
        public void setHeight(double h) {
            super.setWidth(h);
            
        }
        
        public Color setHue(Color color, double h) {
            double newHue = 0;
            
            if(color.getHue() + h >= 360)
                newHue = 360;
            else if (color.getBlue() + h <= 0)
                newHue = 0;
            else
                newHue+=h;
            
            Color output = Color.hsb(newHue, color.getSaturation(),
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color moreSaturation(Color color) {
            double newSat = 0;
            
            if (Math.pow(color.getSaturation(), 2) > 1)
                newSat = 1;
            else
                newSat = Math.pow(color.getSaturation(), 2);
            
            Color output = Color.hsb(color.getHue(), newSat,
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color lessSaturation(Color color) {
            double newSat = 0;
            
            if (Math.sqrt(color.getSaturation()) > 0)
                newSat = Math.sqrt(color.getSaturation());
            
            Color output = Color.hsb(color.getHue(), newSat,
                    color.getBrightness(), color.getOpacity());
            
            return output;
        }
        
        public Color darker(Color color) {
            double brightness = 0;
            if(Math.pow(color.getBrightness(), 2) >=1)
                brightness =1;
            else
                brightness = Math.pow(color.getBrightness(), 2);
            Color output = Color.hsb(color.getHue(), color.getSaturation(),
                    brightness, color.getOpacity());
            return output;
        }
        
        public Color brighter(Color color) {
            double brightness = 0;
            if(Math.sqrt(color.getBrightness())>0)
                brightness = Math.sqrt(color.getBrightness());
            
            Color output = Color.hsb(color.getHue(), color.getSaturation(),
                    brightness, color.getOpacity());
            
            return output;
        }
        
        public Color getColor() {
            return (Color) rectangle.getFill();
        }
        
        public void setColor(Color c) {
            rectangle.setStroke(c);
            rectangle.setFill(c);
        }
    }
    
    public static void main(String[] args) {
        Application.launch(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM