Java作业-8

JavaFX相关

如何从一个组合框中获取一个条目?如何从一个组合框中获取一个选中条目?

comboBox.getItems()
comboBox.getValue()

当选择一个新的条目时,ComboBox 将触发什么事件?

ActionEvent

ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("选项1", "选项2", "选项3");

// 为ComboBox设置一个事件处理器,以响应用户的选择动作
comboBox.setOnAction(event -> {
    String selected = comboBox.getValue();
    System.out.println("用户选择了: " + selected);
});

如果只是想监听选择的变化,而不是用户的点击动作,使用ChangeListener而不是ActionEvent。ChangeListener会在每次选中的条目发生变化时触发,而不仅仅是用户交互时。

// 为ComboBox的选择模型添加一个变化监听器
comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("选中的条目从 " + oldValue + " 变更为 " + newValue);
});

如果需要在用户确认选择后进行操作,通常使用ActionEvent。如果需要跟踪选择的任何变化,无论是由用户操作还是程序逻辑引起的,应该使用ChangeListener

如何创建一个具有一个字符串数组的可观察的列表?

FXCollections.observableArrayList(arrayOfStrings);

列表视图有什么可用的选择模式?什么是默认的选择模式?如何设置一个选择模式?

  • SelectionMode.SINGLE:单选模式,用户一次只能选择一个列表项。
    SelectionMode.MULTIPLE:多选模式,用户可以选择多个列表项。
  • 默认的选择模式是SelectionMode.SINGLE
    listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

如何创建一个水平滚动条?如何创建一个垂直滚动条?

new ScrollBar() ;
setOrientation(Orientation.HORIZONTAL);
setOrientation(Orientation.VERTICAL);

如何创建一个水平滑动条?如何创建一个垂直滑动条?

new Slider();
setOrientation(Orientation.HORIZONTAL);
setOrientation(Orientation.VERTICAL);

如何添加一个监听器用于处理滑动条的属性值改变?

horizontalSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
    System.out.println("滑动条的新值:" + newValue);
});

如何从一个 URL 创建一个 Media对象? 如何创建一个 MediaPlayer? 如何创建一个MediaView ?

// 创建一个Media对象
String mediaUrl = "file.mp4";

Media media = new Media(mediaUrl);
// 创建MediaPlayer
MediaPlayer mediaPlayer = new MediaPlayer(media);
// 创建MediaView
MediaView mediaView = new MediaView(mediaPlayer);

// 播放媒体
mediaPlayer.play();

// 停止媒体播放
mediaPlayer.stop();

// 暂停媒体播放
mediaPlayer.pause();

(选择一种字体)编写一个程序,可以动态地改变堆栈面板上显示的标签中文本的字体。这个消息可以同时以粗体和斜体显示。可以从组合框中选择字体名和字体大小。使用 Font.getFamilies() 可以得到可用的字体名。字体大小的组合框初始化为从1到100之间的数字

package com.example;

//使用组合框设置文字类型
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import java.util.List;

public class DynamicFontChanger extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Text text = new Text(200, 200, "Hello World!");
        //字体类型组合框
        ComboBox<String> cboFont = new ComboBox<>();
        cboFont.setPrefWidth(100);
        List<String> list = Font.getFamilies();
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            cboFont.getItems().add(list.get(i));
        }
        cboFont.setValue(list.get(0));
        //字体大小组合框
        ComboBox<Integer> cboSize = new ComboBox<>();
        cboSize.setPrefWidth(100);
        Integer[] textSize = new Integer[100];
        for (int i = 0; i < 100; i++)
        {
            textSize[i] = i + 1;
        }
        cboSize.getItems().addAll(textSize);
        cboSize.setValue(20);
        //排版字体和大小组合框
        HBox hbForControl = new HBox(10);
        hbForControl.setAlignment(Pos.CENTER);
        hbForControl.getChildren().addAll(cboFont, cboSize);
        //实现组合框功能
        cboFont.setOnAction(e -> {
            text.setFont(Font.font(cboFont.getValue(), cboSize.getValue()));
        });

        cboSize.setOnAction(e -> {
            text.setFont(Font.font(cboFont.getValue(), cboSize.getValue()));
        });
        //排版
        BorderPane pane = new BorderPane();
        pane.setPrefSize(400, 300);
        pane.setTop(hbForControl);
        pane.setCenter(text);

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ControlTextFontSize");
        primaryStage.show();
    }
}

(使用ComboBox和ListView)编写一个程序,演示在列表中选择的条目。程序用组合框指定选择方式。当选择条目后,列表下方的标签中就会显示选定项

package com.example;

//使用复选框设置列表视图的可选模式
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;

public  class ComboBoxAndListView extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        //控制视图列表的显示模式的组合框
        Label[] modes = {new Label("SINGLE"), new Label("MULTIPLE")};
        ObservableList<Label> items = FXCollections.observableArrayList(modes);
        ComboBox<Label> cbMode = new ComboBox<>();
        cbMode.setValue(modes[0]);
        cbMode.getItems().addAll(items);
        //视图列表内容
        Label[] country = {new Label("1"), new Label("2"), new Label("3"),
            new Label("4"), new Label("5"), new Label("6"), 
            new Label("7"), new Label("8"), new Label("9")};
        ObservableList<Label> itemsCountry = FXCollections.observableArrayList(country);
        ListView<Label> lvCountry = new ListView<>();
        lvCountry.getItems().addAll(itemsCountry);
        //排版控制部分
        HBox hb = new HBox(10);
        hb.setAlignment(Pos.CENTER);
        hb.getChildren().addAll(new Label("Selection Mode"), cbMode);
        VBox pane = new VBox(10);
        pane.setPrefSize(300, 200);
        pane.getChildren().addAll(hb, lvCountry);
        //组合框的功能实现
        cbMode.setOnAction(e -> {
            String str = cbMode.getValue().getText();
            if (str.equals("SINGLE"))
            {
                lvCountry.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            }
            else if (str.equals("MULTIPLE"))
            {
                lvCountry.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
            }
        });

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ComboBoxAndListView");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

(修改程序清单16-14) 增加一个滑动条让用户可以为视频设置当前时间,增加一个标签显示当前时间和视频的整体时间。整个时间是5分钟03秒,当前时间是3分58秒。当播放视频时,滑条值和当前时间持续更新