如何在JavaFX中将图像添加到按钮(动作)?
现场演示
->
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ButtonGraphis extends Application {
public void start(Stage stage) {
//创建图形(图像)
Image img = new Image("UIControls/logo.png");
ImageView view = new ImageView(img);
view.setFitHeight(80);
view.setPreserveRatio(true);
//创建一个按钮
Button button = new Button();
//设置按钮的位置
button.setTranslateX(200);
button.setTranslateY(25);
//设置按钮的大小
button.setPrefSize(80, 80);
//将图形设置为按钮
button.setGraphic(view);
//设置舞台
Group root = new Group(button);
Scene scene = new Scene(root, 595, 170, Color.BEIGE);
stage.setTitle("Button Graphics");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
热门推荐
2 足球队祝福语简短
3 初八祝福语简短语
10 送外卖祝福语大全简短
11 早安祝福语大全简短励志
12 新年给妹妹祝福语简短
13 分手祝福语简短短句
14 简短朋友旅行祝福语大全
15 喜得孙子祝福语简短独特
16 老师祝福语简短100字
17 爱情贺卡祝福语简短英语
18 孩子16岁祝福语简短