我有一个带文字的ScrollPane,我需要更新颜色&字体。颜色选择器工作正常。我的问题是ChoiceBox用于选择字体。当我第一次选择字体时它会改变,但是当我选择另一个选项时,它不会更新字体。这是代码:
package helloearthrisemain;
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;
public class HelloEarthRiseMain extends Application {
@Override
public void start(Stage stage) {
String message
= "Earthrise at Christmas: "
+ "[Forty] years ago this Christmas, a turbulent world "
+ "looked to the heavens for a unique view of our home "
+ "planet. This photo of Earthrise over the lunar horizon "
+ "was taken by the Apollo 8 crew in December 1968, showing "
+ "Earth for the first time as it appears from deep space. "
+ "Astronauts Frank Borman, Jim Lovell and William Anders "
+ "had become the first humans to leave Earth orbit, "
+ "entering lunar orbit on Christmas Eve. In a historic live "
+ "broadcast that night, the crew took turns reading from "
+ "the Book of Genesis, closing with a holiday wish from "
+ "Commander Borman: \"We close with good night, good luck, "
+ "a Merry Christmas, and God bless all of you -- all of "
+ "you on the good Earth.\"";
//Color picker
ColorPicker colorPicker = new ColorPicker();
colorPicker.setValue(Color.CORAL);
colorPicker.setLayoutX(400);
colorPicker.setLayoutY(200);
//List of fonts
ObservableList fonts = FXCollections.observableArrayList(
Font.getFamilies());
//Select font
ChoiceBox selectFont;
selectFont = new ChoiceBox();
selectFont.setValue("SansSerif");
selectFont.setLayoutX(600);
selectFont.setLayoutY(200);
selectFont.setItems(fonts);
// Reference to the Text
Text textRef = new Text(message);
textRef.setLayoutY(100);
textRef.setTextOrigin(VPos.TOP);
textRef.setTextAlignment(TextAlignment.JUSTIFY);
textRef.setWrappingWidth(600);
textRef.setFill(colorPicker.getValue());
textRef.setFont(Font.font("SansSerif", FontWeight.BOLD, 24));
//Action for selecting text color.
colorPicker.setOnAction(new EventHandler() {
public void handle(Event t) {
textRef.setFill(colorPicker.getValue());
}
});
选择字体的操作。我相信这就是问题所在。 它会在第一次选择时改变,但在我做另一次选择时则不会改变 选择。
selectFont.getSelectionModel().selectedIndexProperty()
.addListener(new ChangeListener<Number>() {
public void changed(ObservableValue ov, Number value, Number new_value) {
textRef.setFont(Font.font(selectFont.getAccessibleText(), FontWeight.BOLD, 24));
}
});
//I tried this as well. This code did not work either.
/*
selectFont.setOnAction(new EventHandler() {
public void handle(Event u) {
textRef.setFont(Font.font(selectFont.getAccessibleText(), FontWeight.BOLD, 24));
}
});
*/
// Provides the animated scrolling behavior for the text
TranslateTransition transTransition = new TranslateTransition(new Duration(75000), textRef);
transTransition.setToY(-820);
transTransition.setInterpolator(Interpolator.LINEAR);
transTransition.setCycleCount(Timeline.INDEFINITE);
// Create a ScrollPane containing the text
ScrollPane scrollPane = new ScrollPane();
scrollPane.setLayoutX(450);
scrollPane.setLayoutY(400);
scrollPane.setPrefWidth(600);
scrollPane.setPrefHeight(400);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setPannable(true);
scrollPane.setContent(textRef);
// Combine ImageView and Group
Group root = new Group(scrollPane, colorPicker, selectFont);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Hello Earthrise");
stage.setMaximized(true);
stage.show();
// Start the text animation
transTransition.play();
}
public static void main(String[] args) {
Application.launch(args);`enter code here`
}
}
答案 0 :(得分:1)
我尝试了你的代码,即使第一个选择实际上不起作用,你也有这种印象,因为如果你进行调试,你会注意到selectFont.getAccessibleText()
返回null。因此Font.font(selectFont.getAccessibleText(), FontWeight.BOLD, 24)
将覆盖默认的 SansSerif ,并且在下次选择时,该值仍然为null,您将看不到任何明显的变化。
我通过这样做而成功:
ObservableList<String> fonts = FXCollections.observableArrayList(Font.getFamilies());
// Select font
ChoiceBox<String> selectFont;
selectFont = new ChoiceBox<>();
selectFont.setValue("SansSerif");
selectFont.setLayoutX(600);
selectFont.setLayoutY(200);
selectFont.setItems(fonts);
selectFont.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> textRef.setFont(Font.font(newValue, FontWeight.BOLD,24)));
希望它有所帮助!