2025年3月6日17:43:58

master
qmstyle 2025-03-06 17:44:06 +08:00
parent 85c9eff53e
commit 94e5f343b1
6 changed files with 180 additions and 62 deletions

View File

@ -1,5 +1,6 @@
package com.zhangmeng.online.exam.ui.admin;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.layouts.SideMenu;
import com.zhangmeng.online.exam.ui.layouts.TopMenu;
import javafx.scene.control.MenuBar;
@ -23,17 +24,7 @@ public class IndexPage extends BorderPane {
public IndexPage() {
SideMenu.SideMenuItem item1 = new SideMenu.SideMenuItem("试卷管理", new SideMenu.SideMenuData("试卷管理", "/paper/list"));
SideMenu.SideMenuItem item2 = new SideMenu.SideMenuItem("题库管理", new SideMenu.SideMenuData("题库管理", "/question/list"));
SideMenu.SideMenuItem item3 = new SideMenu.SideMenuItem("用户管理", new SideMenu.SideMenuData("用户管理", "/user/list"));
SideMenu.SideMenuItem item4 = new SideMenu.SideMenuItem("系统设置", new SideMenu.SideMenuData("系统设置", "/system/setting"));
List<SideMenu.SideMenuItem> menuItems = new ArrayList<>();
menuItems.add(item1);
menuItems.add(item2);
menuItems.add(item3);
menuItems.add(item4);
sideMenu = new SideMenu(menuItems); // 导航栏容器
sideMenu = new SideMenu(ApiUtils.getMenuItems()); // 导航栏容器
StackPane contentArea = new StackPane(); // 右侧内容区
contentArea.setStyle("-fx-background-color: #3ce53c;");

View File

@ -0,0 +1,14 @@
package com.zhangmeng.online.exam.ui.admin;
/**
* @author zm
* @date 2025/3/6 16:51
* @version: 1.0
*/
public class UserListPage {
}

View File

@ -0,0 +1,92 @@
package com.zhangmeng.online.exam.ui.api;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhangmeng.online.exam.ui.layouts.SideMenu;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zm
* @date 2025/3/6 15:17
* @version: 1.0
*/
public class ApiUtils {
public static final String API_URL = "http://localhost:8080";
public static List<SideMenu.SideMenuItem> getMenuItems() {
List<SideMenu.SideMenuItem> menuItems = new ArrayList<>();
String sideData = HttpUtils.GET(API_URL + "/user/menu");
JSONObject jsonObject = JSON.parseObject(sideData);
JSONArray data = jsonObject.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
JSONObject menu = data.getJSONObject(i);
String name = menu.getString("name");
String url = menu.getString("url");
JSONArray children = (JSONArray) menu.get("children");
List<SideMenu.SideMenuData> sideMenuDataList = new ArrayList<>();
children.forEach(child -> {
JSONObject childMenu = (JSONObject) child;
String childName = childMenu.getString("name");
String childUrl = childMenu.getString("url");
SideMenu.SideMenuData sideMenuData = new SideMenu.SideMenuData(childName, childUrl);
sideMenuDataList.add(sideMenuData);
});
SideMenu.SideMenuItem item = new SideMenu.SideMenuItem(name, sideMenuDataList);
menuItems.add(item);
}
return menuItems;
}
public static Map<String, String> getUserList(){
String userListData = HttpUtils.GET(API_URL + "/user/list");
JSONObject jsonObject = JSON.parseObject(userListData);
JSONArray data = jsonObject.getJSONArray("data");
Map<String, String> userMap = new HashMap<>();
for (Object datum : data) {
JSONObject user = (JSONObject) datum;
userMap.put("序号", user.getString("id"));
userMap.put("用户名", user.getString("username"));
userMap.put("电子邮箱", user.getString("email"));
userMap.put("手机号码", user.getString("phone"));
}
return userMap;
}
public static Map<String, String> getRoleList() {
String userListData = HttpUtils.GET(API_URL + "/role/list");
JSONObject jsonObject = JSON.parseObject(userListData);
JSONArray data = jsonObject.getJSONArray("data");
Map<String, String> userMap = new HashMap<>();
for (Object datum : data) {
JSONObject user = (JSONObject) datum;
userMap.put("序号", user.getString("id"));
userMap.put("角色名称", user.getString("name"));
userMap.put("描述", user.getString("desc"));
userMap.put("角色类型", user.getString("type_name"));
}
return userMap;
}
public static Map<String, String> getPermissionList() {
return null;
}
}

View File

@ -38,15 +38,16 @@ public class DynamicTableComponent extends VBox {
}
}
public DynamicTableComponent() {
public DynamicTableComponent(Map<String, String> dataList) {
super();
TableView<DynamicDataModel> tableView = new TableView<>();
tableView.setItems(data);
tableView.prefHeightProperty().bind(this.heightProperty());
tableView.prefWidthProperty().bind(this.widthProperty());
// 动态生成列(示例字段)‌:ml-citation{ref="3,4" data="citationList"}
List<String> columns = Arrays.asList("姓名", "年龄", "城市");
List<String> columns = Arrays.asList(dataList.keySet().toArray(new String[0]));
columns.forEach(col -> {
TableColumn<DynamicDataModel, String> column =
new TableColumn<>(col);
TableColumn<DynamicDataModel, String> column = new TableColumn<>(col);
// 数据绑定‌:ml-citation{ref="1,3" data="citationList"}
column.setCellValueFactory(param ->
@ -60,16 +61,19 @@ public class DynamicTableComponent extends VBox {
row.getProperty(col).set(event.getNewValue());
});
column.prefWidthProperty().bind(tableView.widthProperty().divide(columns.size()));
tableView.getColumns().add(column);
});
// 填充示例数据‌:ml-citation{ref="2,3" data="citationList"}
data.add(new DynamicDataModel(Map.of(
"姓名", "张三",
"年龄", "28",
"城市", "北京"
)));
tableView.setItems(data);
// data.add(new DynamicDataModel(Map.of(
// "姓名", "张三",
// "年龄", "28",
// "城市", "北京"
// )));
data.add(new DynamicDataModel(dataList));
this.getChildren().addAll(tableView);
}

View File

@ -1,6 +1,7 @@
package com.zhangmeng.online.exam.ui.layouts;
import com.zhangmeng.online.exam.ui.admin.IndexPage;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
import com.zhangmeng.online.exam.ui.components.ShortAnswerComponent;
import javafx.beans.value.ChangeListener;
@ -16,6 +17,7 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import java.util.List;
import java.util.Map;
/**
@ -25,7 +27,7 @@ import java.util.List;
*/
public class SideMenu extends VBox {
public static class SideMenuData{
public static class SideMenuData {
private String text;
private String url;
@ -51,12 +53,12 @@ public class SideMenu extends VBox {
}
}
public static class SideMenuItem{
public static class SideMenuItem {
private String text;
private SideMenuData sideMenuData;
private List<SideMenuData> sideMenuData;
public SideMenuItem(String text, SideMenuData sideMenuData) {
public SideMenuItem(String text, List<SideMenuData> sideMenuData) {
this.text = text;
this.sideMenuData = sideMenuData;
}
@ -69,11 +71,11 @@ public class SideMenu extends VBox {
this.text = text;
}
public SideMenuData getSideMenuData() {
public List<SideMenuData> getSideMenuData() {
return sideMenuData;
}
public void setSideMenuData(SideMenuData sideMenuData) {
public void setSideMenuData(List<SideMenuData> sideMenuData) {
this.sideMenuData = sideMenuData;
}
}
@ -92,39 +94,42 @@ public class SideMenu extends VBox {
TitledPane tp = new TitledPane();
tp.setText(item.getText());
HBox hbox = new HBox();
hbox.setPadding(javafx.geometry.Insets.EMPTY);
VBox vbox = new VBox();
vbox.setPadding(javafx.geometry.Insets.EMPTY);
vbox.setAlignment(javafx.geometry.Pos.CENTER);
hbox.setSpacing(10);
hbox.setAlignment(javafx.geometry.Pos.CENTER);
Label label = new Label(item.getSideMenuData().getText());
hbox.getChildren().add(label);
tp.setContent(hbox);
for (SideMenuData data : item.getSideMenuData()) {
hbox.hoverProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue){
hbox.setStyle("-fx-background-color: #b3f0ee;");
}else {
hbox.setStyle("-fx-background-color: white;");
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(javafx.geometry.Pos.CENTER);
Label label = new Label(data.getText());
hbox.getChildren().add(label);
vbox.getChildren().add(hbox);
hbox.hoverProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue) {
hbox.setStyle("-fx-background-color: #b3f0ee;");
} else {
hbox.setStyle("-fx-background-color: null;");
}
}
}
});
});
hbox.setOnMouseClicked(event -> {
System.out.println("hbox.setOnMouseClicked====>" + item.getSideMenuData().getUrl());
// Parent parent = hbox.getParent().getParent().getParent().getParent();
// System.out.println(parent.getClass().getName());
hbox.setOnMouseClicked(event -> {
Scene scene = hbox.getScene();
IndexPage root = (IndexPage)scene.getRoot();
System.out.println("hbox.setOnMouseClicked====>" + data.getUrl());
switch (data.getUrl()) {
case "/user/list" -> init_table(hbox, ApiUtils.getUserList());
case "/role/list" -> init_table(hbox,ApiUtils.getRoleList());
case "/permission/list" -> init_table(hbox,ApiUtils.getPermissionList());
}
DynamicTableComponent shortAnswerComponent = new DynamicTableComponent();
shortAnswerComponent.setPadding(new Insets(15));
root.setCenter(shortAnswerComponent);
});
});
}
tp.setContent(vbox);
tp.expandedProperty().addListener(new ChangeListener<Boolean>() {
@Override
@ -141,13 +146,23 @@ public class SideMenu extends VBox {
accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
@Override
public void changed(ObservableValue<? extends TitledPane> observable, TitledPane oldValue, TitledPane newValue) {
if (newValue == null){
System.out.println(oldValue.getText()+ "=====>折叠");
if (newValue == null) {
System.out.println(oldValue.getText() + "=====>折叠");
return;
}
System.out.println(newValue.getText()+ "=====>展开");
System.out.println(newValue.getText() + "=====>展开");
}
});
}
private void init_table(HBox hbox, Map<String, String> dataList) {
Scene scene = hbox.getScene();
IndexPage root = (IndexPage) scene.getRoot();
DynamicTableComponent shortAnswerComponent = new DynamicTableComponent(dataList);
shortAnswerComponent.setPadding(new Insets(15));
shortAnswerComponent.prefHeightProperty().bind(root.heightProperty().subtract(15));
root.setCenter(shortAnswerComponent);
}
}

View File

@ -3,10 +3,13 @@ package com.zhangmeng.online.exam.ui.utils;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -21,6 +24,10 @@ public class HttpUtils<T> {
return JSONObject.parseObject(response, clazz);
}
public static String GET(String url) {
return HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
}
public static <T> T GET(String url, Map<String, Object> params, Class<T> clazz) {
String response = HttpUtil.get(url, params);
return JSONObject.parseObject(response, clazz);
@ -39,9 +46,4 @@ public class HttpUtils<T> {
String response = HttpUtil.post(url, paramMap);
return JSONObject.parseObject(response, clazz);
}
public static void main(String[] args) {
}
}