javafx fxml或CSS中的换行符

我是javafx的新手(并且在一般情况下是很多编程的),并且正在尝试将\ n字符添加到我的fxml文件中。我已经尝试过text =“ $ {'10 \ nquestions'}”方法作为测试,但无法解析符号'10 \ nquestions'。学习如何放置\ n字符会有所帮助,但我的实际问题来自quiz.fxml中的Label fx:idquestionText

quiz.fxml

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:controller="app.java.controllers.QuizController" xmlns:fx="http://javafx.com/fxml">
  <left>
    <VBox fx:id="quizLeft">
        <Label text="Java" />
        <Label fx:id="numberOfQuestions" />
        <Label fx:id="score" />
        <Label text="Time taken: 2:34" />
    </VBox>
  </left>
  <center>
    <VBox id="quizCenter">
        <VBox id="questionLabel">
            <Label fx:id="questionNumber" />
            <Label fx:id="questionText" />
        </VBox>

        <RadioButton fx:id="option1" >
            <toggleGroup>
                <ToggleGroup fx:id="answerList" />
            </toggleGroup>
        </RadioButton>
        <RadioButton fx:id="option2" toggleGroup="$answerList" />
        <RadioButton fx:id="option3" toggleGroup="$answerList" />
        <RadioButton fx:id="option4" toggleGroup="$answerList" />
        <StackPane id="submitButton">
            <Button fx:id="submitAnswerButton" text="Submit" onaction="#submitAnswerButton"/>
        </StackPane>
        <Button text="test" onaction="#printQuestions"/>
        <Button fx:id="homeButton" text="home" onaction="#backToWelcome"/>
    </VBox>
  </center>

</BorderPane>

目前,如果问题太长,我会明白这一点。 UI image

运行时间过长的问题字段连接到QuestionTextLabel,后者是从模拟的模拟数据库中填充的。如何获得太长而无法包装成多行的问题。我也尝试过使用CSS进行多种操作,例如弄乱-fx-text-overrun。这也是css和controller类。您还可以在上面提供的img中查看我的项目结构和其他类。

/*quiz.css*/

.root{
  -fx-background-color: #1e349c;
}

#quizLeft{
  -fx-background-color: #9c0e7c;
  -fx-alignment: center;
  -fx-spacing: 10px;

  -fx-min-width: 300px;
}

.label{
  -fx-text-fill: #fea2ff;
  -fx-font-weight: bold;
  -fx-font-size: 30px;
}

#quizCenter{
  -fx-alignment: center;
}

#questionLabel{
  -fx-padding: 70px;
  -fx-alignment: center;
}

/*#questionText{*/
/*    !*-fx-text-overrun: ;*!*/
/*}*/

.radio-button{
  -fx-font-size: 30px;
  -fx-text-fill: yellow;
  -fx-font-weight: bold;
}

#submitButton{
  -fx-padding: 60px;
  -fx-font-size: 30px;
}

package app.java.controllers;

import app.java.dao.Question;
import app.java.services.SceneBuilder;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class QuizController extends Controller implements Initializable {

@FXML
Button submitAnswerButton;
@FXML
Label questionNumber;
@FXML
Label questionText;
@FXML
RadioButton option1;
@FXML
RadioButton option2;
@FXML
RadioButton option3;
@FXML
RadioButton option4;
@FXML
Button homeButton;
@FXML
Label score;
@FXML
Label numberOfQuestions;
@FXML
ToggleGroup answerList;

Question currentQuestion;
int correctAnswers;
int questionsAnswered;
int currentQuestionNumber;
List<Question> quizList;
SceneBuilder sb;

public void backToWelcome() throws Exception{
    sb.setNewScene(homeButton,"welcome");
}

public void submitAnswerButton() throws Exception{
    updateScore();
    if(questionsAnswered == quizList.size()){
        // games over
        sb.setNewSceneWithParameters(submitAnswerButton,"endOfQuiz",score);

    } else {
        // continue to next question
        currentQuestionNumber++;
        currentQuestion = quizList.get(currentQuestionNumber);
        setQuestionNumber(currentQuestionNumber);
        setQuestionText(currentQuestion.getQuery());
        setChoices(currentQuestion.getanswers());
    }
}

public void setQuestionNumber(int questionNumber){
    this.questionNumber.setText("Question #" + (questionNumber + 1));
}

public Question getQuestionFromList(int nextQuestion){
    return quizList.get(nextQuestion);
}

public void setQuestionText(String questionText){
    this.questionText.setText(questionText);
}

public void setChoices(List<String> choices){
    option1.setText(choices.get(0));
    option2.setText(choices.get(1));
    option3.setText(choices.get(2));
    option4.setText(choices.get(3));
}

public boolean checkAnswer(String correctAnswer,String userGuess){
    return correctAnswer.equals(userGuess);
}

public void updateScore(){
    RadioButton selectedRadioButton = (RadioButton) answerList.getSelectedToggle();
    boolean isCorrect = checkAnswer(currentQuestion.getcorrectAnswer(),selectedRadioButton.getText());
    if(isCorrect){
        correctAnswers++;
    }
    questionsAnswered++;
    setScore();
}

public void printQuestions(){
    quizList.forEach(System.out::println);
}

public void setScore(){
    score.setText(correctAnswers + " correct out of " + questionsAnswered);
}

@Override
public void initialize(URL location,ResourceBundle resources) {
    sb = new SceneBuilder();
    currentQuestionNumber = 0;
    questionsAnswered = 0;
    correctAnswers = 0;
    setScore();

}

@Override
public void initData(Object parameter) {
    this.quizList = (List<Question>)parameter;

    // Set first question
    currentQuestion = getQuestionFromList(currentQuestionNumber);
    setQuestionNumber(currentQuestionNumber);
    setQuestionText(currentQuestion.getQuery());
    setChoices(currentQuestion.getanswers());
    this.numberOfQuestions.setText(quizList.size() + " questions");
}
}

我将获得的任何帮助和建议将不胜感激。谢谢!

编辑: 我知道SceneBuilder可以为我做很多事情,但是由于这是我的第一次尝试,所以我想自己编写它,所以我知道如何手动进行。

iCMS 回答:javafx fxml或CSS中的换行符

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2187617.html

大家都在问