Javafx旋转标签问题 [英] Javafx rotate Label issue

查看:113
本文介绍了Javafx旋转标签问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 当我尝试将标签旋转270 label.setRotate(270)时遇到问题.
  • 但标签文本消失.
  • I have an issue when i try to rotate the label by 270 label.setRotate(270).
  • But label text disappear .

  • 那是代码示例.

  • That is the code sample.

LineChart chart = new LineChart(new CategoryAxis(), new NumberAxis());
chart.setLegendVisible(false); 

/**
 * Grid pane which contain the charts and vertical label to indicate for the row title
 */
GridPane root = new GridPane();
root.add(chart, 0, 0);

/**
 * Want to add vertical label to refer to the row title.
 */
Label label = new Label("Row Title");
label.setRotate(270);
// label.setMinWidth(200); 

root.add(label, 1, 0);

更新

  • 当我尝试setMinWidth(200)时,出现另一个问题.
  • When i try to setMinWidth(200) another issue appear.

推荐答案

问题是,向标签添加旋转不会影响其布局范围.就布局而言,标签仍然是水平控件.

The problem is that adding a rotation to the label doesn't affect its layout bounds. The label is, as far as layout is concerned, still a horizontal control.

将其包装在Group中将解决大多数问题,因为该小组将考虑转换.但是,标签中的文本自动换行之类的功能将不再起作用.

Wrapping it in a Group will solve most issues, as the group will take the transform into account. However, things like automatic wrapping of text in the label will no longer function.

如果还需要在垂直标签中自动换行,那么我们需要创建自己的VerticalLabel类,该类在垂直显示时重复使用Label进行的复杂换行计算.

If text wrapping in a vertical label is also desired, then we need to create our own VerticalLabel class that re-uses the complex wrapping calculations done by Label while showing it vertically.

此功能实际上应该是Label类本身的一部分,以得到正确的支持,但是可以通过新控件解决该功能.

This feature should really be part of the Label class itself to be supported properly, but it is possible to work-around it with a new control.

这是代码:

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.VerticalDirection;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.OverrunStyle;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.scene.transform.Rotate;

public class VerticalLabel extends Pane {
  private final InternalLabel label = new InternalLabel();
  private final VerticalDirection direction;

  public VerticalLabel(VerticalDirection direction) {
    this.direction = direction;
    this.label.getTransforms().add(new Rotate(direction == VerticalDirection.DOWN ? 90 : -90, 0, 0));
    this.label.setManaged(false);
    this.getChildren().add(label);
  }

  // Content bias is Vertical if text wrapping is active: 

  @Override
  public Orientation getContentBias() {
    return label.isWrapText() ? Orientation.VERTICAL : null;
  }

  @Override
  protected void layoutChildren() {
    super.layoutChildren();

    label.resizeRelocate(direction == VerticalDirection.DOWN ? getWidth() : 0, direction == VerticalDirection.DOWN ? 0 : getHeight(), getHeight(), getWidth());
  }

  // Compute methods, with width and height swapped around:

  @Override
  protected double computePrefWidth(double height) {
    return label.computePrefHeight(height);
  }

  @Override
  protected double computePrefHeight(double width) {
    return label.computePrefWidth(width);
  }

  @Override
  protected double computeMaxWidth(double height) {
    return label.computeMaxHeight(height);
  }

  @Override
  protected double computeMaxHeight(double width) {
    return label.computeMaxWidth(width);
  }

  @Override
  protected double computeMinWidth(double height) {
    return label.computeMinHeight(height);
  }

  @Override
  protected double computeMinHeight(double width) {
    return label.computeMinWidth(width);
  }

  // Delegate methods:

  public ObjectProperty<Node> labelForProperty() {
    return label.labelForProperty();
  }

  public final StringProperty textProperty() {
    return label.textProperty();
  }

  public final String getText() {
    return label.getText();
  }

  public void setText(String text) {
    label.setText(text);
  }

  public final void setLabelFor(Node value) {
    label.setLabelFor(value);
  }

  public final Node getLabelFor() {
    return label.getLabelFor();
  }

  public final ObjectProperty<TextAlignment> textAlignmentProperty() {
    return label.textAlignmentProperty();
  }

  public final void setTextAlignment(TextAlignment value) {
    label.setTextAlignment(value);
  }

  public final TextAlignment getTextAlignment() {
    return label.getTextAlignment();
  }

  public final ObjectProperty<OverrunStyle> textOverrunProperty() {
    return label.textOverrunProperty();
  }

  public final void setTextOverrun(OverrunStyle value) {
    label.setTextOverrun(value);
  }

  public final OverrunStyle getTextOverrun() {
    return label.getTextOverrun();
  }

  public final StringProperty ellipsisStringProperty() {
    return label.ellipsisStringProperty();
  }

  public final void setEllipsisString(String value) {
    label.setEllipsisString(value);
  }

  public final String getEllipsisString() {
    return label.getEllipsisString();
  }

  public final BooleanProperty wrapTextProperty() {
    return label.wrapTextProperty();
  }

  public final boolean isWrapText() {
    return label.isWrapText();
  }

  public void setWrapText(boolean wrapText) {
    label.setWrapText(wrapText);
  }

  public final ObjectProperty<Font> fontProperty() {
    return label.fontProperty();
  }

  public final void setFont(Font value) {
    label.setFont(value);
  }

  public final Font getFont() {
    return label.getFont();
  }

  public final ObjectProperty<Node> graphicProperty() {
    return label.graphicProperty();
  }

  public final void setGraphic(Node value) {
    label.setGraphic(value);
  }

  public final Node getGraphic() {
    return label.getGraphic();
  }

  public final ObjectProperty<ContextMenu> contextMenuProperty() {
    return label.contextMenuProperty();
  }

  public final void setContextMenu(ContextMenu value) {
    label.setContextMenu(value);
  }

  public final BooleanProperty underlineProperty() {
    return label.underlineProperty();
  }

  public final void setUnderline(boolean value) {
    label.setUnderline(value);
  }

  public final DoubleProperty lineSpacingProperty() {
    return label.lineSpacingProperty();
  }

  public final void setLineSpacing(double value) {
    label.setLineSpacing(value);
  }

  public final double getLineSpacing() {
    return label.getLineSpacing();
  }

  public final ObjectProperty<ContentDisplay> contentDisplayProperty() {
    return label.contentDisplayProperty();
  }

  public final void setContentDisplay(ContentDisplay value) {
    label.setContentDisplay(value);
  }

  public final ReadOnlyObjectProperty<Insets> labelPaddingProperty() {
    return label.labelPaddingProperty();
  }

  public final Insets getLabelPadding() {
    return label.getLabelPadding();
  }

  public final DoubleProperty graphicTextGapProperty() {
    return label.graphicTextGapProperty();
  }

  public final void setGraphicTextGap(double value) {
    label.setGraphicTextGap(value);
  }

  public final double getGraphicTextGap() {
    return label.getGraphicTextGap();
  }

  public final void setTextFill(Paint value) {
    label.setTextFill(value);
  }

  public final Paint getTextFill() {
    return label.getTextFill();
  }

  public final ObjectProperty<Paint> textFillProperty() {
    return label.textFillProperty();
  }

  public final BooleanProperty mnemonicParsingProperty() {
    return label.mnemonicParsingProperty();
  }

  /**
   * Inner class to make compute(...) methods accessible.
   */
  private class InternalLabel extends Label {
    @Override
    public double computePrefWidth(double height) {
      return super.computePrefWidth(height);
    }

    @Override
    public double computePrefHeight(double width) {
      return super.computePrefHeight(width);
    }

    @Override
    public double computeMaxWidth(double height) {
      return super.computeMaxWidth(height);
    }

    @Override
    public double computeMaxHeight(double width) {
      return super.computeMaxHeight(width);
    }

    @Override
    public double computeMinWidth(double height) {
      return super.computeMinWidth(height);
    }

    @Override
    public double computeMinHeight(double width) {
      return super.computeMinHeight(width);
    }
  }
}

这篇关于Javafx旋转标签问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆