簡體   English   中英

什么相當於JavaFX中的JPanel

[英]Whats the equivalent of JPanel in JavaFX

我仍然在JavaFX中學習和試驗GUI ,我似乎無法獲得我想要的“外觀”..我試圖在面板中組合幾個標簽 ,然后在另一個面板中添加另一個標簽 但我似乎無法弄清楚如何在JavaFX中正確使用“JPanels”

任何幫助將不勝感激:D謝謝

編輯:這是我試圖通過嘗試不同的布局,仍然沒有運氣實現

預習

雖然Java FX Pane類似於Swing JPanel ,但下面的示例使用Pane子類來獲得各種布局效果。 尤其是,

  • 而不是將JPanel設置為GridLayout ,而是使用GridPane

  • 而不是將JPanel設置為BoderLayout ,而是使用BorderPane

  • 使用ContentDisplay.TOP到標簽的內容定位其上面的文字,如圖所示這里

  • 使用ContentDisplay.CENTER for topCenter使標簽覆蓋矩形; 為了比較,以前的版本使用了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);
    }
}

相當於JPanelJavaFX是一個Pane

你可以有一些非常好的教程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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM