广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

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

    来源:网络收集  点击:  时间:2025-01-04
    【导读】:
    Butotn 添加背景图片方法/步骤1/4分步阅读

    布局文件 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/4

    样式表文件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/4

    主程序 JianShuApp.java

    package 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/4

    控制器文件 JianShuController.java

    package 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);

    }

    }

    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_1230896.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-05 17:13:21  耗时:0.027
    0.027s