(JavaFX)图片控件&滑块&文件选择器

1.图片控件

1.1 图标
  • 1)先实例化一个图标对象 图标路径(两种方式)

    • ImageView iv=new ImageView(new Image(“file:路径”))

    • ImageView iv=new ImageView("file:路径”)(省略了new Image)**

    //文本+路径

    Label lname=new Label(“名字”,图标对象名iv)

    2)也可以把文本跟图标对象路径都放在一条代码上

    Label lname=new Label("文本",new ImageView("file:路径")

    Label limage = new Label("商品名称", new ImageView(new Image("file:D:\\images\\icons\\goods_insert//name.png")));
    ​
    //实例化一个图片对象
            ImageView iv = new ImageView("file:D:\\images/icons/goods_insert/category.png");
            //创建文本 并指定一个图片
            Label ltype = new Label("商品类型", iv);

1.2 选择封面图片

ImageView  对象名=new ImageView(file:路径)
Label 文本对象名=new Label(请选择照片,图标对象)
Button bb=new Button(选择封面)按钮
TextField  文本框名=new TextField() 文本框用来放图片路径
Hbox hb=new Hbox(文本框名,按钮名)水平布局
        ImageView ifm = new ImageView("file:D:\\images\\icons\\goods_insert/cover.png");
        Label ltp = new Label("选择封面", ifm);
        Button bimg = new Button("选择封面");
        TextField timg = new TextField();
        HBox hb = new HBox(bimg,timg);
        gp.addRow(3, ltp,hb);

1.3加载CSS图片封面

舞台对象名sc.getStylesheets().add(本类名.class.getResource(“Login.css”).toExternalForm())

2.滑块

 ImageView iprice = new ImageView("file:D:\\images\\icons\\goods_insert/price.png");
        Label lprice = new Label("图书价格", iprice);
        //刻度尺
        Slider s = new Slider(0,100,50);
        gp.addRow(2,lprice, s);
        
        s.setMajorTickUnit(8);//刻度间隙为9
        s.setMinorTickCount(2);//每次滑动为2
        //显示刻度
        s.setShowTickMarks(true);
        //显示数字
        s.setShowTickLabels(true);

3.文本域

  // 创建 TextArea 实例
        TextArea textArea = new TextArea();
        // 设置初始文本
        textArea.setText("This is an example of JavaFX TextArea.\nYou can type multiple lines here.");
        
        //设置文本域的行数和列数
        textArea.setPrefRowCount(5); // 设置首选的行数
        textArea.setPrefColumnCount(20); // 设置首选的列数
        
        //设置文本域的高和宽
        textArea.setPrefWidth(20);//宽
        textArea.setPrefHeight(100);//高
        
        
        //设置文本域的滚动条策略
         textArea.setScrollTop(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条
        textArea.setScrollLeft(ScrollPane.ScrollBarPolicy.AS_NEEDED); // 根据需要显示水平滚动条
        
        //获取文本域中的文本
        String currentText = textArea.getText();
        
        // 将 TextArea 添加到布局中
        vbox.getChildren().add(textArea);

4.文件选择器

   //第四行:选择封面图片
        ImageView ifm = new ImageView("file:D:\\images\\icons\\goods_insert/cover.png");
        Label ltp = new Label("选择封面", ifm);
        Button bimg = new Button("选择封面");
        TextField timg = new TextField();
        HBox hb = new HBox(bimg,timg);
        gp.addRow(3, ltp,hb);
        //给按钮添加事件
        bimg.setOnAction(a->{
            FileChooser fc = new FileChooser();
            //显示文件选择窗口,接收选中的文件
            File file = fc.showOpenDialog(stage);
            //把选中的文件路径赋值到文本框:file.getPath()-获取文件的全路径
            timg.setText(file.getPath());
        });

5.日期选择器

 ImageView itime = new ImageView("file:D:\\images\\icons\\goods_insert/time.png");
        Label ltime = new Label("选择封面", itime);
        //时间
        DatePicker dp = new DatePicker();
        gp.addRow(4, ltime,dp);

6.颜色选择器

  • 它允许用户通过图形界面选择颜色

​
  
  @Override
    public void start(Stage primaryStage) {
        // 创建颜色选择器
        ColorPicker colorPicker = new ColorPicker();
        // 设置默认颜色
        colorPicker.setValue(Color.RED);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值