下面列出了怎么用javafx.scene.text.FontWeight的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateTemplateObjectHelp(final String itemName, final String itemDescription) {
templateObjectHelp.getChildren().clear();
final Text objectNameText = new Text(itemName + ":\n");
objectNameText.setFont(Font.font(FONT_FAMILY, FontWeight.NORMAL, 16));
objectNameText.setFill(Color.web(TEXT_COLOR));
templateObjectHelp.getChildren().add(objectNameText);
String objDescrip = " " + itemDescription + "\n";
final Text objectDescripText = new Text(objDescrip + "\n");
objectDescripText.setFont(Font.font(FONT_FAMILY, FontWeight.BOLD, 12));
objectDescripText.setFill(Color.WHITE);
objectDescripText.setFontSmoothingType(FontSmoothingType.LCD);
templateObjectHelp.getChildren().add(objectDescripText);
}
private void updateAttributeDescriptionHelp(String attributeName, String attributeDescription, String attributeType) {
Platform.runLater(() -> {
attributeDescriptionHelp.getChildren().clear();
final Text attributeHeadingText = new Text("Attribute to set:\n");
attributeHeadingText.setFont(Font.font(FONT_FAMILY, FontWeight.NORMAL, 16));
attributeHeadingText.setFill(Color.web(TEXT_COLOR));
final Text attributeLabelText = new Text(attributeName);
attributeLabelText.setFont(Font.font(FONT_FAMILY, FontWeight.BOLD, 12));
attributeLabelText.setFill(Color.WHITE);
final Text attributeSpacingText = new Text(" - ");
attributeSpacingText.setFont(Font.font(FONT_FAMILY, FontWeight.NORMAL, 12));
attributeSpacingText.setFill(Color.WHITE);
final Text attributeDescriptionText = new Text(attributeDescription);
attributeDescriptionText.setFont(Font.font(FONT_FAMILY, FontPosture.ITALIC, 12));
attributeDescriptionText.setFill(Color.WHITE);
final Text attributeTypeText = new Text(" (type: " + attributeType + ")");
attributeTypeText.setFont(Font.font(FONT_FAMILY, FontWeight.NORMAL, 12));
attributeTypeText.setFill(Color.WHITE);
attributeDescriptionHelp.getChildren().addAll(attributeHeadingText, attributeLabelText, attributeSpacingText, attributeDescriptionText, attributeTypeText);
});
}
public static Node createIconContent() {
Text htmlStart = new Text("<html>");
Text htmlEnd = new Text("</html>");
htmlStart.setFont(Font.font(null, FontWeight.BOLD, 20));
htmlStart.setStyle("-fx-font-size: 20px;");
htmlStart.setTextOrigin(VPos.TOP);
htmlStart.setLayoutY(11);
htmlStart.setLayoutX(20);
htmlEnd.setFont(Font.font(null, FontWeight.BOLD, 20));
htmlEnd.setStyle("-fx-font-size: 20px;");
htmlEnd.setTextOrigin(VPos.TOP);
htmlEnd.setLayoutY(31);
htmlEnd.setLayoutX(20);
return new Group(htmlStart, htmlEnd);
}
public DropShadowSample() {
Text sample = new Text(0,40,"DropShadow Effect");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,36));
final DropShadow dropShadow = new DropShadow();
sample.setEffect(dropShadow);
getChildren().add(sample);
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Radius", dropShadow.radiusProperty(), 0d, 20d),
new SimplePropertySheet.PropDesc("Offset X", dropShadow.offsetXProperty(), -10d, 10d),
new SimplePropertySheet.PropDesc("Offset Y", dropShadow.offsetYProperty(), -10d, 10d),
new SimplePropertySheet.PropDesc("Spread", dropShadow.spreadProperty(), 0d, 1d),
new SimplePropertySheet.PropDesc("Color", dropShadow.colorProperty())
);
// END REMOVE ME
}
public static Node createIconContent() {
Text text = new Text("abc");
text.setTextOrigin(VPos.TOP);
text.setLayoutX(10);
text.setLayoutY(11);
text.setFill(Color.BLACK);
text.setOpacity(0.5);
text.setFont(Font.font(null, FontWeight.BOLD, 20));
text.setStyle("-fx-font-size: 20px;");
Text text2 = new Text("abc");
text2.setTextOrigin(VPos.TOP);
text2.setLayoutX(28);
text2.setLayoutY(51);
text2.setFill(Color.BLACK);
text2.setFont(javafx.scene.text.Font.font(null, FontWeight.BOLD, 20));
text2.setStyle("-fx-font-size: 20px;");
Line line = new Line(30, 32, 45, 57);
line.setStroke(Color.DARKMAGENTA);
return new javafx.scene.Group(text, line, text2);
}
private void drawGraphics() {
graphics.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
graphics.drawImage(image, 0, 0);
if (annotations.size() > 0) {
for (int i = 0; i < annotations.size(); i++) {
Annotation annotationFX = annotations.get(i);
double x = annotationFX.getX();
double y = annotationFX.getY();
graphics.setFill(ANNOTATION_COLOR);
graphics.fillRect(x, y, annotationFX.getWidth(), annotationFX.getHeight());
graphics.setFill(Color.RED);
graphics.fillArc(x - 25, y - 25, 50, 50, 270, 90, ArcType.ROUND);
graphics.setFill(Color.WHITE);
graphics.setFont(Font.font(null, FontWeight.EXTRA_BOLD, 14));
if (i > 8) {
graphics.fillText(Integer.toString(i + 1), x + 5, y + 15);
} else {
graphics.fillText(Integer.toString(i + 1), x + 5, y + 15);
}
}
}
}
private void setLabel() {
String text;
if (failuresList == null) {
text = "";
} else if (failuresList.size() > 0) {
String errorString = failuresList.size() == 1 ? " error" : " errors";
text = failuresList.size() + errorString;
tableLabel.setTextFill(Color.RED);
} else {
text = "No Errors";
tableLabel.setTextFill(Color.GREEN);
}
tableLabel.setText(text);
tableLabel.setFont(Font.font(tableLabel.getFont().toString(), FontWeight.BOLD, 12));
tableLabel.setLabelFor(resultTable);
}
public StatusBar() {
setId("status-bar");
msgLabel = createLabel("");
extraLabel = createLabel(" ");
extraLabel.setFont(Font.font("System", FontPosture.ITALIC, 12.0));
fixtureLabel = createLabel(" ");
fixtureLabel.setFont(Font.font("System", FontWeight.BOLD, 12.0));
rowLabel = createLabel(" ");
columnLabel = createLabel(" ");
insertLabel = createLabel(" ");
Region region = new Region();
getChildren().addAll(msgLabel, region, createSeparator(), extraLabel, createSeparator(), fixtureLabel, createSeparator(),
rowLabel, createSeparator(), columnLabel, createSeparator(), insertLabel, createSeparator());
HBox.setHgrow(region, Priority.ALWAYS);
getStylesheets().add(ModalDialog.class.getClassLoader().getResource("net/sourceforge/marathon/fx/api/css/marathon.css")
.toExternalForm());
}
public static Node createIconContent() {
Text htmlStart = new Text("<html>");
Text htmlEnd = new Text("</html>");
htmlStart.setFont(Font.font(null, FontWeight.BOLD, 20));
htmlStart.setStyle("-fx-font-size: 20px;");
htmlStart.setTextOrigin(VPos.TOP);
htmlStart.setLayoutY(11);
htmlStart.setLayoutX(20);
htmlEnd.setFont(Font.font(null, FontWeight.BOLD, 20));
htmlEnd.setStyle("-fx-font-size: 20px;");
htmlEnd.setTextOrigin(VPos.TOP);
htmlEnd.setLayoutY(31);
htmlEnd.setLayoutX(20);
return new Group(htmlStart, htmlEnd);
}
public DropShadowSample() {
Text sample = new Text(0,40,"DropShadow Effect");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,36));
final DropShadow dropShadow = new DropShadow();
sample.setEffect(dropShadow);
getChildren().add(sample);
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Radius", dropShadow.radiusProperty(), 0d, 20d),
new SimplePropertySheet.PropDesc("Offset X", dropShadow.offsetXProperty(), -10d, 10d),
new SimplePropertySheet.PropDesc("Offset Y", dropShadow.offsetYProperty(), -10d, 10d),
new SimplePropertySheet.PropDesc("Spread", dropShadow.spreadProperty(), 0d, 1d),
new SimplePropertySheet.PropDesc("Color", dropShadow.colorProperty())
);
// END REMOVE ME
}
public static Node createIconContent() {
Text text = new Text("abc");
text.setTextOrigin(VPos.TOP);
text.setLayoutX(10);
text.setLayoutY(11);
text.setFill(Color.BLACK);
text.setOpacity(0.5);
text.setFont(Font.font(null, FontWeight.BOLD, 20));
text.setStyle("-fx-font-size: 20px;");
Text text2 = new Text("abc");
text2.setTextOrigin(VPos.TOP);
text2.setLayoutX(28);
text2.setLayoutY(51);
text2.setFill(Color.BLACK);
text2.setFont(javafx.scene.text.Font.font(null, FontWeight.BOLD, 20));
text2.setStyle("-fx-font-size: 20px;");
Line line = new Line(30, 32, 45, 57);
line.setStroke(Color.DARKMAGENTA);
return new javafx.scene.Group(text, line, text2);
}
private Parent buildUI() {
fc = new FileChooser();
fc.getExtensionFilters().clear();
ExtensionFilter jpgFilter = new ExtensionFilter("JPG, JPEG images", "*.jpg", "*.jpeg", "*.JPG", ".JPEG");
fc.getExtensionFilters().add(jpgFilter);
fc.setSelectedExtensionFilter(jpgFilter);
fc.setTitle("Select a JPG image");
lstLabels = new ListView<>();
lstLabels.setPrefHeight(200);
Button btnLoad = new Button("Select an Image");
btnLoad.setOnAction(e -> validateUrlAndLoadImg());
HBox hbBottom = new HBox(10, btnLoad);
hbBottom.setAlignment(Pos.CENTER);
loadedImage = new ImageView();
loadedImage.setFitWidth(300);
loadedImage.setFitHeight(250);
Label lblTitle = new Label("Label image using TensorFlow");
lblTitle.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, 40));
VBox root = new VBox(10,lblTitle, loadedImage, new Label("Results:"), lstLabels, hbBottom);
root.setAlignment(Pos.TOP_CENTER);
return root;
}
public HistoryListItemNode(@NotNull HistoryListItem item) {
super(5);
final VBox vboxTitle = new VBox(5);
final Label lblTitle = new Label(item.getItemTitle());
lblTitle.setFont(Font.font(15));
vboxTitle.getChildren().add(lblTitle);
Font subInfoLabelFont = Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, 10);
Font subInfoTextFont = Font.font(subInfoLabelFont.getSize());
for (HistoryListItemSubInfo subInfo : item.getSubInfo()) {
final Label lbl = new Label(subInfo.getLabel());
lbl.setFont(subInfoLabelFont);
final Label lblInfo = new Label(subInfo.getInfo());
lblInfo.setFont(subInfoTextFont);
vboxTitle.getChildren().add(new HBox(5, lbl, lblInfo));
}
getChildren().add(vboxTitle);
final Label lblMainInfo = new Label(item.getInformation());
lblMainInfo.setWrapText(true);
getChildren().add(lblMainInfo);
}
private void draw() {
Canvas canvas = new Canvas(scale, scale);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.TRANSPARENT);
gc.fillRect(0, 0, scale, scale);
gc.setFill(background);
gc.fillRect(0, 0, scale, scale);
gc.setFill(Color.WHITE);
gc.setTextAlign(TextAlignment.CENTER);
gc.setTextBaseline(VPos.CENTER);
gc.setFont(Font.font("Tahoma", FontWeight.SEMI_BOLD, scale * 0.6));
gc.fillText(String.valueOf(character), Math.round(scale / 2.0), Math.round(scale / 2.0));
Platform.runLater(() -> canvas.snapshot(null, this));
}
/**
* Called internally to configure size, position and style of the overlay.
*/
private void configureOverlay() {
dimmer = new StackPane();
dimmer.setManaged(false);
dimmerMessage = new Label(DEFAULT_OVERLAY_MESSAGE);
dimmerMessage.setFont(Font.font(dimmerMessage.getFont().getFamily(), FontWeight.BOLD, 18));
dimmerMessage.setTextFill(Color.WHITE);
dimmer.getChildren().add(dimmerMessage);
dimmer.setStyle("-fx-background-color: rgba(0, 0, 0, 0.6);");
getChildren().add(dimmer);
layoutBoundsProperty().addListener((v, o, n) -> {
Platform.runLater(() -> {
if(MapPane.this.getScene().getWindow() == null) return;
Point2D mapPoint = contentPane.localToParent(0, 0);
double topHeight = contentPane.getTop() == null ? 0 : contentPane.getTop().getLayoutBounds().getHeight();
dimmer.resizeRelocate(mapPoint.getX(), mapPoint.getY() + topHeight,
contentPane.getWidth(), contentPane.getHeight() - topHeight);
});
});
}
@Override
public void start(Stage theStage)
{
theStage.setTitle( "Canvas Example" );
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 400, 200 );
root.getChildren().add( canvas );
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill( Color.RED );
gc.setStroke( Color.BLACK );
gc.setLineWidth(2);
Font theFont = Font.font( "Times New Roman", FontWeight.BOLD, 48 );
gc.setFont( theFont );
gc.fillText( "Hello, World!", 60, 50 );
gc.strokeText( "Hello, World!", 60, 50 );
Image earth = new Image( "earth.png" );
gc.drawImage( earth, 180, 100 );
theStage.show();
}
/**
* Sets the font to be used for drawing text.
*
* @param font the font ({@code null} is permitted but ignored).
*
* @see #getFont()
*/
@Override
public void setFont(Font font) {
if (font == null) {
return;
}
this.font = font;
FontWeight weight = font.isBold() ? FontWeight.BOLD : FontWeight.NORMAL;
FontPosture posture = font.isItalic()
? FontPosture.ITALIC : FontPosture.REGULAR;
this.gc.setFont(javafx.scene.text.Font.font(font.getFamily(),
weight, posture, font.getSize()));
}
@Override
public void loadValueFromXML(Element xmlElement) {
NodeList colorNodes = xmlElement.getElementsByTagName("color");
if (colorNodes.getLength() != 1)
return;
NodeList fontNodes = xmlElement.getElementsByTagName("font");
if (fontNodes.getLength() != 1)
return;
String scolor = colorNodes.item(0).getTextContent();
String sfont = fontNodes.item(0).getTextContent();
try {
// font
String[] s = sfont.split(",");
String name = s[0];
FontWeight style = FontWeight.findByName(s[1]);
double size = Double.valueOf(s[2]);
Font f = Font.font(name, style, size);
// color
s = scolor.split(",");
double r = Double.valueOf(s[0]);
double g = Double.valueOf(s[1]);
double b = Double.valueOf(s[2]);
double a = Double.valueOf(s[3]);
Color c = new Color(r, g, b, a);
value = new FontSpecs(c, f);
} catch (Exception e) {
e.printStackTrace();
}
}
public CopiedButton() {
setText("COPIED");
setPrefHeight(20);
// white text colour
setTextFill(Color.WHITE);
setFont(Font.font("Arial Black", FontWeight.BOLD, 10));
// blue background
setBackground(new Background(new BackgroundFill(UIUtils.LIGHT_BLUE, RADII, Insets.EMPTY)));
}
public LinkedHashMap<Object, Object> getYAMLData(){
LinkedHashMap<Object, Object> data = new LinkedHashMap<>();
data.put("color", color.get().toString());
data.put("font", font.get().getFamily());
data.put("size", font.get().getSize());
data.put("bold", FontUtils.getFontWeight(font.get()) == FontWeight.BOLD);
data.put("italic", FontUtils.getFontPosture(font.get()) == FontPosture.ITALIC);
data.put("uses", uses);
data.put("date", creationDate);
data.put("text", text);
return data;
}
public static FontWeight getFontWeight(Font font) {
String[] style = font.getStyle().split(" ");
if(style.length >= 1){
if(style[0].equals("Bold")){
return FontWeight.BOLD;
}
}
return FontWeight.NORMAL;
}
private void trigger(Event t) {
BibleSearchTreeView tv = (BibleSearchTreeView) t.getSource();
TreeItem<BibleInterface> ti = tv.getSelectionModel().getSelectedItem();
if (ti != null) {
if (ti.getValue() instanceof BibleVerse) {
textPane.getChildren().clear();
BibleChapter chapter = (BibleChapter) ti.getValue().getParent();
BibleVerse[] verses = chapter.getVerses();
BibleVerse selected = (BibleVerse) ti.getValue();
int x = selected.getNum() - 1;
for (int i = 0; i < verses.length; i++) {
Text text = new Text(verses[i].toString() + " ");
text.getStyleClass().add("text");
if (i == x) {
text.setFont(Font.font("Sans", FontWeight.BOLD, 14));
} else {
text.setFont(Font.font("Sans", 14));
}
textPane.getChildren().add(text);
text.wrappingWidthProperty().bind(sp.widthProperty().subtract(20)); //-20 to account for scroll bar width
}
} else {
ti.setExpanded(!ti.isExpanded());
}
} else {
tv.selectionModelProperty().get().selectFirst();
}
}
protected BorderPane createCalendarMonthLabelPane() {
monthYearLabel = new Label();
monthYearLabel.getStyleClass().add(SPINNER_LABEL);
monthYearLabel.setFont(Font.font(ROBOTO, FontWeight.BOLD, 13));
monthYearLabel.setTextFill(DEFAULT_COLOR);
BorderPane monthContainer = new BorderPane();
monthContainer.setMinHeight(50);
monthContainer.setCenter(monthYearLabel);
monthContainer.setPadding(new Insets(2, 12, 2, 12));
return monthContainer;
}
public void setFontSize(double pickFontSize) {
setFont(Font.font(theme.getFont().getFamily(),
theme.isBold() ? FontWeight.BOLD : FontWeight.NORMAL,
theme.isItalic() ? FontPosture.ITALIC : FontPosture.REGULAR,
pickFontSize));
}
/**
* Get the font represented on this dialog.
*
* @return the font.
*/
public SerializableFont getTranslateFont() {
return new SerializableFont(Font.font(fontSelection.getSelectionModel().getSelectedItem(),
boldButton.isSelected() ? FontWeight.BOLD : FontWeight.NORMAL,
italicButton.isSelected() ? FontPosture.ITALIC : FontPosture.REGULAR,
QueleaProperties.get().getMaxFontSize()));
}
/**
* Determine the largest font size we can safely use for every section of a
* text displayable.
* <p>
* @param displayable the displayable to check.
* @return the font size to use
*/
private double getUniformFontSize(TextDisplayable displayable) {
if (!QueleaProperties.get().getUseUniformFontSize()) {
return -1;
}
Font font = theme.getFont();
font = Font.font(font.getName(),
theme.isBold() ? FontWeight.BOLD : FontWeight.NORMAL,
theme.isItalic() ? FontPosture.ITALIC : FontPosture.REGULAR,
QueleaProperties.get().getMaxFontSize());
double fontSize = Double.POSITIVE_INFINITY;
for (TextSection section : displayable.getSections()) {
String[] textArr;
if (QueleaProperties.get().getShowChords()) {
textArr = section.getText(true, false);
} else {
textArr = section.getText(false, false);
}
double newSize;
List<LyricLine> newText;
if (displayable instanceof BiblePassage) {
newText = new ArrayList<>();
for (String str : section.getText(false, false)) {
for (String line : str.split("\n")) {
newText.add(new LyricLine(line));
}
}
} else {
newText = sanctifyText(textArr);
}
newSize = pickFontSize(font, newText, getCanvas().getWidth() * 0.92, getCanvas().getHeight() * 0.9);
if (newSize < fontSize) {
fontSize = newSize;
}
}
if (fontSize == Double.POSITIVE_INFINITY) {
fontSize = -1;
}
return fontSize;
}
/**
* Sets the font to be used for drawing text.
*
* @param font the font ({@code null} is permitted but ignored).
*
* @see #getFont()
*/
@Override
public void setFont(Font font) {
if (font == null) {
return;
}
this.font = font;
FontWeight weight = font.isBold() ? FontWeight.BOLD : FontWeight.NORMAL;
FontPosture posture = font.isItalic()
? FontPosture.ITALIC : FontPosture.REGULAR;
this.gc.setFont(javafx.scene.text.Font.font(font.getFamily(),
weight, posture, font.getSize()));
}
private void updateFonts() {
digitalFontSizeFactor = 1.0;
switch(getSkinnable().getValueFont()) {
case LCD:
valueFont = Font.font("Digital-7", (0.6 * height));
digitalFontSizeFactor = 1.4;
break;
case DIGITAL:
valueFont = Font.font("Digital Readout Upright", (0.5833333333 * height));
break;
case DIGITAL_BOLD:
valueFont = Font.font("Digital Readout Thick Upright", (0.5833333333 * height));
break;
case ELEKTRA:
valueFont = Font.font("Elektra", (0.62 * height));
break;
case STANDARD:
default:
valueFont = Font.font("Open Sans", FontWeight.NORMAL, (0.5 * height));
break;
}
backgroundText.setFont(valueFont);
backgroundText.setOpacity((Lcd.LcdFont.LCD == getSkinnable().getValueFont() ||
Lcd.LcdFont.DIGITAL == getSkinnable().getValueFont() ||
Lcd.LcdFont.DIGITAL_BOLD == getSkinnable().getValueFont() ||
Lcd.LcdFont.ELEKTRA == getSkinnable().getValueFont()) &&
!getSkinnable().isTextMode() ? 1 : 0);
text.setFont(valueFont);
unitFont = Font.font(getSkinnable().getUnitFont(), FontWeight.NORMAL, (0.26 * height));
titleFont = Font.font(getSkinnable().getTitleFont(), FontWeight.BOLD, (0.1666666667 * height));
smallFont = Font.font(getSkinnable().getSmallFont(), FontWeight.NORMAL, (0.1666666667 * height));
}
public static Node createIconContent() {
Text sample = new Text("FX");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,80));
sample.setStyle("-fx-font-size: 80px;");
sample.setFill(Color.web("#aaaaaa"));
final InnerShadow innerShadow = new InnerShadow();
innerShadow.setRadius(4);
innerShadow.setOffsetX(1);
innerShadow.setOffsetY(1);
innerShadow.setColor(Color.web("#333333"));
sample.setEffect(innerShadow);
return sample;
}
public static Node createIconContent() {
Text sample = new Text("FX");
sample.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD,80));
sample.setStyle("-fx-font-size: 80px;");
sample.setFill(Color.web("#333333"));
final GaussianBlur GaussianBlur = new GaussianBlur();
GaussianBlur.setRadius(15);
sample.setEffect(GaussianBlur);
return sample;
}