Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - I want to display the text in the Label in a few lines. I used the setWrapText (true) method to do this, but it doesn't work

Here is the part of the code that is related to the Label.

I have no idea where the problem is!!!!!

I used the scene builder and checked the warp Test option. And the FXML file has also been updated.

In everything I found on the internet, it was just said to use this method and I did it but it does not work :)

enter code here/*

package drug.drugstore;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

//start class BaseController
public class BaseController implements Initializable {

    //create an object form class SwitchForm and assign it to switchForm
    private SwitchForm switchForm = new SwitchForm();

    //instance variable
    @FXML
    private ImageView baseFormImageView;
    @FXML
    private ImageView informationalImageView;
    @FXML
    public Label baseMessageLabel;
    @FXML
    private Button closeButton;
    @FXML
    private Label baseDefaultMessageLabel;

    //start method intialize to 
    @Override
    public void initialize(URL url, ResourceBundle rb) {

        //create an object from class File and assign it to lockFile
        File baseFormFile = new File("D:\downloads\Download Chrome\base form picture.jpg");

        //create an object from class Image and assign it to lockImage
        Image baseFormImage = new Image(baseFormFile.toURI().toString());

        baseFormImageView.setImage(baseFormImage); //set Image to ImageView

        //create an object from class File and assign it to lockFile
        File informationalFile = new File("D:\downloads\Download Chrome\base message.jpg");

        //create an object from class Image and assign it to lockImage
        Image informationalImage = new Image(informationalFile.toURI().toString());

        informationalImageView.setImage(informationalImage); //set Image to ImageView

        // create an object from class ShowHealthMessage and assign it to messages
        TimerTask messages;
        messages = new TimerTask() {

            int messageNumber = 1; //assign 1 to messageNumber

            // start method run 
            // to make while loop to create after every 20 second a new String message for label
            @Override
            public void run() {

                //start switch statement
                switch (messageNumber) {

                    case 1:

                        // call method runLater from class Platform to change the value of baseMessageLabel
                        Platform.runLater(() -> {

                            baseMessageLabel.setText("Know the name and doses of your medicines");  // set the new String to the baseMessageLabe
                            baseMessageLabel.setWrapText(true); // baseMessageLabel with Wrapped Text
                            
                        } // end method runLater

                        ); // end class Platfrom

                        messageNumber++; // increment the messageNumber to choose new String value to baseMessageLabel

                        break;

                    case 2:

                        // call method runLater from class Platform to change the value of baseMessageLabel
                        Platform.runLater(new Runnable() {

                            //start method run
                            public void run() {
                                baseMessageLabel.setText("Know why the medicine is important and what it treats");  // set the new String to the baseMessageLabe
                                baseMessageLabel.setWrapText(true); // baseMessageLabel with Wrapped Text
                            } //  end metohd run

                        } // end method runLater

                        ); //  end class Platform

                        messageNumber++; // increment the messageNumber to choose new String value to baseMessageLabel
                        break;

                    case 3:

                        // call method runLater from class Platform to change the value of baseMessageLabel
                        Platform.runLater(new Runnable() {

                            //start method run
                            public void run() {

                                baseMessageLabel.setText("Read your labels to learn what you are taking, how and when to take it, etc");  // set the new String to the baseMessageLabe
                                baseMessageLabel.setWrapText(true); // baseMessageLabel with Wrapped Text

                            } //end method rin

                        } // end method runLater 

                        ); //end class Platform

                        messageNumber++; // increment the messageNumber to choose new String value to baseMessageLabel
                        break;





                    default:
                        messageNumber = 1; // ssume messageNumber to 1 beacase we want show over and over our messages

                        // call method runLater from class Platform to change the value of baseMessageLabel
                        Platform.runLater(() -> {
                            baseMessageLabel.setText("Ask if lower doses might be helpful depending on your age or any kidney or liver problems"); // set the new String to the baseMessageLabel
                            baseMessageLabel.setWrapText(true);
                        }); //end metho runLater of class Platfrom
                        messageNumber++; // increment the messageNumber to choose new String value to baseMessageLabel
                        break;

                } //end switch statement

            } // end method run

        }; // end statement of TimerTask

        // create an object from class Timer and assign it to timer
        Timer timer = new Timer();

        // set schedule at the fixed rate to make change in baseMessageLabel from begin and refresh every 20 second
        timer.scheduleAtFixedRate(messages, 0, 20_000);

    } //end method inititalize

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...