gpt4 book ai didi

JavaFX 可调整 Canvas 大小的问题

转载 作者:行者123 更新时间:2023-12-01 10:01:38 25 4
gpt4 key购买 nike

我有一个问题已部分解决,但我很好奇另一个(更好的)解决方案..我想要一个 Canvas ,它可以填充整个窗口,并在窗口大小调整时自行调整大小。

选项 1(部分解决方案):

据此:https://dlemmermann.wordpress.com/2014/04/10/javafx-tip-1-resizable-canvas/这是:How to make canvas Resizable in javaFX?我已经创建(复制)了该类:

public class ResizableCanvas extends Canvas {

public ResizableCanvas() {
widthProperty().addListener(evt -> draw());
heightProperty().addListener(evt -> draw());
}

private void draw() {
double width = getWidth();
double height = getHeight();

GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, width, height);

gc.setStroke(Color.RED);
gc.strokeLine(0, 0, width, height);
gc.strokeLine(0, height, width, 0);
}

@Override
public boolean isResizable() {
return true;
}

@Override
public double prefWidth(double height) {
return getWidth();
}

@Override
public double prefHeight(double width) {
return getHeight();
}
}

我将以下内容放入我的 ViewField.fxml 文件中:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.StackPane?>
<?import blahblahblah.ResizableCanvas?>

<StackPane fx:id="pane" minHeight="500.0" minWidth="500.0" style="-fx-background-color: white; -fx-border-color: black;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blahblahblah.ViewFieldController">
<children>
<ResizableCanvas fx:id="resizableCanvas" />
</children>
</StackPane>

我的 Controller (附加到此 fxml 文件)是 ViewFieldController.java:

import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.fxml.FXML;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ViewFieldController {

private Stage dialogStage;

@FXML
private StackPane pane;

@FXML
private ResizableCanvas resizableCanvas;

@FXML
private void initialize() {

GraphicsContext gc = resizableCanvas.getGraphicsContext2D();

ReadOnlyDoubleProperty widthProperty = pane.widthProperty();
ReadOnlyDoubleProperty heightProperty = pane.heightProperty();
resizableCanvas.widthProperty().bind(widthProperty);
resizableCanvas.heightProperty().bind(heightProperty);

drawArea(gc);
}

public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}

private void drawArea(GraphicsContext gc) {

gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(0, 0, resizableCanvas.getWidth(), resizableCanvas.getHeight());
gc.strokeLine(0, resizableCanvas.getHeight(), resizableCanvas.getWidth(), 0);
}

}

窗口是从主 Controller MainApp.java加载的:

public class MainApp extends Application {

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

public void showViewField() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/ViewField.fxml"));
StackPane pane = (StackPane) loader.load();

Stage stage = new Stage();
stage.setTitle("View Field");
Scene scene = new Scene(pane);
stage.setScene(scene);

ViewFieldController controller = loader.getController();
controller.setDialogStage(stage);

stage.show();

} catch (IOException e) {
e.printStackTrace();
}
}

}

这个解决方案有效(“大X”出现在 Canvas 上,这要归功于drawArea()方法)。此解决方案的问题是,将 ResisableCanvas 类放入 FXML 文件后,我无法再在此文件上使用 SceneBuilder,因为出现以下异常:

java.io.IOException: javafx.fxml.LoadException: 
...

at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.load(FXOMLoader.java:92)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:82)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:97)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.updateFxomDocument(EditorController.java:2384)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.setFxmlTextAndLocation(EditorController.java:664)
at com.oracle.javafx.scenebuilder.app.DocumentWindowController.loadFromFile(DocumentWindowController.java:381)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.performOpenFiles(SceneBuilderApp.java:554)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.handleOpenFilesAction(SceneBuilderApp.java:424)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.handleLaunch(SceneBuilderApp.java:403)
at com.oracle.javafx.scenebuilder.app.AppPlatform.requestStartGeneric(AppPlatform.java:139)
at com.oracle.javafx.scenebuilder.app.AppPlatform.requestStart(AppPlatform.java:106)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.start(SceneBuilderApp.java:353)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
...

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMLoader.load(FXOMLoader.java:89)
... 20 more
Caused by: java.lang.ClassNotFoundException: blahblahblah.ResizableCanvas
at java.lang.ClassLoader.findClass(ClassLoader.java:530)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 25 more

选项 2(部分解决方案):

我的第二个解决方案使用相同的 ResizingCanvas 类,但没有 FXML 文件或 Controller 类。

public class MainApp extends Application {

//...

public void showViewField2() {

ResizableCanvas canvas = new ResizableCanvas();
StackPane pane = new StackPane();
pane.getChildren().add(canvas);

Stage stage = new Stage();
stage.setTitle("View Field");
Scene scene = new Scene(pane);
stage.setScene(scene);

canvas.widthProperty().bind(pane.widthProperty());
canvas.heightProperty().bind(pane.heightProperty());

stage.show();
}

}

这个解决方案也有效(屏幕上出现大X),但同样的问题出现在这里:我无法在 ScreenBuilder 中打开 FXML 文件,但出于不同的原因(根本没有 FXML 文件)。

选项 3(我原来的解决方案,不起作用):

我已用原始 Canvas 类替换了 ViewField.fxml 文件中的 RessizedCanvas 类:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="pane" minHeight="500.0" minWidth="500.0" style="-fx-background-color: white; -fx-border-color: black;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blahblahblah.ViewFieldController">
<children>
<Canvas fx:id="regularCanvas" />
</children>
</StackPane>

我的 ViewFieldController.java 看起来像这样:

import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ViewFieldController {

private Stage dialogStage;

@FXML
private StackPane pane;

@FXML
private Canvas regularCanvas;

@FXML
private void initialize() {

GraphicsContext gc = regularCanvas.getGraphicsContext2D();

ReadOnlyDoubleProperty widthProperty = pane.widthProperty();
ReadOnlyDoubleProperty heightProperty = pane.heightProperty();
regularCanvas.widthProperty().bind(widthProperty);
regularCanvas.heightProperty().bind(heightProperty);

drawArea(gc);
}

public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}

private void drawArea(GraphicsContext gc) {

gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(0, 0, regularCanvas.getWidth(), regularCanvas.getHeight());
gc.strokeLine(0, regularCanvas.getHeight(), regularCanvas.getWidth(), 0);
}

}

但是这里的屏幕是空的。 Canvas 上没有绘制“大X”。如果我调整窗口大小,我不知道 Canvas 大小是否调整,但可以将 FXML 文件加载到 SceneBuilder 中以进行进一步的工作..

如果我给 Canvas 一些高度和宽度也不是更好(结果是一样的)。

<StackPane fx:id="pane" minHeight="500.0" minWidth="500.0" style="-fx-background-color: white; -fx-border-color: black;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blahblahblah.ViewFieldController">
<children>
<Canvas fx:id="regularCanvas" height="300.0" width="300.0" />
</children>
</StackPane>

我的问题是:

  1. 为什么第三个案例中没有出现“大X”?
  2. 我应该做哪些修改才能使第三个案例起作用?
  3. 还有其他解决方案可以解决我的问题吗?
  4. 为什么我在第一种情况下遇到异常?
  5. 为什么无法在 SceneBuilder 中“适合父级” Canvas ?
  6. 为什么在 SceneBuilder 中禁用“可调整”选项?

最佳答案

您可以通过将自定义 Canvas 捆绑到 jar 文件并将其导入场景生成器库,使选项 1 与场景生成器一起使用。请参阅this question ,例如。

选项 3 不起作用,因为您只从 initialize() 方法调用 drawArea()。此时, Canvas 不是场景的一部分,当然也不会显示在窗口中;因此它还没有经过布局,因此宽度和高度为零。您可以通过向宽度和高度添加监听器并在它们发生变化时重新绘制来修复此选项,就像在选项 1 中所做的那样:

@FXML
private void initialize() {

GraphicsContext gc = regularCanvas.getGraphicsContext2D();

ReadOnlyDoubleProperty widthProperty = pane.widthProperty();
ReadOnlyDoubleProperty heightProperty = pane.heightProperty();
regularCanvas.widthProperty().bind(widthProperty);
regularCanvas.heightProperty().bind(heightProperty);

regularCanvas.widthProperty().addListener((obs, oldWidth, newWidth) -> drawArea(gc));
regularCanvas.heightProperty().addListener((obs, oldHeight, newHeight) -> drawArea(gc));

drawArea(gc);
}

关于JavaFX 可调整 Canvas 大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com