0

我有 3 个使用网格窗格定位的颜色选择器,我也在使用画布。但问题是,画布将网格窗格推到屏幕底部。因此,如果我使用root.add(HeadColor, 0, 1);(将第一个颜色选择器添加到第 0 列和第 1 行的网格窗格中)颜色选择器将自身定位在屏幕中间的某个位置,因为网格窗格从网格窗格下方开始。所以我的问题是,如何将网格窗格放置在画布顶部,或将颜色选择器放置在画布顶部?

我使用的代码:

package gui;

import display.Image;
import java.awt.Button;
import java.io.IOException;
import java.util.logging.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.canvas.*;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JavaFXColorPicker extends Application {
private Image head;
private Image heart;

ColorPicker HeadColor = new ColorPicker();
ColorPicker BodyColor = new ColorPicker();
ColorPicker LegColor = new ColorPicker();

@Override
public void start(Stage primaryStage) throws IOException {

    GridPane root = new GridPane();
    final Canvas canvas = new Canvas(200,300);

    Scene scene = new Scene(root, 500, 400, Color.BLACK);   
    GraphicsContext gc = canvas.getGraphicsContext2D();
    root.setHgap(10);
    root.setVgap(10);
    root.setPadding(new Insets(10, 30, 30, 30));

    HeadColor.setOnAction(new EventHandler(){ 
        @Override
        public void handle(Event event) {
            Color HeadPaint = HeadColor.getValue();
            Color[] HeadColorArray = new Color[]
                {
                    new Color(0 , 0, 0, 1),
                    HeadPaint,
                    HeadPaint,
                    HeadPaint,
                    HeadPaint,
                    new Color(1, 1, 1, 1) // de ogen
                 };

            try {
                head = new display.Image(display.Parts.playerHead);
            } catch (IOException ex) {
                Logger.getLogger(JavaFXColorPicker.class.getName())
                        .log(Level.SEVERE, null, ex);
            }

            head.recolour(HeadColorArray);
            Image i = head;
            gc.drawImage(i.show(), 200, 100);
        }
    });

    root.getChildren().add(canvas);
    root.add(HeadColor, 0, 1); 
    root.add(BodyColor, 0, 2); 
    root.add(LegColor,0,3);

    primaryStage.setTitle("Customize Character!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

1 回答 1

0

问题是这样的:

root.getChildren().add(canvas);

GridPane文档指出:

如果未显式设置行/列索引,则子项将被放置在第一行/列中。

就像使用 ColorPickers 一样,为 Canvas 分配一行和一列。

于 2016-01-02T03:18:28.973 回答