java fx combox,JavaFX 类 ComboBox用法及代码示例

ComboBox是JavaFX库的一部分。 JavaFX ComboBox是简单ComboBox的实现,它显示了一个项目列表,用户最多可以从中选择一个项目,它继承了ComboBoxBase类。

ComboBox的构造函数:

ComboBox():创建一个默认的空组合框

ComboBox(ObservableList i):使用给定的项目创建一个组合框

常用方法:

方法

说明

getEditor()

获取属性编辑器的值

getItems()

返回组合框的项目

getVisibleRowCount()

返回属性visibleRowCount的值。

setItems(ObservableList v)设置组合框的项目

setVisibleRowCount(int v)

设置属性VisibleRowCount的值

以下示例程序旨在说明JavaFX的ComboBox类:

程序创建一个组合框并向其中添加项目:此程序创建一个名为combo_box的组合框,并使用ChoiceBox(FXCollections.observableArrayList(week_days))向其中添加字符串列表。我们将组合框和一个标签(描述)添加到磁贴窗格(getChildren().add()函数)。我们将创建一个舞台(容器),并将图块添加到场景中,然后将场景添加到场景中。我们将使用show()函数显示舞台。

// Java Program to create a combo Box and add items to it

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.*;

import javafx.scene.layout.*;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.collections.*;

import javafx.stage.Stage;

import javafx.scene.text.Text.*;

import javafx.scene.paint.*;

import javafx.scene.text.*;

public class combo_box_1 extends Application {

// Launch the application

public void start(Stage stage)

{

// Set title for the stage

stage.setTitle("creating combo box ");

// Create a tile pane

TilePane r = new TilePane();

// Create a label

Label description_label =

new Label("This is a combo box example ");

// Weekdays

String week_days[] =

{ "Monday", "Tuesday", "Wednesday",

"Thrusday", "Friday" };

// Create a combo box

ComboBox combo_box =

new ComboBox(FXCollections

.observableArrayList(week_days));

// Create a tile pane

TilePane tile_pane = new TilePane(combo_box);

// Create a scene

Scene scene = new Scene(tile_pane, 200, 200);

// Set the scene

stage.setScene(scene);

stage.show();

}

public static void main(String args[])

{

// Launch the application

launch(args);

}

}

输出:

8600ddd8dec9a4d02e0b63e2aed545e7.png

程序创建一个组合框并向其中添加一个事件处理程序:此程序创建一个名为combo_box的组合框,并使用(ChoiceBox(FXCollections.observableArrayList(week_days)))向其中添加字符串列表。我们将组合框和一个标签(描述)添加到磁贴窗格(getChildren().add()函数)。我们将创建一个舞台(容器),并将图块添加到场景中,然后将场景添加到场景中。我们将使用show()函数显示舞台。我们将添加一个事件处理程序事件来处理combo_box的事件,该事件会将所选标签的文本更改为所选项目。我们还将选择的标签添加到平铺窗格中。

// Java program to create a combo box and add event handler to it

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.*;

import javafx.scene.layout.*;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.collections.*;

import javafx.stage.Stage;

import javafx.scene.text.Text.*;

import javafx.scene.paint.*;

import javafx.scene.text.*;

public class combo_box_2 extends Application {

// Launch the application

public void start(Stage stage)

{

// Set title for the stage

stage.setTitle("creating combo box ");

// Create a tile pane

TilePane r = new TilePane();

// Create a label

Label description_label =

new Label("This is a combo box example ");

// Weekdays

String week_days[] =

{ "Monday", "Tuesday", "Wednesday",

"Thrusday", "Friday" };

// Create a combo box

ComboBox combo_box =

new ComboBox(FXCollections

.observableArrayList(week_days));

// Label to display the selected menuitem

Label selected = new Label("default item selected");

// Create action event

EventHandler event =

new EventHandler() {

public void handle(ActionEvent e)

{

selected.setText(combo_box.getValue() + " selected");

}

};

// Set on action

combo_box.setOnAction(event);

// Create a tile pane

TilePane tile_pane = new TilePane(combo_box, selected);

// Create a scene

Scene scene = new Scene(tile_pane, 200, 200);

// Set the scene

stage.setScene(scene);

stage.show();

}

public static void main(String args[])

{

// Launch the application

launch(args);

}

}

输出:

2396326d994b8f1b88a883c5c137b5e7.png

51a0d0c337bf779bf82c5f216302e04f.png

注意:以上程序可能无法在在线IDE中运行,请使用离线转换器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值