我有一个Java对象,我想在其中插入值。例如,我想这样做:
private UserData ud = new UserData();
public class UserData
{
private String hostName;
private String userName;
private String passwd;
public UserData(String hostName, String userName, String passwd)
{
this.hostName = hostName;
this.userName = userName;
this.passwd = passwd;
}
private UserData()
{
}
public String getHostName()
{
return hostName;
}
public void setHostName(String hostName)
{
this.hostName = hostName;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPasswd()
{
return passwd;
}
public void setPasswd(String passwd)
{
this.passwd = passwd;
}
}
我有这个简单的文本字段
TextField textFieldsz = new TextField();
grid.add(textFieldsz, 1, 0);
Text chartTitle = new Text("User name:");
grid.add(chartTitle, 0, 1);
TextField textFieldds = new TextField();
grid.add(textFieldds, 1, 1);
Text chartcTisstle = new Text("Password:");
grid.add(chartcTisstle, 0, 2);
PasswordField textFieeds = new PasswordField();
grid.add(textFieeds, 1, 2);
我有这个用于登录的按钮
btnLogin.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
}
});
我掌握了如何使业务逻辑的基本知识。您能否在JavaFX中告诉我如何将TextFields中的值插入Java对象并执行某些操作?
答案 0 :(得分:4)
这是解决问题的一种方法。
final UserData ud = new UserData();
GridPane grid = new GridPane();
final TextField textFieldsz = new TextField();
grid.add(textFieldsz, 1, 0);
Text chartTitle = new Text("User name:");
grid.add(chartTitle, 0, 1);
final TextField textFieldds = new TextField();
grid.add(textFieldds, 1, 1);
Text chartcTisstle = new Text("Password:");
grid.add(chartcTisstle, 0, 2);
final PasswordField textFieeds = new PasswordField();
grid.add(textFieeds, 1, 2)
Scene sc = new Scene(grid,400,400);
Button btn = new Button("PRESS");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
ud.setHostName(textFieldsz.getText());
ud.setUserName(textFieldds.getText());
ud.setPasswd(textFieeds.getText());
arg0.consume();
}
});
如您所见,您必须将final
关键字添加到您在内部匿名类中使用的对象(按钮的事件处理程序)。
另一种方法是使用JavaFX-Bindings,但我不认为绑定适合登录屏幕。