-2
我不知道爲什麼主類不能正常工作。我創建了主頁面, 然後,建模(連接數據庫與主應用程序),然後數據庫(從數據庫中檢索信息)。 創建所有內容後,我發現顯示結果的主類不是顯示結果,而是從db中獲取, 顯示的是'Controller @ person ...'(它是包和類的名稱)。Javafx主頁面,控制器和數據庫類別運行不正常
通過代碼後,確定代碼中沒有錯誤,但可能是語法錯誤。 誰能幫我找出什麼在代碼丟失
public class Cbt extends Application {
private SchoolDB sdb;
Person person = new Person();
TextField username, firstName, lastName, initial, password;
GridPane addGridStaffRegLO;
AnchorPane staffRegLO;
Scene staffReg;
ComboBox qualification;
public void start(Stage theMainStage) throws DBException {
stageCbt = theMainStage;
border = new BorderPane();
hbox = addHBox();
border.setTop(addHBox());
border.setBottom(addHDBox());
border.setLeft(addVbox());
border.setRight(addFlowPaneLO());
border.setCenter(addLogReg(addGridPaneLO()));
}
private HBox addHBox() {
hbox = new HBox();
hbox.getStyleClass().add("hbox");
hbox.getChildren().add(displayWelcomeMsg);
return hbox;
}
//the vbox for left border
private VBox addVbox() {
vbox = new VBox();
vbox.setPadding(new Insets(15, 12, 15, 12));
vbox.setSpacing(10);
return vbox;
}
private HBox addHDBox() {
hbdox = new HBox();
hbdox.getStyleClass().add("hbox");
hbdox.getChildren().add(displayWelcomeMsg);
return hbdox;
}
private HBox addSHBox() {
hbox = new HBox();
hbox.getStyleClass().add("hbox");
hbox.getChildren().add(displayWelcomeMsg);
return hbox;
}
private GridPane addStaffGridRegLO() {
addGridStaffRegLO = new GridPane();
addGridStaffRegLO.getStyleClass().add("grid");
addGridStaffRegLO.setHgap(10);
addGridStaffRegLO.setVgap(10);
addGridStaffRegLO.setPadding(new Insets(0, 10, 0, 10));
Text welcome = new Text("Welcome to the school CBT for jamb");
welcome.setFont(Font.font("Arial", FontWeight.BOLD, 20));
addGridStaffRegLO.add(welcome, 1, 0);
Text ss = new Text("For both staffs and students");
ss.setFont(Font.font("Arial", FontWeight.BOLD, 20));
addGridStaffRegLO.add(ss, 2, 0);
firstName = new TextField();
addGridStaffRegLO.add(firstName, 3, 1);
firstName.textProperty().bind(person.firstNameProperty());
firstName.setPromptText("Enter your student firstname");
Tooltip fn = new Tooltip();
fn.setText("student firstname here");
firstName.setTooltip(fn);
lastName = new TextField();
addGridStaffRegLO.add(lastName, 3, 2);
lastName.textProperty().bind(person.lastNameProperty());
lastName.setPromptText("Enter student lastname");
Tooltip ln = new Tooltip();
ln.setText("Student lastname here");
lastName.setTooltip(ln);
initial = new TextField();
initial.textProperty().bind(person.initialProperty());
addGridStaffRegLO.add(initial, 3, 3);
initial.setPromptText("Staff number here");
Tooltip initTip = new Tooltip();
initTip.setText("Student initial here");
initial.setTooltip(initTip);
username = new TextField();
addGridStaffRegLO.add(username, 3, 4);
username.textProperty().bind(login.usernameProperty());
username.setPromptText("Enter your username");
Tooltip userTip = new Tooltip();
userTip.setText("your username here");
username.setTooltip(userTip);
password = new PasswordField();
addGridStaffRegLO.add(password, 3, 5);
password.textProperty().bind(login.passwordProperty());
password.setPromptText("Your password here");
Tooltip pass = new Tooltip();
pass.setText("Your passowrd here");
password.setTooltip(pass);
ObservableList<School> options = FXCollections.observableArrayList();
qualification = new ComboBox(options);
qualification.setPromptText("Staff qualification here");
addGridStaffRegLO.add(qualification, 3, 6);
try {
options.addAll(SchoolDB.getAllStudents());
} catch (DBException ex) {
Logger.getLogger(Cbt.class.getName()).log(Level.SEVERE, null, ex);
}
return addGridStaffRegLO;
}
private AnchorPane addStaffRegLO(GridPane addGridStaffRegLO) {
staffRegLO = new AnchorPane();
staffRegLO.getStyleClass().add("pane");
Register = new Button("Signup");
HBox hb = new HBox();
hb.getStyleClass().add("hb");
hb.getChildren().add(Register);
staffRegLO.getChildren().addAll(addGridStaffRegLO, hb);
// Anchor buttons to bottom right, anchor grid to top
AnchorPane.setBottomAnchor(hb, 8.0);
AnchorPane.setRightAnchor(hb, 5.0);
AnchorPane.setTopAnchor(addGridStaffRegLO, 10.0);
return staffRegLO;
}
}
這是模型類
public class Person {
private final SimpleStringProperty firstName = new SimpleStringProperty(this, "firstName");
public StringProperty firstNameProperty() {
return firstName;
}
public final String getFirstName() {
return firstNameProperty().get();
}
public void setFirstName(String firstName) {
firstNameProperty().set(firstName);
}
private final SimpleStringProperty lastName = new SimpleStringProperty(this, "lastName");
public StringProperty lastNameProperty() {
return lastName;
}
public final String getLastName() {
return lastNameProperty().get();
}
public void setLastName(String lastName) {
lastNameProperty().set(lastName);
}
private final SimpleStringProperty initial = new SimpleStringProperty(this, "initial");
public StringProperty initialProperty() {
return initial;
}
public final String getInitial() {
return initialProperty().get();
}
public void setInitial(String initial) {
initialProperty().set(initial);
}
private final IntegerProperty studentId = new SimpleIntegerProperty(this, "studentId");
public IntegerProperty studentIdProperty() {
return studentId;
}
public final int getStudentId() {
return studentIdProperty().get();
}
public void setStudentId(int studentId) {
studentIdProperty().set(studentId);
}
public Person() {
}
public Person(String firstName, String lastName, String initial, int studentId) {
setFirstName(firstName);
setLastName(lastName);;
setInitial(initial);
setStudentId(studentId);
}
這是數據庫類
import java.util.ArrayList;
import java.util.List;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import controller.Person;
public class SchoolDB {
public static List<School> getAllStudents() throws DBException {
String sql = "SELECT last_name, first_name, middle_name, id FROM stu_reg";
List<School> studentsDetail = new ArrayList<>();
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String lastName = rs.getString("last_name");
String firstName = rs.getString("first_name");
String initial = rs.getString("middle_name");
int studentId = rs.getInt("id");
//Person p = new Person(lastName, firstName, initial, studentId);
School sch = new School();
sch.setFirstName(firstName);
studentsDetail.add(sch);
}
return studentsDetail;
} catch (SQLException e) {
throw new DBException(e);
}
}
}
通過使用調試器逐步完成該操作,您學到了什麼? –
我沒有使用調試器 –
任何原因爲什麼不? –