需求:设置两个按钮,一个把文本区的文字变成红色,另外一个把文本区的颜色变回原来的颜色:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
Button btnRed = new Button("字体变红");
Button btnCommon = new Button("恢复原样");
TextArea textArea = new TextArea("字体颜色");
@Override
public void start(Stage primaryStage) throws Exception {
//创建边界面板
BorderPane borderPane = new BorderPane();
//创建单行面板
HBox hBox = new HBox();
//设置单行面板组件的间距为5像素
hBox.setSpacing(5);
//设置单行面板的节点居中对齐
hBox.setAlignment(Pos.CENTER);
//将两个按钮添加到单行面板的节点中
hBox.getChildren().addAll(btnRed,btnCommon);
//边界面板放置组件
borderPane.setCenter(textArea);
borderPane.setBottom(hBox);
//用枚举名调用静态常亮设置按钮居中对齐
BorderPane.setAlignment(btnRed, Pos.CENTER);
//创建监听者对象
ColorChange colorChange