ChoiceBox控件负责选择合适的选项,单独使用JAVAFX很容易实现。但如果使用到scenebuiler,ChoiceBox控件操作则较为麻烦,为了学会这个控件的基本使用,我搜寻了很多资料,希望能帮到大家。控件效果展示如下。
首先放置控件至合适位置后,将生成的Controller类的代码复制至相应的Controller中,具体教程见
scenebuilder教程-与Controller类进行绑定文章来源地址https://www.toymoban.com/news/detail-508078.html
之后设立字符串数组如下所示
private String[] ID = { "生活管家", "后勤管家", "管理员" };
在类后面加上 implements Initializable点击黄色的灯泡图标,会自动补全initialize方法,initialize方法负责执行在页面打开前的各种操作
public class MainpageController implements Initializable
initialize方法内部如下所示,其中choiceBox1.getItems().addAll(ID)负责将字符串内容添加至ChoiceBox空间中,choiceBox1.getSelectionModel().select(0)负责选中ChoiceBox控件中第一个子项,而choiceBox1.setOnAction(this::getID)负责绑定页面操作,getID方法会在下文介绍
public void initialize(URL arg0, ResourceBundle arg1) {
choiceBox1.getItems().addAll(ID);
choiceBox1.getSelectionModel().select(0);// TODO Auto-generated method stub
choiceBox1.setOnAction(this::getID);
}
getID方法如下所示,负责获取ChoiceBox控件中的内容并返回
public String getID(ActionEvent event) {
String myID = choiceBox1.getValue();
return myID;
}
完成代码如下所示
package Controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import Model_File.ServantFile;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
* 主页面控制页面
* @author wjz
*
*/
public class MainpageController implements Initializable {
@FXML
private Button button2;
@FXML
private Button button3;
@FXML
private TextField textField1;
@FXML
private PasswordField passWordField1;
@FXML
private Button button1;
@FXML
private ChoiceBox<String> choiceBox1;
private String[] ID = { "生活管家", "后勤管家", "管理员" };
/**
* 用户账号
*/
public static String pac;
/**
* 用户密码
*/
public static String acc;
@FXML
void buttonAction1(ActionEvent event) throws IOException {
String account = textField1.getText();
String password = passWordField1.getText();
acc = account;
pac = password;
ServantFile servantFile = ServantFile.getServantFile();
switch (getID(event)) {
case "生活管家":
if (servantFile.otherEqual(account, password)) {
Stage stage = (Stage) button1.getScene().getWindow();
stage.close();
LifeMangementController lmc = new LifeMangementController();
lmc.start(stage);
break;
} else if (!servantFile.unOtherEqual1(account, password)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("账号输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else if (!servantFile.unOtherEqual2(account, password)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("密码输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("未知错误");
alert.setContentText("请重新输入┖");
alert.showAndWait();
break;
}
case "后勤管家":
if (servantFile.otherEqual(account, password)) {
Stage stage = (Stage) button1.getScene().getWindow();
stage.close();
LogisticsController lc = new LogisticsController();
lc.start(stage);
break;
} else if (!servantFile.unOtherEqual1(account, password)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("账号输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else if (!servantFile.unOtherEqual2(account, password)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("密码输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("未知错误");
alert.setContentText("请重新输入┖");
alert.showAndWait();
break;
}
case "管理员":
if (account.equals("1111") && password.equals("1111")) {
Stage stage = (Stage) button1.getScene().getWindow();
stage.close();
AdminController aC = new AdminController();
aC.start(stage);
break;
} else if (!account.equals("1111")) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("账号输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else if (!password.equals("1111")) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("密码输入错误");
alert.setContentText("请重新输入");
alert.showAndWait();
break;
} else {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("错误");
alert.setHeaderText("未知错误");
alert.setContentText("请重新输入┖");
alert.showAndWait();
break;
}
}
}
@FXML
void buttonAction3(ActionEvent event) {
Stage stage = (Stage) button3.getScene().getWindow();
stage.close();
}
@FXML
void buttonAction2(ActionEvent event) {
textField1.clear();
passWordField1.clear();
}
/**
* 在选择框中添加元素
*/
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
choiceBox1.getItems().addAll(ID);
choiceBox1.getSelectionModel().select(0);// TODO Auto-generated method stub
choiceBox1.setOnAction(this::getID);
}
public String getID(ActionEvent event) {
String myID = choiceBox1.getValue();
return myID;
}
}
文章来源:https://www.toymoban.com/news/detail-508078.html
到了这里,关于JAVAFX中ChoiceBox控件使用教程(用到scenebuilder)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!