本文介绍了什么相当于JavaFX中的JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在 JavaFX 中学习并尝试 GUI ,我似乎无法获得我所追求的外观..我是尝试在面板中对几个标签进行分组,然后在不同面板中添加另一个标签。但我似乎无法弄清楚如何在 JavaFX 中正确使用JPanels



任何帮助都将是非常感谢:D
谢谢



编辑:
以下是我尝试通过尝试不同的布局实现的目标,没有运气



解决方案

Java FX

  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
* /
公共类BorderTest扩展应用程序{

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(黑色);
root.setBackground(new Background(new BackgroundFill(
Color.LIGHTGRAY,CornerRadii.EMPTY,Insets.EMPTY)));

BorderPane top = new BorderPane();
top.setPadding(new Insets(16));
top.setBorder(红色);
top.setLeft(createLabel(Label 1,yellow,16));
标签topCenter = createLabel(标签2,黄色,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(蓝色);
bot.setCenter(createLabel(Label 4,Color.GREEN,24));
root.add(bot,0,1);

场景场景=新场景(根);
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);
标签l =新标签(text,r);
l.setContentDisplay(ContentDisplay.TOP);
l.setTextFill(color);
l.setFont(new Font(16));
返回l;
}

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


I'm still learning and experimenting with GUIs in JavaFX and I cant seem to get the "look" that I'm aiming for.. I'm trying to group a couple of Labels in a Panel and then in a different panel add another Label. But I cant seem to figure out how to properly use "JPanels" in JavaFX?

Any help would be greatly appreciated :DThanks

EDIT:Here is what I'm trying to achieve by trying different layouts, no luck still

解决方案

While Java FX Pane is similar to Swing JPanel, the example below uses subclasses of Pane to get various layout effects. In particular,

  • Instead of a JPanel set to GridLayout, use GridPane.

  • Instead of a JPanel set to BoderLayout, use BorderPane.

  • Use ContentDisplay.TOP to position a label's content above its text, as shown here.

  • Use ContentDisplay.CENTER for topCenter to make the label overlay the rectangle; for comparison, a previous version used StackPane.

  • Use setPadding(), setMargin() and setVgap() to spread things out a little.

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);
    }
}

这篇关于什么相当于JavaFX中的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:30