程序效果:
弹窗内容:
1.启动类
HelloJavaFX:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class HelloJavaFX extends Application {
private BorderPane root = new BorderPane();
private int pageIndexs = 0;
private int pageSize = 10;
private int count = 0;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("标题");
primaryStage.initStyle(StageStyle.DECORATED);
Scene scene = new Scene(root, 1000, 500, Color.WHITE);
// root.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY,null,null)));
// 初始化菜单
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.setTop(menuBar);
meanu(menuBar);// 调用meanu生成菜单方法
// 主页内容
TextField textField = new TextField();
textField.setPromptText("请输入每页条数");
textField.setMaxWidth(100);
Button button = new Button("确定");
button.setOnAction(actionEvent -> {
if (!textField.getText().isEmpty()) {
pageSize = Integer.parseInt(textField.getText());
Pagination pagination = pagination();
root.setBottom(pagination);
}
});
root.setRight(button);
root.setLeft(textField);
TableView tables = intiTable();
root.getChildren().add(tables);
// 分页
Pagination pagination = pagination();
root.setBottom(pagination);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* 初始化查询条件
*/
private HBox initeSearch() {
// 查询条件
HBox hbox = new HBox();
hbox.setSpacing(10);
Label label = new Label("查询条件");
TextField textField = new TextField();
textField.setPromptText("请输入查询条件");
Button button = new Button("查询");
button.setOnAction(actionEvent -> {
System.out.println("查询");
});
hbox.getChildren().addAll(label, textField, button);
return hbox;
}
/**
* 初始化菜单
*/
private void meanu(MenuBar menuBar) {
// File menu - new, save, exit,滚动条按钮
Menu fileMenu = new Menu("文件");
// 新建==================================================
MenuItem newMenuItem = new MenuItem("新建");
// 弹出新窗口代码
Stage newWindow = new Stage();
newMenuItem.setOnAction(actionEvent -> {
windows(newWindow);// 调用windows方法
});
// 添加==================================================
MenuItem saveMenuItem = new MenuItem("添加");
saveMenuItem.setOnAction(actionEvent -> {
System.out.println("添加");
});
// 退出==================================================
MenuItem exitMenuItem = new MenuItem("退出");
exitMenuItem.setOnAction(actionEvent -> Platform.exit());
// 添加滚动条子菜单按钮====================================
CustomMenuItem customMenuItem = new CustomMenuItem(new Slider());
customMenuItem.setHideOnClick(false);
// 添加自定义菜单项========================================
fileMenu.getItems().addAll(newMenuItem, saveMenuItem,
new SeparatorMenuItem(), exitMenuItem, customMenuItem);
// 编辑==================================================
Menu webMenu = new Menu("编辑");
CheckMenuItem htmlMenuItem = new CheckMenuItem("网页");
htmlMenuItem.setSelected(true);
webMenu.getItems().add(htmlMenuItem);
CheckMenuItem cssMenuItem = new CheckMenuItem("样式");
cssMenuItem.setSelected(true);
webMenu.getItems().add(cssMenuItem);
// 帮助==================================================
Menu sqlMenu = new Menu("帮助");
ToggleGroup tGroup = new ToggleGroup();
RadioMenuItem mysqlItem = new RadioMenuItem("注册");
mysqlItem.setToggleGroup(tGroup);
RadioMenuItem oracleItem = new RadioMenuItem("忘记密码");
oracleItem.setToggleGroup(tGroup);
oracleItem.setSelected(true);
sqlMenu.getItems().addAll(mysqlItem, oracleItem,
new SeparatorMenuItem());
Menu tutorialManeu = new Menu("教程");
tutorialManeu.getItems().addAll(
new CheckMenuItem("Java"),
new CheckMenuItem("JavaFX"),
new CheckMenuItem("Swing"));
sqlMenu.getItems().add(tutorialManeu);
menuBar.getMenus().addAll(fileMenu, webMenu, sqlMenu);
}
/**
* 初始化数据
*
* @return TableView
*/
private List<AdminUserDO> resetList() {
List<AdminUserDO> adminUserDOList = new AdminUserServices().getList();
count = adminUserDOList.size();
adminUserDOList = adminUserDOList.stream().filter(adminUserDO -> adminUserDO.getId() > pageIndexs).collect(Collectors.toList());
adminUserDOList = adminUserDOList.stream().filter(adminUserDO -> pageIndexs == 0 ? adminUserDO.getId() <= pageSize : adminUserDO.getId() <= pageSize * Integer.valueOf(pageIndexs + 1)).collect(Collectors.toList());
adminUserDOList = adminUserDOList.stream().filter(adminUserDO -> adminUserDO.getId() >= pageSize * Integer.valueOf(pageIndexs) + 1).collect(Collectors.toList());
return adminUserDOList;
}
/**
* 初始化弹窗
*
* @return TableView
*/
private void windows(Stage newWindow) {
newWindow.close();// 关闭新窗口 最后再调用show()方法,防止重复开启
newWindow.setTitle("列表");
// 设置新窗口的内容(这里仅使用一个简单的VBox作为示例)
VBox vBox = new VBox();
// 可以在这里添加更多内容到vBox
TextField textField = new TextField();
textField.setMaxWidth(200);
textField.setMaxHeight(200);
textField.setPromptText("请输入");
TextField textField2 = new TextField();
textField2.setMaxWidth(200);
textField2.setMaxHeight(200);
textField2.setPromptText("请输入");
TextField textField3 = new TextField();
textField3.setMaxWidth(200);
textField3.setMaxHeight(200);
textField3.setPromptText("请输入");
TableView table = intiTable();// 初始化表格
// 初始化查询条件
HBox hbox = initeSearch();
vBox.getChildren().add(hbox);
vBox.getChildren().add(table);
Pagination pagination = pagination();// 初始化分页
vBox.getChildren().add(pagination);
Scene scenes = new Scene(vBox, 950, 400);
newWindow.setScene(scenes);
newWindow.show();
}
/**
* 初始化表格
*
* @return TableView
*/
private TableView intiTable() {
// 主页内容
TableView tables = new TableView();
Class<AdminUserDO> adminUserDOClasses = AdminUserDO.class;// 表头实体
TableColumn[] tableColumn = field(adminUserDOClasses);// 初始化表头
for (int i = 0; i < tableColumn.length; i++) {
tables.getColumns().add(tableColumn[i]);// 添加表头
}
// 查询所有用户数据
ObservableList<AdminUserDO> datas = FXCollections.observableArrayList();
List<AdminUserDO> adminUserDOList = resetList();
// 添加数据到表格中
adminUserDOList.stream().forEach(adminUserDO1 -> {
datas.add(adminUserDO1);
});
tables.setItems(datas);
return tables;
}
/**
* 初始化表头
*
* @param adminUserDOClasses
* @return
*/
private TableColumn[] field(Class<AdminUserDO> adminUserDOClasses) {
Field[] fieldNameses = adminUserDOClasses.getDeclaredFields();
TableColumn[] tableColumns = new TableColumn[fieldNameses.length];
final int[] i = {0};
Arrays.stream(fieldNameses).forEach(field -> {
String name = field.getName();
TableColumn tableColumn = null;
switch (name) {
case "id":
tableColumn = new TableColumn("序号");
break;
case "username":
tableColumn = new TableColumn("账号");
break;
case "password":
tableColumn = new TableColumn("密码");
break;
case "nickname":
tableColumn = new TableColumn("姓名");
break;
case "sex":
tableColumn = new TableColumn("性别");
break;
case "email":
tableColumn = new TableColumn("邮箱");
break;
case "mobile":
tableColumn = new TableColumn("手机号");
break;
case "remark":
tableColumn = new TableColumn("备注");
break;
default:
tableColumn = new TableColumn(field.getName());
break;
}
tableColumn.setCellValueFactory(new PropertyValueFactory<>(field.getName()));
tableColumns[i[0]] = tableColumn;// 添加到数组中
i[0]++;
// TableColumn tableColumn = null;
// if (field.getName().equals("id")) {
// tableColumn = new TableColumn("序号");
// } else if (field.getName().equals("username")) {
// tableColumn = new TableColumn("账号");
// } else if (field.getName().equals("password")) {
// tableColumn = new TableColumn("密码");
// } else if (field.getName().equals("nickname")) {
// tableColumn = new TableColumn("姓名");
// } else if (field.getName().equals("sex")) {
// tableColumn = new TableColumn("性别");
// } else if (field.getName().equals("email")) {
// tableColumn = new TableColumn("邮箱");
// } else if (field.getName().equals("mobile")) {
// tableColumn = new TableColumn("手机号");
// } else if (field.getName().equals("remark")) {
// tableColumn = new TableColumn("备注");
// } else {
// tableColumn = new TableColumn(field.getName());
// }
// tableColumn.setCellValueFactory(new PropertyValueFactory<>(field.getName()));
// tableColumns[i[0]] = tableColumn;// 添加到数组中
// i[0]++;
});
return tableColumns;
}
/**
* 初始化分页
*
* @return TableView
*/
private Pagination pagination() {
// 分页
Pagination pagination = new Pagination();
pagination.setCurrentPageIndex(pageIndexs);
pagination.setPageCount(count / pageSize);
pagination.setPageFactory((Integer pageIndex) -> {
pageIndexs = pageIndex;
TableView tableView = intiTable();// 初始化表格
root.setCenter(tableView);
TextField textField = new TextField();
textField.setMaxWidth(50);// 最大宽度
textField.setText(String.valueOf(pageIndexs + 1));
// textField.setPromptText(String.valueOf(pageIndexs+1));
return textField;
});
return pagination;
}
public static void main(String[] args) {
launch(args);
}
}
2.模拟数据列表:
AdminUserServices:
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class AdminUserServices {
List<AdminUserDO> getList(){
List<AdminUserDO> adminUserDOList=new ArrayList<>();
for (int i =0;i<20;i++){
AdminUserDO adminUserDO=new AdminUserDO();
adminUserDO.setId(Long.valueOf(i));
adminUserDO.setUsername("测试"+i);
adminUserDO.setPassword("123456"+i);
adminUserDO.setEmail("123456789@163.com");
adminUserDO.setMobile("123456789");
adminUserDO.setSex(1);
adminUserDO.setRemark("备注"+i);
adminUserDOList.add(adminUserDO);
}
return adminUserDOList;
};
}
3.数据实体:
AdminUserDO:
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.time.LocalDateTime;
import java.util.Set;
public class AdminUserDO {
/**
* 用户ID
*/
private Long id;
/**
* 用户账号
*/
private String username;
/**
* 加密后的密码
*
* 因为目前使用 {@link BCryptPasswordEncoder} 加密器,所以无需自己处理 salt 盐
*/
private String password;
/**
* 用户昵称
*/
private String nickname;
/**
* 备注
*/
private String remark;
/**
* 部门 ID
*/
private Long deptId;
/**
* 岗位编号数组
*/
private Set<Long> postIds;
/**
* 用户邮箱
*/
private String email;
/**
* 手机号码
*/
private String mobile;
/**
* 用户性别
*
* 枚举类 {@link SexEnum}
*/
private Integer sex;
/**
* 用户头像
*/
private String avatar;
/**
* 帐号状态
*
* 枚举 {@link CommonStatusEnum}
*/
private Integer status;
/**
* 最后登录IP
*/
private String loginIp;
/**
* 最后登录时间
*/
private LocalDateTime loginDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public Set<Long> getPostIds() {
return postIds;
}
public void setPostIds(Set<Long> postIds) {
this.postIds = postIds;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getLoginIp() {
return loginIp;
}
public void setLoginIp(String loginIp) {
this.loginIp = loginIp;
}
public LocalDateTime getLoginDate() {
return loginDate;
}
public void setLoginDate(LocalDateTime loginDate) {
this.loginDate = loginDate;
}
}