Javafx如何在背景图上设置按钮

 时间:2024-10-24 19:36:58

1、布局文件 jianshu.xml<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Button?><?import javafx.scene.layout.AnchorPane?><AnchorPane xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.soft1841.sample5.JianShuController"> <Button fx:id="testBtn" text="测试按钮" AnchorPane.leftAnchor="200" AnchorPane.topAnchor="100" /></AnchorPane>

2、样式表文件jianshu.css#testBtn{ -fx-text-fill:#FFFFFF; -fx-background-color: rgb(202,48,43); -fx-border-color: rgb(202,48,43); -fx-border-radius: 20; -fx-background-radius: 20; -fx-pref-width: 100px; -fx-pref-height: 35px; -fx-padding: 6 6 6 6;}

3、主程序 JianShuApp.javapackage com.soft1841.sample5;import com.soft1841.sample4.SingerApp;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.scene.image.Image;import javafx.stage.Stage;import java.net.URL;public class JianShuApp extends Application { @Override public void start(Stage primaryStage) throws Exception { URL location = getClass().getResource("jianshu.fxml"); FXMLLoader fxmlLoader = new FXMLLoader(location); Parent root = fxmlLoader.load(); Scene scene = new Scene(root, 800, 600); scene.getStylesheets().add(JianShuApp.class.getResource("jianshu.css").toExternalForm()); primaryStage.setTitle("简书"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); }}

4、控制器文件 JianShuController.javapackage com.soft1841.sample5;import javafx.fxml.FXML;import javafx.fxml.Initializable;import javafx.scene.control.Button;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import java.net.URL;import java.util.ResourceBundle;public class JianShuController implements Initializable { @FXML private Button testBtn; @Override public void initialize(URL location, ResourceBundle resources) { Image btnImg = new Image("/img/heart.png"); ImageView imageView = new ImageView(btnImg); //给按钮设置图标 testBtn.setGraphic(imageView); }}

Javafx如何在背景图上设置按钮
  • eclipse怎么显示代码行数
  • 如何在dreamweaver中快速合并单元格
  • 怎么下载HTMLTestRunner的py文件
  • div+css制作冻结效果-悬浮导航
  • Scrapy项目的创建
  • 热门搜索
    有关端午节的手抄报 遨游汉字王国的手抄报 手抄报感恩老师 小学安全教育手抄报 我爱中华手抄报 心理手抄报资料 手抄报花边简单手绘 关于教师节的手抄报 肺炎手抄报 防火知识手抄报