public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button b1 = new Button();
b1.setText("这是按钮");
b1.setLayoutX(100);
b1.setLayoutY(50);
b1.setPrefWidth(500);
b1.setPrefHeight(200);
// 设置按钮的字体
b1.setFont(Font.font("sans-serif",40));
//b1.setFont(Font.font(40));
// 设置按钮的背景颜色
BackgroundFill backgroundFill = new BackgroundFill(Paint.valueOf("#8FBC8F"), new CornerRadii(20), Insets.EMPTY);
Background background = new Background(backgroundFill);
b1.setBackground(background);
// 设置按钮的边框
BorderStroke borderStroke = new BorderStroke(Paint.valueOf("#8A2BE2"), BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(10));
Border border = new Border(borderStroke);
b1.setBorder(border);
// 设置按钮文字的颜色
b1.setTextFill(Paint.valueOf("#CD0000"));
Button b2 = new Button();
b2.setText("这是按钮");
b2.setLayoutX(100);
b2.setLayoutY(300);
b2.setPrefWidth(500);
b2.setPrefHeight(200);
//可以用以下的CSS样式才替代以上设置
b2.setStyle(
"-fx-background-color: cadetblue;"+
"-fx-background-radius: 20;"+
"-fx-text-fill: antiquewhite;"+
"-fx-font-family: 'Microsoft YaHei UI';"+
"-fx-font-size: 50"
);
// 按钮单击事件
b2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Button button = (Button)event.getSource();
System.out.println(button.getText());
}
});
//更复杂的按键监听
b2.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 2){
switch (event.getButton()){
case SECONDARY:
System.out.println("双击右键");
break;
case PRIMARY:
System.out.println("双击左键");
break;
case MIDDLE:
System.out.println("双击中键");
break;
}
}
}
});
Group group = new Group();
group.getChildren().addAll(b1,b2);
Scene scene = new Scene(group);
primaryStage.setScene(scene);
primaryStage.setHeight(600);
primaryStage.setWidth(800);
primaryStage.show();
primaryStage.centerOnScreen();
}
}