java JavaFX 中 JPanel 的等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37931670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats the equivalent of JPanel in JavaFX
提问by user6408978
I'm still learning and experimenting with GUIs in JavaFXand I cant seem to get the "look" that I'm aiming for.. I'm trying to group a couple of Labelsin a Paneland then in a different paneladd another Label. But I cant seem to figure out how to properly use "JPanels"in JavaFX?
我还在学习和尝试使用GUIS IN的JavaFX,我不能似乎得到了“看”是我的目标..我想组一对夫妇的标签一个在面板,然后在不同的面板插件另一个标签。但我似乎无法弄清楚如何在JavaFX 中正确使用“JPanels”?
Any help would be greatly appreciated :D Thanks
任何帮助将不胜感激:D 谢谢
EDIT:Here is what I'm trying to achieve by trying different layouts, no luck still
编辑:这是我试图通过尝试不同的布局来实现的目标,仍然没有运气
回答by trashgod
While Java FX Pane
is similar to Swing JPanel
, the example below uses subclasses of Pane
to get various layout effects. In particular,
虽然 Java FXPane
与 Swing 类似JPanel
,但下面的示例使用 的子类Pane
来获得各种布局效果。特别是,
Instead of a
JPanel
set toGridLayout
, useGridPane
.Instead of a
JPanel
set toBoderLayout
, useBorderPane
.Use
ContentDisplay.TOP
to position a label's content above its text, as shown here.Use
ContentDisplay.CENTER
fortopCenter
to make the label overlay the rectangle; for comparison, a previous versionusedStackPane
.Use
setPadding()
,setMargin()
andsetVgap()
to spread things out a little.
取而代之的是
JPanel
设置为GridLayout
,使用GridPane
。取而代之的是
JPanel
设置为BoderLayout
,使用BorderPane
。使用
ContentDisplay.TOP
一个标签的内容定位其上面的文字,如图所示这里。使用
ContentDisplay.CENTER
为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);
}
}
回答by GOXR3PLUS
You can have some very nice tutorials http://java2s.com/about javaFX and many many more.JavaFX equivalent to JPanel
is Paneand an example:(taken from http://zetcode.com/gui/javafx/layoutpanes/)
你可以有一些非常好的教程http://java2s.com/关于 javaFX 等等。JavaFX 相当于JPanel
是Pane和一个例子:(取自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);
}
}