下面列出了怎么用javafx.scene.text.TextAlignment的API类实例代码及写法,或者点击链接到github查看源代码。
private void showOpenFileButton() {
Hyperlink link = new Hyperlink(Util.text("create-file-open"));
TextFlow flow = new TextFlow(new WikiLabel("create-file-success"), link);
flow.setTextAlignment(TextAlignment.CENTER);
addElement(flow);
link.setOnAction(ev -> {
try {
Desktop.getDesktop().open(Session.FILE);
} catch (IOException ex) {
Session.LOGGER.log(Level.WARNING,
"Cannot open file: {0}",
new String[]{ex.getLocalizedMessage()}
);
}
});
nextButton.linkTo("StartPane", stage, true).setText(Util.text("create-file-back-to-start"));
nextButton.setVisible(true);
}
private void setContent() {
TextFlow flow = new TextFlow(new Text(Util.text("login-intro")), link);
flow.setTextAlignment(TextAlignment.CENTER);
addElement(flow);
addElement(loginText);
addElement(passwordText);
addElement(loginButton);
addElement(loginStatus);
if (!Settings.getSetting("user").isEmpty()) {
loginText.setText(Settings.getSetting("user"));
Platform.runLater(() -> {
passwordText.requestFocus();
});
}
}
private void initGraphics() {
if (Double.compare(getPrefWidth(), 0.0) <= 0 || Double.compare(getPrefHeight(), 0.0) <= 0 || Double.compare(getWidth(), 0.0) <= 0 ||
Double.compare(getHeight(), 0.0) <= 0) {
if (getPrefWidth() > 0 && getPrefHeight() > 0) {
setPrefSize(getPrefWidth(), getPrefHeight());
} else {
setPrefSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
}
}
canvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
ctx = canvas.getGraphicsContext2D();
text = new Text(String.format(Locale.US, "%.0f%%", getValue() * 100.0));
text.setTextAlignment(TextAlignment.CENTER);
text.setTextOrigin(VPos.TOP);
text.setFill(getTextColor());
Helper.enableNode(text, getTextVisible());
getChildren().setAll(canvas, text);
}
public Watch(int address, final MetacheatUI outer) {
super();
this.outer = outer;
this.address = address;
cell = outer.cheatEngine.getMemoryCell(address);
redraw = outer.animationTimer.scheduleAtFixedRate(this::redraw, MetacheatUI.FRAME_RATE, MetacheatUI.FRAME_RATE, TimeUnit.MILLISECONDS);
setBackground(new Background(new BackgroundFill(Color.NAVY, CornerRadii.EMPTY, Insets.EMPTY)));
Label addrLabel = new Label("$" + Integer.toHexString(address));
addrLabel.setOnMouseClicked((evt)-> outer.inspectAddress(address));
addrLabel.setTextAlignment(TextAlignment.CENTER);
addrLabel.setMinWidth(GRAPH_WIDTH);
addrLabel.setFont(new Font(Font.getDefault().getFamily(), 14));
addrLabel.setTextFill(Color.WHITE);
graph = new Canvas(GRAPH_WIDTH, GRAPH_HEIGHT);
getChildren().add(addrLabel);
getChildren().add(graph);
CheckBox hold = new CheckBox("Hold");
holding = hold.selectedProperty();
holding.addListener((prop, oldVal, newVal) -> this.updateHold());
getChildren().add(hold);
hold.setTextFill(Color.WHITE);
}
@Override protected void initGraphics() {
super.initGraphics();
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
description = new Label(tile.getDescription());
description.setAlignment(tile.getDescriptionAlignment());
description.setTextAlignment(TextAlignment.RIGHT);
description.setWrapText(true);
description.setTextOverrun(OverrunStyle.WORD_ELLIPSIS);
description.setTextFill(tile.getTextColor());
description.setPrefSize(PREFERRED_WIDTH * 0.9, PREFERRED_HEIGHT * 0.795);
Helper.enableNode(description, tile.isTextVisible());
text = new Text(tile.getText());
text.setFill(tile.getUnitColor());
Helper.enableNode(text, tile.isTextVisible());
getPane().getChildren().addAll(titleText, text, description);
}
@Override
public void updateItem(TopicInfo item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setGraphic(null);
setText(null);
} else {
Text t = IconBuilder.create(item.getFactory().getGlyph(), 18.0).build();
t.setFill(Color.WHITE);
TextFlow tf = new TextFlow(t);
tf.setTextAlignment(TextAlignment.CENTER);
tf.setPadding(new Insets(5, 5, 5, 5));
tf.setStyle("-fx-background-color: #505050; -fx-background-radius: 5px;");
tf.setPrefWidth(30.0);
setGraphic(tf);
String label = item.getLabel().getValue();
setText(item.getFactory().getTypeName() + ((label == null || label.isEmpty()) ? "" : " : " + label));
}
}
@Override protected void initGraphics() {
super.initGraphics();
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
description = new Label(tile.getDescription());
description.setAlignment(tile.getDescriptionAlignment());
description.setTextAlignment(TextAlignment.RIGHT);
description.setWrapText(true);
description.setTextOverrun(OverrunStyle.WORD_ELLIPSIS);
description.setTextFill(tile.getDescriptionColor());
description.setPrefSize(PREFERRED_WIDTH * 0.9, PREFERRED_HEIGHT * 0.795);
Helper.enableNode(description, tile.isTextVisible());
text = new Text(tile.getText());
text.setFill(tile.getUnitColor());
Helper.enableNode(text, tile.isTextVisible());
getPane().getChildren().addAll(titleText, text, description);
}
@Override protected void initGraphics() {
super.initGraphics();
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
description = new Label(tile.getDescription());
description.setAlignment(Pos.CENTER);
description.setTextAlignment(TextAlignment.CENTER);
description.setWrapText(true);
description.setTextOverrun(OverrunStyle.WORD_ELLIPSIS);
description.setTextFill(tile.getTextColor());
description.setPrefSize(PREFERRED_WIDTH * 0.9, PREFERRED_HEIGHT * 0.795);
Helper.enableNode(description, tile.isTextVisible());
text = new Text(tile.getText());
text.setFill(tile.getUnitColor());
Helper.enableNode(text, tile.isTextVisible());
getPane().getChildren().addAll(titleText, text, description);
}
private void drawLogoLayer() {
ctx.clearRect(0, 0, size, size);
if (Clock.Design.BOSCH == getSkinnable().getDesign()) {
ctx.setFill(getSkinnable().isNightMode() ? Color.rgb(240, 240, 240) : Color.rgb(10, 10, 10));
ctx.fillRect(size * 0.5 - 1, size * 0.18, 2, size * 0.27);
ctx.fillRect(size * 0.5 - 1, size * 0.55, 2, size * 0.27);
ctx.fillRect(size * 0.18, size * 0.5 - 1, size * 0.27, 2);
ctx.fillRect(size * 0.55, size * 0.5 - 1, size * 0.27, 2);
}
if (getSkinnable().getText().isEmpty()) return;
ctx.setFill(getSkinnable().isNightMode() ? Color.WHITE : Color.BLACK);
ctx.setFont(Fonts.opensansSemiBold(size * 0.05));
ctx.setTextBaseline(VPos.CENTER);
ctx.setTextAlign(TextAlignment.CENTER);
ctx.fillText(getSkinnable().getText(), size * 0.5, size * 0.675, size * 0.8);
}
private void drawDataPoints() {
ctx.setTextAlign(TextAlignment.CENTER);
ctx.setTextBaseline(VPos.CENTER);
ctx.setFont(Font.font(size * 0.0175));
for (int i = 0 ; i < points.size() ; i++) {
DataPoint point = points.get(i);
ctx.setFill(Color.rgb(255, 255, 255, 0.5));
ctx.fillOval(point.getX() - 8, point.getY() - 8, 16, 16);
//ctx.setStroke(getUseColorMapping() ? getColorForValue(point.getValue(), 1) : getColorForValue(point.getValue(), isDiscreteColors()));
ctx.setStroke(Color.BLACK);
ctx.strokeOval(point.getX() - 8, point.getY() - 8, 16, 16);
ctx.setFill(Color.BLACK);
ctx.fillText(Long.toString(Math.round(point.getValue())), point.getX(), point.getY(), 16);
}
}
private void drawBoxes(List<DetectedObject> predictedObjects) {
ctx.setLineWidth(3);
int w = yoloModel.getInputWidth();
int h = yoloModel.getInputHeight();
int gridW = yoloModel.getGridW();
int gridH = yoloModel.getGridH();
ctx.setTextAlign(TextAlignment.CENTER);
for (DetectedObject obj : predictedObjects) {
String cl = yoloModel.getModelClasses()[obj.getPredictedClass()];
double[] xy1 = obj.getTopLeftXY();
double[] xy2 = obj.getBottomRightXY();
int x1 = (int) Math.round(w * xy1[0] / gridW);
int y1 = (int) Math.round(h * xy1[1] / gridH);
int x2 = (int) Math.round(w * xy2[0] / gridW);
int y2 = (int) Math.round(h * xy2[1] / gridH);
int rectW = x2 - x1;
int rectH = y2 - y1;
ctx.setStroke(colors.get(cl));
ctx.strokeRect(x1, y1, rectW, rectH);
ctx.strokeText(cl, x1 + (rectW / 2), y1 - 2);
ctx.setFill(Color.WHITE);
ctx.fillText(cl, x1 + (rectW / 2), y1 - 2);
}
}
private void drawText() {
final double CENTER_X = 0.5 * width;
final double CENTER_Y = 0.5 * height;
final int NO_OF_SECTORS = getNoOfSectors();
Font font = Fonts.latoRegular(0.035 * size);
double radAngle = RadarChartMode.SECTOR == getMode() ? Math.toRadians(180 + angleStep * 0.5) : Math.toRadians(180);
double radAngleStep = Math.toRadians(angleStep);
textGroup.getChildren().clear();
for (int i = 0 ; i < NO_OF_SECTORS ; i++) {
double r = size * 0.48;
double x = CENTER_X - size * 0.015 + (-Math.sin(radAngle) * r);
double y = CENTER_Y + (+Math.cos(radAngle) * r);
Text text = new Text(data.get(i).getName());
text.setFont(font);
text.setFill(data.get(i).getTextColor());
text.setTextOrigin(VPos.CENTER);
text.setTextAlignment(TextAlignment.CENTER);
text.setRotate(Math.toDegrees(radAngle) - 180);
text.setX(x);
text.setY(y);
textGroup.getChildren().add(text);
radAngle += radAngleStep;
}
}
private void setup(String text, String tooltip, int minWidth, int prefWidth, boolean resizable) {
label = new Label(text);
label.setTooltip(new Tooltip(tooltip));
label.setTextAlignment(TextAlignment.CENTER);
setGraphic(label);
if (minWidth != -1) {
setMinWidth(minWidth);
}
if (prefWidth != -1) {
setPrefWidth(prefWidth);
}
setResizable(resizable);
// setOnEditStart(e -> controller.suspend());
// setOnEditCancel(e -> controller.resume());
// setOnEditCommit(e -> controller.resume());
this.text = text;
}
/** Create tracker with restricted position range
* @param restriction Bounds within which tracker will stay
*/
public Tracker(final Rectangle2D restriction)
{
this.restriction = restriction;
tracker.getStyleClass().add("tracker");
handle_top_left = createHandle();
handle_top = createHandle();
handle_top_right = createHandle();
handle_right = createHandle();
handle_bottom_right = createHandle();
handle_bottom = createHandle();
handle_bottom_left = createHandle();
handle_left = createHandle();
locationLabel = createLabel(TextAlignment.LEFT, Pos.TOP_LEFT);
sizeLabel = createLabel(TextAlignment.RIGHT, Pos.BOTTOM_RIGHT);
getChildren().addAll(tracker, handle_top_left, handle_top, handle_top_right,
handle_right, handle_bottom_right,
handle_bottom, handle_bottom_left, handle_left, locationLabel, sizeLabel);
hookEvents();
}
private void drawLabel(StateTransitionEdge pEdge, GraphicsContext pGraphics)
{
adjustLabelFont(pEdge);
Rectangle2D labelBounds = getLabelBounds(pEdge);
double x = labelBounds.getMinX();
double y = labelBounds.getMinY();
Paint oldFill = pGraphics.getFill();
Font oldFont = pGraphics.getFont();
pGraphics.translate(x, y);
pGraphics.setFill(Color.BLACK);
pGraphics.setFont(aFont);
pGraphics.setTextAlign(TextAlignment.CENTER);
pGraphics.fillText(pEdge.getMiddleLabel(), labelBounds.getWidth()/2, 0);
pGraphics.setFill(oldFill);
pGraphics.setFont(oldFont);
pGraphics.translate(-x, -y);
}
private void initGraphics() {
if (Double.compare(getPrefWidth(), 0.0) <= 0 || Double.compare(getPrefHeight(), 0.0) <= 0 || Double.compare(getWidth(), 0.0) <= 0 ||
Double.compare(getHeight(), 0.0) <= 0) {
if (getPrefWidth() > 0 && getPrefHeight() > 0) {
setPrefSize(getPrefWidth(), getPrefHeight());
} else {
setPrefSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
}
}
canvas = new Canvas(getPrefWidth(), getPrefHeight());
ctx = canvas.getGraphicsContext2D();
ctx.setTextAlign(TextAlignment.CENTER);
ctx.setTextBaseline(VPos.CENTER);
getChildren().setAll(canvas);
}
private void configureTab(Tab tab, String title, String iconPath, AnchorPane containerPane, URL resourceURL, EventHandler<Event> onSelectionChangedEvent) {
double imageWidth = 40.0;
ImageView imageView = new ImageView(new Image(iconPath));
imageView.setFitHeight(imageWidth);
imageView.setFitWidth(imageWidth);
Label label = new Label(title);
label.setMaxWidth(tabWidth - 20);
label.setPadding(new Insets(5, 0, 0, 0));
label.setStyle("-fx-text-fill: black; -fx-font-size: 10pt; -fx-font-weight: bold;");
label.setTextAlignment(TextAlignment.CENTER);
BorderPane tabPane = new BorderPane();
tabPane.setRotate(90.0);
tabPane.setMaxWidth(tabWidth);
tabPane.setCenter(imageView);
tabPane.setBottom(label);
tab.setText("");
tab.setGraphic(tabPane);
tab.setOnSelectionChanged(onSelectionChangedEvent);
if (containerPane != null && resourceURL != null) {
try {
Parent contentView = FXMLLoader.load(resourceURL);
containerPane.getChildren().add(contentView);
AnchorPane.setTopAnchor(contentView, 0.0);
AnchorPane.setBottomAnchor(contentView, 0.0);
AnchorPane.setRightAnchor(contentView, 0.0);
AnchorPane.setLeftAnchor(contentView, 0.0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Converts the String property TextAlignment to the appropriate TextAlignment.
* @return
*/
private TextAlignment getAlignment() {
try {
return TextAlignment.valueOf(Property.TEXT_ALIGN.toString().toUpperCase());
} catch(IllegalArgumentException e) {
log.warn(String.format(
"Invalid TEXT_ALIGN property: %s. It should be one of (Case insensitive): Center, Right, Left, or Justify.",
Property.TEXT_ALIGN.toString()
), e
);
// Default to center align.
return TextAlignment.CENTER;
}
}
@Override
public void initializeParts() {
value = new Text(0, ARTBOARD_HEIGHT * 0.5, String.format(FORMAT, getSkinnable().getValue()));
value.getStyleClass().add("value");
value.setTextOrigin(VPos.CENTER);
value.setTextAlignment(TextAlignment.CENTER);
value.setMouseTransparent(true);
value.setWrappingWidth(ARTBOARD_HEIGHT);
value.setBoundsType(TextBoundsType.VISUAL);
thumb = new Circle(ARTBOARD_HEIGHT * 0.5, ARTBOARD_HEIGHT * 0.5, ARTBOARD_HEIGHT * 0.5);
thumb.getStyleClass().add("thumb");
thumbGroup = new Group(thumb, value);
valueBar = new Line();
valueBar.getStyleClass().add("valueBar");
valueBar.setStrokeLineCap(StrokeLineCap.ROUND);
applyCss(valueBar);
strokeWidthFromCSS = valueBar.getStrokeWidth();
scale = new Line();
scale.getStyleClass().add("scale");
scale.setStrokeLineCap(StrokeLineCap.ROUND);
// always needed
drawingPane = new Pane();
}
private Text createCenteredText(double cx, double cy, String styleClass) {
Text text = new Text();
text.getStyleClass().add(styleClass);
text.setTextOrigin(VPos.CENTER);
text.setTextAlignment(TextAlignment.CENTER);
double width = cx > ARTBOARD_WIDTH * 0.5 ? ((ARTBOARD_WIDTH - cx) * 2.0) : cx * 2.0;
text.setWrappingWidth(width);
text.setBoundsType(TextBoundsType.VISUAL);
text.setY(cy);
return text;
}
@Override protected void initGraphics() {
super.initGraphics();
titleText = new Text();
titleText.setFill(tile.getTitleColor());
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
text = new Text(tile.getText());
text.setFill(tile.getUnitColor());
Helper.enableNode(text, tile.isTextVisible());
LocalTime duration = tile.getDuration();
leftText = new Text(Integer.toString(duration.getHour() > 0 ? duration.getHour() : duration.getMinute()));
leftText.setFill(tile.getValueColor());
leftUnit = new Text(duration.getHour() > 0 ? "h" : "m");
leftUnit.setFill(tile.getValueColor());
rightText = new Text(Integer.toString(duration.getHour() > 0 ? duration.getMinute() : duration.getSecond()));
rightText.setFill(tile.getValueColor());
rightUnit = new Text(duration.getHour() > 0 ? "m" : "s");
rightUnit.setFill(tile.getValueColor());
timeText = new TextFlow(leftText, leftUnit, rightText, rightUnit);
timeText.setTextAlignment(TextAlignment.RIGHT);
timeText.setPrefWidth(PREFERRED_WIDTH * 0.9);
description = new Label(tile.getDescription());
description.setAlignment(Pos.TOP_RIGHT);
description.setWrapText(true);
description.setTextFill(tile.getTextColor());
Helper.enableNode(description, !tile.getDescription().isEmpty());
getPane().getChildren().addAll(titleText, text, timeText, description);
}
private void updateBackgroundText() {
// Setup the semitransparent background timeText
backgroundTimeText.setTextOrigin(VPos.BASELINE);
backgroundTimeText.setTextAlignment(TextAlignment.RIGHT);
backgroundSecondText.setTextOrigin(VPos.BASELINE);
backgroundSecondText.setTextAlignment(TextAlignment.RIGHT);
// Setup the semitransparent background timeText
// Width of one time segment
String backgroundSegment = "_";
switch(clock.getLcdFont()) {
case LCD : backgroundSegment = "8"; break;
case DIGITAL :
case DIGITAL_BOLD:
case ELEKTRA : backgroundSegment = "_"; break;
}
// Add segments to background timeText
backgroundTextBuilder.setLength(0);
backgroundTextBuilder.append(backgroundSegment);
backgroundTextBuilder.append(backgroundSegment);
backgroundTextBuilder.append(":");
backgroundTextBuilder.append(backgroundSegment);
backgroundTextBuilder.append(backgroundSegment);
backgroundTimeText.setText(backgroundTextBuilder.toString());
backgroundSecondText.setText(backgroundSegment + backgroundSegment);
}
private HBox buildFooter() {
Button closeButton = new Button(DefaultI18nContext.getInstance().i18n("Close"));
closeButton.getStyleClass().addAll(Style.BUTTON.css());
closeButton.setTextAlignment(TextAlignment.CENTER);
closeButton.setOnAction(e -> eventStudio().broadcast(activeteCurrentModule()));
HBox footer = new HBox(closeButton);
footer.getStyleClass().addAll(Style.CLOSE_FOOTER.css());
return footer;
}
public void addElement(String text, String cssClass, int height) {
WikiLabel label = new WikiLabel(text).setWrapped(true);
label.setTextAlignment(TextAlignment.LEFT);
label.setClass(cssClass);
VBox box = new VBox();
box.getChildren().add(label);
box.setMinHeight(height);
addElement(box);
}
private TextAlignment alignmentFromIndex(int index) {
switch (index) {
case -1:
return TextAlignment.LEFT;
case 0:
return TextAlignment.CENTER;
case 1:
return TextAlignment.RIGHT;
}
return TextAlignment.CENTER;
}
private void initAboutContent() {
final Text appNameText = getAppNameText();
final Text appVersionText = getAppVersionText();
final Text authorText = getAuthorText();
textFlow.setTextAlignment(TextAlignment.CENTER);
textFlow.getChildren().addAll(appNameText, new Text("\n"),
appVersionText, new Text("\n\n"),
authorText, new Text("\n\n"),
new Text("Source: " ), sourceCodePage);
}
@Override
public void drawText(String s, double x, double y, boolean centerHorz, boolean centerVert)
{
ctx.setTextAlign(centerHorz ? TextAlignment.CENTER : TextAlignment.LEFT);
ctx.setTextBaseline(centerVert ? VPos.CENTER : VPos.TOP);
ctx.strokeText(s, x, y);
}
private Text createNumber(String number, double layoutX, double layoutY) {
Text text = new Text(number);
text.setLayoutX(layoutX);
text.setLayoutY(layoutY);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(FILL_COLOR);
text.setFont(NUMBER_FONT);
return text;
}
private Node addSeparator(String name) {
Separator separator = new Separator();
separator.setPadding(new Insets(8, 0, 0, 0));
HBox.setHgrow(separator, Priority.ALWAYS);
Text text = new Text(name);
text.setTextAlignment(TextAlignment.CENTER);
HBox hBox = new HBox(text, separator);
HBox.setHgrow(hBox, Priority.ALWAYS);
return hBox;
}
private Text createNumber(String number, double layoutX, double layoutY) {
Text text = new Text(number);
text.setLayoutX(layoutX);
text.setLayoutY(layoutY);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(FILL_COLOR);
text.setFont(NUMBER_FONT);
return text;
}