我正在尝试在ToggleButton
内创建ToggleGroup
,但是场景构建器中提供的ToggleButtonGroup
类不包含getSelectedToggle()
方法,这将允许我操纵选定的切换状态。但是,这个方法包含在ToggleGroup
类中,但不幸的是,这个类在scenebuilder中不存在。
即使ToggleGroup
类已启用,也很难,因为它不会扩展javafx.scene.Node
...
我有什么方法可以将getSelectedToggle()
方法与类ToggleButtonGroup
一起使用?如果不是我有什么方法可以通过这个?
提前感谢...
当我尝试使用ToggleGroup
时,这是我的代码:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="799.9999000000025" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.medicalManagement.LoginScreen.LoginScreenController">
<children>
<Pane prefHeight="800.0" prefWidth="640.0" />
<Pane layoutX="641.0" layoutY="34.0" prefHeight="800.0" prefWidth="640.0">
<children>
<Label layoutX="18.0" layoutY="168.0" prefHeight="23.0" prefWidth="114.0" text="Username">
<font>
<Font size="14.0" fx:id="x1" />
</font>
</Label>
<TextField fx:id="userName" layoutX="17.0" layoutY="213.0" prefHeight="25.0" prefWidth="171.0" />
<Label font="$x1" layoutX="264.0" layoutY="168.0" prefHeight="25.0" prefWidth="84.0" text="Password" />
<Button fx:id="login" layoutX="19.0" layoutY="372.0" mnemonicParsing="false" onAction="" prefHeight="32.0" prefWidth="171.0" text="Log In" />
<Separator layoutY="129.0" orientation="VERTICAL" prefHeight="425.0" prefWidth="0.0" />
<PasswordField id="passWord" fx:id="passWord" layoutX="238.0" layoutY="211.0" prefWidth="171.0" />
<Label fx:id="dbStatus" layoutX="501.0" layoutY="756.0" prefHeight="18.0" prefWidth="120.0" />
<ToggleGroup fx:id="userToggleGroup" toggles="adminToggle, nurseToggle">
<RadioButton fx:id="adminToggle" layoutX="37.0" layoutY="290.0" mnemonicParsing="false" text="Admin" />
<RadioButton fx:id="nurseToggle" layoutX="206.0" layoutY="290.0" mnemonicParsing="false" text="Nurse" />
</ToggleGroup>
</children>
</Pane>
</children>
</Pane>
这是controller
类代码:
package com.medicalManagement.LoginScreen;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class LoginScreenController implements Initializable{
LoginModel loginModel = new LoginModel();
@FXML
private TextField userName;
@FXML
private PasswordField passWord;
@FXML
private Button login;
@FXML
private ToggleGroup userToggleGroup;
@FXML
private ToggleButton adminToggle;
@FXML
private ToggleButton nurseToggle;
@FXML
private Label dbStatus;
@Override
public void initialize(URL location, ResourceBundle resources) {
if (this.loginModel.isDbConnected()){
this.dbStatus.setText("connected to database");
}else {
this.dbStatus.setText("database not connected");
}
this.adminToggle.setUserData("Admin");
this.nurseToggle.setUserData("Nurse");
this.adminToggle.setToggleGroup(userToggleGroup);
this.nurseToggle.setToggleGroup(userToggleGroup);
}
public void login(){
try{
if (this.loginModel.isLogin(this.userName.getText(), this.passWord.getText(),this.userToggleGroup.getSelectedToggle().getUserData().toString())){
Stage stage = (Stage)this.login.getScene().getWindow();
stage.close();
switch (this.userToggleGroup.getSelectedToggle().getUserData().toString()){
case "Admin":
adminLogin();
break;
case "Nurse":
nurseLogin();
break;
}
}else{
System.out.println(this.userName.getText());
System.out.println(this.passWord.getText());
System.out.println(this.userToggleGroup.getSelectedToggle().getUserData().toString());
}
}catch (Exception e ){
e.printStackTrace();
System.out.println(this.userName.getText());
System.out.println(this.passWord.getText());
System.out.println(this.userToggleGroup.getSelectedToggle().getUserData().toString());
}
}
private void nurseLogin() {
}
public void adminLogin(){
}
public static void main(String[] args) {
}
}