【问题标题】:JavaFX 2 Line on Pane窗格上的 JavaFX 2 行
【发布时间】:2013-07-16 09:42:58
【问题描述】:

我想在窗格中添加一行,但运行应用程序时未显示该行。 在我的窗格中添加带有 ImageView 的标签正在工作,我可以看到我在 ImageView 上设置的图像。我尝试以多种方式显示一条线,使用画布,绘制一条线并将画布添加到窗格中,还构建一条线并将其直接添加到窗格中,没有一个有效。

Pane pane = new Pane();
//Image image = new Image("image.png");
//ImageView imageView = new ImageView();
//imageView.setImage(image);
//Label label = new Label();
//label.setGraphic(imageView);
//Canvas canvas = new Canvas(105, 105);
//canvas.relocate(296, 128);
//GraphicsContext gc = canvas.getGraphicsContext2D();
//gc.setStroke(Color.BLUE);
//gc.setLineWidth(5);
//gc.strokeLine(0, 0, canvas.getWidth(), canvas.getHeight());
Line redLine = LineBuilder.create()
                        .startX(296)
                        .startY(128)
                        .endX(401)
                        .endY(233)
                        .fill(Color.RED)
                        .strokeWidth(10.0f)
                        .build();
pane.getChildren().add(redLine);
//pane.getChildren().add(canvas);
//pane.getChildren().addAll(label, redLine);

【问题讨论】:

  • “不工作”是什么意思?当我复制粘贴您的代码时,它对我有用(显示了一条线,虽然是一条黑色的)
  • 是的,我的背景是黑色的,这就是为什么我没有看到这条线。必须设置属性 stroke 才能用想要的颜色绘制线条。

标签: java javafx javafx-2


【解决方案1】:

您发布的代码似乎可以正常工作 - 这是我得到的结果:

这是我使用的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;
import javafx.stage.Stage;


public class FXLine extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Pane pane = new Pane();
        Line redLine = LineBuilder.create()
            .startX(296)
            .startY(128)
            .endX(401)
            .endY(233)
            .fill(Color.RED)
            .strokeWidth(10.0f)
            .build();
        pane.getChildren().add(redLine);
        stage.setScene(new Scene(pane, 425, 275));
        stage.show();
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多