【问题标题】:Whats the equivalent of JPanel in JavaFXJavaFX中JPanel的等价物是什么
【发布时间】:2016-10-22 05:58:45
【问题描述】:

我仍在学习和试验 JavaFX 中的 GUI,但我似乎无法获得我想要的“外观”。我是尝试在一个面板中组合几个标签,然后在一个不同的面板中添加另一个标签。但我似乎无法弄清楚如何在 JavaFX 中正确使用 "JPanels"

任何帮助将不胜感激:D 谢谢

编辑: 这是我试图通过尝试不同的布局来实现的目标,但仍然没有运气

【问题讨论】:

  • tutorial
  • 请编辑您的问题以包含一个显示您的意图的 Swing minimal reproducible example 和一个显示您当前方法的 JavaFX minimal reproducible example example
  • 我编辑了问题以包含托管在快速文件链接上的图像。随着您获得更多声誉,您还可以内联包含图像。同时,请不要再链接到快速文件:那个网站太烦人了。我还建议您下载 SceneBuilder from Gluon 并尝试使用它创建您的 UI 布局。

标签: java swing user-interface layout javafx


【解决方案1】:

虽然 Java FX Pane 类似于 Swing JPanel,但下面的示例使用 Pane 的子类来获得各种布局效果。特别是,

  • 不要将JPanel 设置为GridLayout,而是使用GridPane

  • 不要将JPanel 设置为BoderLayout,而是使用BorderPane

  • 使用ContentDisplay.TOP 将标签的内容置于其文本上方,如here 所示。

  • 使用ContentDisplay.CENTERtopCenter 使标签覆盖矩形;为了比较,之前的version 使用了StackPane

  • 使用setPadding()setMargin()setVgap() 将事情分散一点。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/a/37935114/230513
 */
public class BorderTest extends Application {

    private static final Border black = new Border(new BorderStroke(Color.BLACK,
        BorderStrokeStyle.SOLID, new CornerRadii(8), new BorderWidths(2)));
    private static final Border red = new Border(new BorderStroke(Color.RED,
        BorderStrokeStyle.SOLID, new CornerRadii(8), new BorderWidths(2)));
    private static final Border blue = new Border(new BorderStroke(Color.BLUE,
        BorderStrokeStyle.SOLID, new CornerRadii(8), new BorderWidths(2)));
    private static final Color yellow = Color.YELLOW.deriveColor(0, .9, 1, 1);

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test");

        GridPane root = new GridPane();
        root.setPadding(new Insets(16));
        root.setVgap(16);
        root.setBorder(black);
        root.setBackground(new Background(new BackgroundFill(
            Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));

        BorderPane top = new BorderPane();
        top.setPadding(new Insets(16));
        top.setBorder(red);
        top.setLeft(createLabel("Label 1", yellow, 16));
        Label topCenter = createLabel("Label 2", yellow, 64);
        topCenter.setContentDisplay(ContentDisplay.CENTER);
        BorderPane.setMargin(topCenter, new Insets(16));
        top.setCenter(topCenter);
        top.setRight(createLabel("Label 3", yellow, 16));
        root.add(top, 0, 0);

        BorderPane bot = new BorderPane();
        bot.setPadding(new Insets(16));
        bot.setBorder(blue);
        bot.setCenter(createLabel("Label 4", Color.GREEN, 24));
        root.add(bot, 0, 1);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Label createLabel(String text, Color color, int size) {
        Rectangle r = new Rectangle(3 * size, 2 * size);
        r.setFill(Color.TRANSPARENT);
        r.setStroke(color);
        r.setStrokeWidth(3);
        Label l = new Label(text, r);
        l.setContentDisplay(ContentDisplay.TOP);
        l.setTextFill(color);
        l.setFont(new Font(16));
        return l;
    }

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

【讨论】:

  • 非常感谢,我从玩弄和试验您的示例中学到了很多东西!但我确实有一个小问题,例如,我如何获取顶部 BorderPaneHeightWidth(top.getWidth())
  • @user6408978:实验的荣誉;再次检查之后 show(),但问问自己为什么需要知道;用setHgrow(Priority.ALWAYS)添加ColumnConstraints的实例,如API所示,看看效果。
【解决方案2】:

你可以有一些非常好的教程http://java2s.com/ 关于 javaFX 等等。JavaFX 等价于 JPanelPane 和一个例子:(取自 http://zetcode.com/gui/javafx/layoutpanes/

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

/**
 * ZetCode JavaFX tutorial
 *
 * This program positions three shapes
 * using absolute coordinates.
 *
 * Author: Jan Bodnar
 * Website: zetcode.com
 * Last modified: June 2015
 */

public class AbsoluteLayoutEx extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        Pane root = new Pane();

        Rectangle rect = new Rectangle(25, 25, 50, 50);
        rect.setFill(Color.CADETBLUE);

        Line line = new Line(90, 40, 230, 40);
        line.setStroke(Color.BLACK);

        Circle circle = new Circle(130, 130, 30);
        circle.setFill(Color.CHOCOLATE);

        root.getChildren().addAll(rect, line, circle);

        Scene scene = new Scene(root, 250, 220, Color.WHITESMOKE);

        stage.setTitle("Absolute layout");
        stage.setScene(scene);
        stage.show();
    }

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

【讨论】:

    【解决方案3】:

    JPanel 等效的JavaFXPane

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2022-11-28
      • 2015-03-06
      相关资源
      最近更新 更多