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應用程序中,創(chuàng)建一個ChoiceBox對象并填充選項:
public class Main extends Application {
? ? @Override
? ? public void start(Stage primaryStage) {
? ? ? ? // 創(chuàng)建一個ChoiceBox對象
? ? ? ? ChoiceBox<String> choiceBox = new ChoiceBox<>();
? ? ? ? // 填充選項列表
? ? ? ? choiceBox.getItems().addAll("Option 1", "Option 2", "Option 3");
? ? ? ? // 設置默認選擇
? ? ? ? choiceBox.setValue("Option 1");
? ? ? ? // 創(chuàng)建一個布局并將ChoiceBox添加到其中
? ? ? ? VBox layout = new VBox();
? ? ? ? layout.getChildren().add(choiceBox);
? ? ? ? // 創(chuàng)建一個場景并將布局添加到其中
? ? ? ? Scene scene = new Scene(layout, 300, 200);
? ? ? ? // 設置場景并顯示舞臺
? ? ? ? primaryStage.setScene(scene);
? ? ? ? primaryStage.show();
? ? }
? ??
? ? public static void main(String[] args) {
? ? ? ? launch(args);
? ? }
}
在這個示例中,我們創(chuàng)建一個包含三個選項的ChoiceBox對象,并將其添加到一個垂直布局VBox中。我們設置了默認選項為“Option 1”。最后,我們將布局添加到場景中,并顯示舞臺。
1.1 字符轉換器
當使用復雜對象來支持ChoiceBox時,需要StringConverter。這個對象序列化一個往返于選擇框的字符串。對于這個程序,只需要編碼toString()來替換Pair對象的默認toString()。(toString和fromString都需要實現(xiàn)才能編譯。)
空對象EMPTY_PAIR用于防止nullpointerexception??梢栽L問和比較assetClass(). getvalue()的返回值,而無需添加特殊的null處理邏輯。
public class ChoicesApp extends Application {
// 創(chuàng)建一個ChoiceBox對象
private final ChoiceBox<Pair<String,String>> assetClass = new ChoiceBox<>();
//創(chuàng)建空Pair對象
private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");
@Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Asset Class:");
assetClass.setPrefWidth(200);
Button saveButton = new Button("Save");
// 創(chuàng)建一個布局并將ChoiceBox添加到其中
HBox hbox = new HBox(
label,
assetClass,
saveButton);
hbox.setSpacing( 10.0d );
hbox.setAlignment(Pos.CENTER );
hbox.setPadding( new Insets(40) );
Scene scene = new Scene(hbox);
initChoice();
saveButton.setOnAction(
(evt) -> System.out.println("saving " + assetClass.getValue())
);
primaryStage.setTitle("ChoicesApp");
primaryStage.setScene( scene );
primaryStage.show();
}
//創(chuàng)建復雜對象choiceBox
private void initChoice() {
List<Pair<String,String>> assetClasses = new ArrayList<>();
assetClasses.add( new Pair("Equipment", "20000"));
assetClasses.add( new Pair("Furniture", "21000"));
assetClasses.add( new Pair("Investment", "22000"));
assetClass.setConverter( new StringConverter<Pair<String,String>>() {
@Override
public String toString(Pair<String, String> pair) {
return pair.getKey();
}
@Override
public Pair<String, String> fromString(String string) {
return null;
}
});
assetClass.getItems().add( EMPTY_PAIR );
assetClass.getItems().addAll( assetClasses );
assetClass.setValue( EMPTY_PAIR );
}
public static void main(String[] args) {
launch(args);
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-706103.html
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應用程序中,創(chuàng)建一個ComboBox對象并填充選項:
?
public class ComboBoxTest extends Application {
@Override
public void start(Stage primaryStage) {
// 創(chuàng)建一個ComboBox對象
ComboBox<String> comboBox = new ComboBox<>();
// 填充選項列表
comboBox.setItems(FXCollections.observableArrayList("Option 1", "Option 2", "Option 3"));
// 設置默認選擇
comboBox.setValue("Option 1");
// 創(chuàng)建一個布局并將ComboBox添加到其中
VBox layout = new VBox();
layout.getChildren().add(comboBox);
// 創(chuàng)建一個場景并將布局添加到其中
Scene scene = new Scene(layout, 300, 200);
// 設置場景并顯示舞臺
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
?
在這個示例中,我們創(chuàng)建了一個包含三個選項的ComboBox對象,并將其添加到一個垂直布局VBox中。我們設置了默認選項為“Option 1”。最后,我們將布局添加到場景中,并顯示舞臺。
除了上述示例,ComboBox還有一些其他特性,例如可以設置最大可見行數(shù)、設置下拉列表的最大高度、設置事件監(jiān)聽器等。你可以根據(jù)需求來定制ComboBox的使用。
2.1 ComboBox設置選中回調
public class ComboBoxTest2 extends Application {
// 創(chuàng)建一個ComboBox對象
private final ComboBox<Pair<String, String>> account = new ComboBox<>();
private final static Pair<String, String> EMPTY_PAIR = new Pair<>("", "");
@Override
public void start(Stage primaryStage) throws Exception {
Label accountsLabel = new Label("Account:");
account.setPrefWidth(200);
Button saveButton = new Button("Save");
// 創(chuàng)建一個布局并將ComboBox添加到其中
HBox hbox = new HBox(
accountsLabel,
account,
saveButton);
hbox.setSpacing( 10.0d );
hbox.setAlignment(Pos.CENTER );
hbox.setPadding( new Insets(40) );
Scene scene = new Scene(hbox);
//初始化
initCombo();
saveButton.setOnAction( (evt) -> {
if( account.getValue().equals(EMPTY_PAIR ) ) {
System.out.println("no save needed; no item selected");
} else {
System.out.println("saving " + account.getValue());
}
});
primaryStage.setTitle("CombosApp");
primaryStage.setScene( scene );
primaryStage.show();
}
private void initCombo() {
List<Pair<String,String>> accounts = new ArrayList<>();
accounts.add( new Pair<>("Auto Expense", "60000") );
accounts.add( new Pair<>("Interest Expense", "61000") );
accounts.add( new Pair<>("Office Expense", "62000") );
accounts.add( new Pair<>("Salaries Expense", "63000") );
account.getItems().add( EMPTY_PAIR );
account.getItems().addAll( accounts );
account.setValue( EMPTY_PAIR );
//設置選中回調
Callback<ListView<Pair<String,String>>, ListCell<Pair<String,String>>> factory =
(lv) ->
new ListCell<Pair<String,String>>() {
@Override
protected void updateItem(Pair<String, String> item, boolean empty) {
super.updateItem(item, empty);
if( empty ) {
setText("");
} else {
setText( item.getKey() );
}
}
};
account.setCellFactory( factory );
account.setButtonCell( factory.call( null ) );
}
public static void main(String[] args) {
launch(args);
}
}
若此文檔不夠詳細,可以參考JAVAFX基礎入門_嗶哩嗶哩_bilibili文章來源:http://www.zghlxwxcb.cn/news/detail-706103.html
?
到了這里,關于JavaFX 用戶界面控件1——ChoiceBox ComboBox的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!