当前位置 主页 > 服务器问题 > Linux/apache问题 > 最大化 缩小
设计触发器:
对于每一个Panel,我们要指定一个触发器类,这个是放在该fxml文件中的,例如IDEA中默认创建的就是AnchorPane对象,在它那一行就能找到:fx:controller="sample.MainController" ,这个MainController就是我创建的一个类
之后,我们可以对该panel下各个控件设计触发事件后的反映,这个可以在SceneBuilder中填写,在Code那一栏下面。设计了之后,它就会到我们指定的那个触发器类下寻找这个方法,如果没有的话IDEA会提示你创建
注意,触发器类可以创建多个,这样更便于管理,降低耦合度
在触发器中获取fxml中的控件对象
有时候,我们需要在事件相应中获取对象的值,例如设计登录页面时点击'提交'的按钮,我们需要知道输入框的字符串。这时候我们可以在触发器中获取这些元素,前提是我们为这些控件输入了fx:id,它是全局性的,不允许重复。例如我们可以通过声明:
@FXML private TextField username; @FXML private TextField password;
获取两个TextField对象下的值:
usernameString=username.getText(); passwordString=password.getText();
页面跳转
我们需要为每一个页面设计一个Java类,例如我设计了一个SignIn_Student.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SignIn_Student extends Application{
private String usernameString;
private String passwordString;
@Override
public void start(Stage stage) throws Exception{
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("SignIn_Student.fxml"));//加载页面
Scene anotherScene=new Scene(root);
stage.setTitle("Please log in");
stage.setScene(anotherScene);
stage.show();
}
}
TableView的使用
这个控件用起来着实有点麻烦。折腾了好久。
我们肯定需要在某一个fxml页面中加入了这个TableView,并且输入了Table和它每一个TableColumn的fx:id.
我们需要为有TableView的fxml文件单独创建一个控制器类,之后会说为什么
我们需要创建一个类来表示要储存的数据,例如我这里创建了一个Courses.class:(下面的get和set方法是IDEA自动生成的)
package sample;
import javafx.beans.property.*;
import java.time.LocalDate;
import java.time.LocalTime;
public class Courses {
private final StringProperty department;
private final StringProperty lecturer;
private final ObjectProperty<LocalDate> Time;
private final StringProperty location;
private final IntegerProperty ID;
public Courses(String name, String department, String lecturer, LocalDate time, String location, Integer ID) {
this.name = new SimpleStringProperty(name);
this.department = new SimpleStringProperty(department);
this.lecturer = new SimpleStringProperty(lecturer);
this.Time = new SimpleObjectProperty<LocalDate>(time);
this.location = new SimpleStringProperty(location);
this.ID = new SimpleIntegerProperty(ID);
}
//String,String,String, Date,String,Integer
private final StringProperty name;
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getDepartment() {
return department.get();
}
public StringProperty departmentProperty() {
return department;
}
public void setDepartment(String department) {
this.department.set(department);
}
public String getLecturer() {
return lecturer.get();
}
public StringProperty lecturerProperty() {
return lecturer;
}
public void setLecturer(String lecturer) {
this.lecturer.set(lecturer);
}
public LocalDate getTime() {
return Time.get();
}
public ObjectProperty<LocalDate> timeProperty() {
return Time;
}
public void setTime(LocalDate time) {
this.Time.set(time);
}
public String getLocation() {
return location.get();
}
public StringProperty locationProperty() {
return location;
}
public void setLocation(String location) {
this.location.set(location);
}
public int getID() {
return ID.get();
}
public IntegerProperty IDProperty() {
return ID;
}
public void setID(int ID) {
this.ID.set(ID);
}
}