**15.6(两个消息交替出现)
- 编写一个程序,当单击鼠标时面板上交替显示两个文本"Java is fun"和"Java is powerful"
代码展示:编程练习题15_6TwoInfo.java
package chapter_15;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class 编程练习题15_6TwoInfo extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
Text text = new Text("Java is fun");
stackPane.getChildren().add(text);
stackPane.setOnMousePressed(e ->{
if(text.getText() == "Java is fun")
text.setText("Java is powerful");
else text.setText("Java is fun");
});
Scene scene = new Scene(stackPane, 300, 150);
primaryStage.setTitle("编程练习题15_6TwoInfo");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
- 结果展示