当前位置:   article > 正文

JavaFX 用户界面控件1——ChoiceBox ComboBox_javafx choicebox

javafx choicebox

1.选择框ChoiceBox

JavaFX的ChoiceBox是一个用户界面控件,用于向用户显示一个选项列表,并允许用户从中选择一个或多个选项。下面是一个ChoiceBox的简单示例和使用介绍:

首先,导入JavaFX的相关类:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

在JavaFX应用程序中,创建一个ChoiceBox对象并填充选项:


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        // 创建一个ChoiceBox对象
        ChoiceBox<String> choiceBox = new ChoiceBox<>();

        // 填充选项列表
        choiceBox.getItems().addAll("Option 1", "Option 2", "Option 3");

        // 设置默认选择
        choiceBox.setValue("Option 1");

        // 创建一个布局并将ChoiceBox添加到其中
        VBox layout = new VBox();
        layout.getChildren().add(choiceBox);

        // 创建一个场景并将布局添加到其中
        Scene scene = new Scene(layout, 300, 200);

        // 设置场景并显示舞台
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

在这个示例中,我们创建一个包含三个选项的ChoiceBox对象,并将其添加到一个垂直布局VBox中。我们设置了默认选项为“Option 1”。最后,我们将布局添加到场景中,并显示舞台。

1.1 字符转换器

当使用复杂对象来支持ChoiceBox时,需要StringConverter。这个对象序列化一个往返于选择框的字符串。对于这个程序,只需要编码toString()来替换Pair对象的默认toString()。(toString和fromString都需要实现才能编译。)

空对象EMPTY_PAIR用于防止nullpointerexception。可以访问和比较assetClass(). getvalue()的返回值,而无需添加特殊的null处理逻辑。

  1. public class ChoicesApp extends Application {
  2. // 创建一个ChoiceBox对象
  3. private final ChoiceBox<Pair<String,String>> assetClass = new ChoiceBox<>();
  4. //创建空Pair对象
  5. private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");
  6. @Override
  7. public void start(Stage primaryStage) throws Exception {
  8. Label label = new Label("Asset Class:");
  9. assetClass.setPrefWidth(200);
  10. Button saveButton = new Button("Save");
  11. // 创建一个布局并将ChoiceBox添加到其中
  12. HBox hbox = new HBox(
  13. label,
  14. assetClass,
  15. saveButton);
  16. hbox.setSpacing( 10.0d );
  17. hbox.setAlignment(Pos.CENTER );
  18. hbox.setPadding( new Insets(40) );
  19. Scene scene = new Scene(hbox);
  20. initChoice();
  21. saveButton.setOnAction(
  22. (evt) -> System.out.println("saving " + assetClass.getValue())
  23. );
  24. primaryStage.setTitle("ChoicesApp");
  25. primaryStage.setScene( scene );
  26. primaryStage.show();
  27. }
  28. //创建复杂对象choiceBox
  29. private void initChoice() {
  30. List<Pair<String,String>> assetClasses = new ArrayList<>();
  31. assetClasses.add( new Pair("Equipment", "20000"));
  32. assetClasses.add( new Pair("Furniture", "21000"));
  33. assetClasses.add( new Pair("Investment", "22000"));
  34. assetClass.setConverter( new StringConverter<Pair<String,String>>() {
  35. @Override
  36. public String toString(Pair<String, String> pair) {
  37. return pair.getKey();
  38. }
  39. @Override
  40. public Pair<String, String> fromString(String string) {
  41. return null;
  42. }
  43. });
  44. assetClass.getItems().add( EMPTY_PAIR );
  45. assetClass.getItems().addAll( assetClasses );
  46. assetClass.setValue( EMPTY_PAIR );
  47. }
  48. public static void main(String[] args) {
  49. launch(args);
  50. }
  51. }

 

2.组合框ComboBox

JavaFX的ComboBox是一个用户界面控件,它结合了文本框和下拉列表,可以向用户显示一组选项,并允许用户从中选择一个或多个选项。下面是一个ComboBox的简单示例和使用说明:

首先,导入JavaFX的相关类:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

在JavaFX应用程序中,创建一个ComboBox对象并填充选项:
 

  1. public class ComboBoxTest extends Application {
  2. @Override
  3. public void start(Stage primaryStage) {
  4. // 创建一个ComboBox对象
  5. ComboBox<String> comboBox = new ComboBox<>();
  6. // 填充选项列表
  7. comboBox.setItems(FXCollections.observableArrayList("Option 1", "Option 2", "Option 3"));
  8. // 设置默认选择
  9. comboBox.setValue("Option 1");
  10. // 创建一个布局并将ComboBox添加到其中
  11. VBox layout = new VBox();
  12. layout.getChildren().add(comboBox);
  13. // 创建一个场景并将布局添加到其中
  14. Scene scene = new Scene(layout, 300, 200);
  15. // 设置场景并显示舞台
  16. primaryStage.setScene(scene);
  17. primaryStage.show();
  18. }
  19. public static void main(String[] args) {
  20. launch(args);
  21. }
  22. }

 

在这个示例中,我们创建了一个包含三个选项的ComboBox对象,并将其添加到一个垂直布局VBox中。我们设置了默认选项为“Option 1”。最后,我们将布局添加到场景中,并显示舞台。

除了上述示例,ComboBox还有一些其他特性,例如可以设置最大可见行数、设置下拉列表的最大高度、设置事件监听器等。你可以根据需求来定制ComboBox的使用。

2.1 ComboBox设置选中回调

  1. public class ComboBoxTest2 extends Application {
  2. // 创建一个ComboBox对象
  3. private final ComboBox<Pair<String, String>> account = new ComboBox<>();
  4. private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");
  5. @Override
  6. public void start(Stage primaryStage) throws Exception {
  7. Label accountsLabel = new Label("Account:");
  8. account.setPrefWidth(200);
  9. Button saveButton = new Button("Save");
  10. // 创建一个布局并将ComboBox添加到其中
  11. HBox hbox = new HBox(
  12. accountsLabel,
  13. account,
  14. saveButton);
  15. hbox.setSpacing( 10.0d );
  16. hbox.setAlignment(Pos.CENTER );
  17. hbox.setPadding( new Insets(40) );
  18. Scene scene = new Scene(hbox);
  19. //初始化
  20. initCombo();
  21. saveButton.setOnAction( (evt) -> {
  22. if( account.getValue().equals(EMPTY_PAIR ) ) {
  23. System.out.println("no save needed; no item selected");
  24. } else {
  25. System.out.println("saving " + account.getValue());
  26. }
  27. });
  28. primaryStage.setTitle("CombosApp");
  29. primaryStage.setScene( scene );
  30. primaryStage.show();
  31. }
  32. private void initCombo() {
  33. List<Pair<String,String>> accounts = new ArrayList<>();
  34. accounts.add( new Pair<>("Auto Expense", "60000") );
  35. accounts.add( new Pair<>("Interest Expense", "61000") );
  36. accounts.add( new Pair<>("Office Expense", "62000") );
  37. accounts.add( new Pair<>("Salaries Expense", "63000") );
  38. account.getItems().add( EMPTY_PAIR );
  39. account.getItems().addAll( accounts );
  40. account.setValue( EMPTY_PAIR );
  41. //设置选中回调
  42. Callback<ListView<Pair<String,String>>, ListCell<Pair<String,String>>> factory =
  43. (lv) ->
  44. new ListCell<Pair<String,String>>() {
  45. @Override
  46. protected void updateItem(Pair<String, String> item, boolean empty) {
  47. super.updateItem(item, empty);
  48. if( empty ) {
  49. setText("");
  50. } else {
  51. setText( item.getKey() );
  52. }
  53. }
  54. };
  55. account.setCellFactory( factory );
  56. account.setButtonCell( factory.call( null ) );
  57. }
  58. public static void main(String[] args) {
  59. launch(args);
  60. }
  61. }

若此文档不够详细,可以参考JAVAFX基础入门_哔哩哔哩_bilibili

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/37798
推荐阅读
相关标签
  

闽ICP备14008679号