|
|
|
|
@ -2,6 +2,8 @@ package com.smartlibrary.gui;
|
|
|
|
|
|
|
|
|
|
import com.smartlibrary.service.BookService;
|
|
|
|
|
import com.smartlibrary.model.Book;
|
|
|
|
|
import com.smartlibrary.ai.AIService;
|
|
|
|
|
import com.smartlibrary.ai.AIModelFactory;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import javax.swing.table.DefaultTableModel;
|
|
|
|
|
@ -10,14 +12,19 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 智能图书管理系统 - 桌面图形界面
|
|
|
|
|
* 集成 AI 聊天和语音功能
|
|
|
|
|
*/
|
|
|
|
|
public class GUIApplication extends JFrame {
|
|
|
|
|
private final BookService bookService;
|
|
|
|
|
private final AIService aiService;
|
|
|
|
|
private JTable bookTable;
|
|
|
|
|
private DefaultTableModel tableModel;
|
|
|
|
|
private JTextArea chatArea;
|
|
|
|
|
private JTextField chatInput;
|
|
|
|
|
|
|
|
|
|
public GUIApplication() {
|
|
|
|
|
this.bookService = new BookService();
|
|
|
|
|
this.aiService = AIModelFactory.getDefaultService();
|
|
|
|
|
initUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -33,8 +40,8 @@ public class GUIApplication extends JFrame {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initUI() {
|
|
|
|
|
setTitle("智能图书管理系统 - GUI 版本");
|
|
|
|
|
setSize(900, 600);
|
|
|
|
|
setTitle("智能图书管理系统 - GUI 版本 (AI增强)");
|
|
|
|
|
setSize(1100, 700);
|
|
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
|
setLocationRelativeTo(null);
|
|
|
|
|
|
|
|
|
|
@ -48,11 +55,21 @@ public class GUIApplication extends JFrame {
|
|
|
|
|
// 工具栏
|
|
|
|
|
mainPanel.add(createToolBar(), BorderLayout.NORTH);
|
|
|
|
|
|
|
|
|
|
// 图书表格
|
|
|
|
|
mainPanel.add(createTablePanel(), BorderLayout.CENTER);
|
|
|
|
|
// 左侧图书表格
|
|
|
|
|
JPanel leftPanel = new JPanel(new BorderLayout());
|
|
|
|
|
leftPanel.add(createTablePanel(), BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
// 右侧 AI 聊天面板
|
|
|
|
|
JPanel rightPanel = createAIChatPanel();
|
|
|
|
|
|
|
|
|
|
// 分割面板
|
|
|
|
|
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
|
|
|
|
|
splitPane.setResizeWeight(0.7);
|
|
|
|
|
splitPane.setDividerLocation(700);
|
|
|
|
|
mainPanel.add(splitPane, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
// 状态栏
|
|
|
|
|
JLabel statusBar = new JLabel("就绪");
|
|
|
|
|
JLabel statusBar = new JLabel("就绪 | AI 助手已启用");
|
|
|
|
|
statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
|
|
|
|
|
mainPanel.add(statusBar, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
@ -77,6 +94,14 @@ public class GUIApplication extends JFrame {
|
|
|
|
|
switchMenu.add(new JMenuItem("App 二维码")).addActionListener(e -> showAppQRCode());
|
|
|
|
|
menuBar.add(switchMenu);
|
|
|
|
|
|
|
|
|
|
// AI 菜单
|
|
|
|
|
JMenu aiMenu = new JMenu("AI 功能");
|
|
|
|
|
aiMenu.add(new JMenuItem("智能推荐")).addActionListener(e -> showAIRecommendation());
|
|
|
|
|
aiMenu.add(new JMenuItem("图书问答")).addActionListener(e -> chatInput.requestFocus());
|
|
|
|
|
aiMenu.addSeparator();
|
|
|
|
|
aiMenu.add(new JMenuItem("语音输入")).addActionListener(e -> startVoiceInput());
|
|
|
|
|
menuBar.add(aiMenu);
|
|
|
|
|
|
|
|
|
|
// 帮助菜单
|
|
|
|
|
JMenu helpMenu = new JMenu("帮助");
|
|
|
|
|
helpMenu.add(new JMenuItem("关于")).addActionListener(e -> showAbout());
|
|
|
|
|
@ -223,7 +248,103 @@ public class GUIApplication extends JFrame {
|
|
|
|
|
|
|
|
|
|
private void showAbout() {
|
|
|
|
|
JOptionPane.showMessageDialog(this,
|
|
|
|
|
"智能图书管理系统 v1.0.0\n\n四端统一架构:\n- CLI 命令行\n- GUI 桌面\n- Web 浏览器\n- App 移动端",
|
|
|
|
|
"智能图书管理系统 v1.0.0 (AI增强版)\n\n四端统一架构:\n- CLI 命令行\n- GUI 桌面\n- Web 浏览器\n- App 移动端\n\nAI功能:\n- 智能图书推荐\n- 图书问答助手\n- 语音输入识别",
|
|
|
|
|
"关于", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ========== AI 功能 ==========
|
|
|
|
|
|
|
|
|
|
private JPanel createAIChatPanel() {
|
|
|
|
|
JPanel panel = new JPanel(new BorderLayout(5, 5));
|
|
|
|
|
panel.setBorder(BorderFactory.createTitledBorder("AI 图书助手"));
|
|
|
|
|
panel.setPreferredSize(new Dimension(350, 0));
|
|
|
|
|
|
|
|
|
|
// 聊天记录区域
|
|
|
|
|
chatArea = new JTextArea();
|
|
|
|
|
chatArea.setEditable(false);
|
|
|
|
|
chatArea.setLineWrap(true);
|
|
|
|
|
chatArea.setWrapStyleWord(true);
|
|
|
|
|
chatArea.append("AI助手: 您好!我是智能图书助手,可以帮您:\n");
|
|
|
|
|
chatArea.append("• 推荐图书\n");
|
|
|
|
|
chatArea.append("• 查询馆藏\n");
|
|
|
|
|
chatArea.append("• 解答阅读问题\n\n");
|
|
|
|
|
JScrollPane scrollPane = new JScrollPane(chatArea);
|
|
|
|
|
panel.add(scrollPane, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
// 输入面板
|
|
|
|
|
JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
|
|
|
|
|
chatInput = new JTextField();
|
|
|
|
|
chatInput.addActionListener(e -> sendChatMessage());
|
|
|
|
|
|
|
|
|
|
JButton sendBtn = new JButton("发送");
|
|
|
|
|
sendBtn.addActionListener(e -> sendChatMessage());
|
|
|
|
|
|
|
|
|
|
JButton voiceBtn = new JButton("🎤");
|
|
|
|
|
voiceBtn.setToolTipText("语音输入");
|
|
|
|
|
voiceBtn.addActionListener(e -> startVoiceInput());
|
|
|
|
|
|
|
|
|
|
JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 0));
|
|
|
|
|
btnPanel.add(voiceBtn);
|
|
|
|
|
btnPanel.add(sendBtn);
|
|
|
|
|
|
|
|
|
|
inputPanel.add(chatInput, BorderLayout.CENTER);
|
|
|
|
|
inputPanel.add(btnPanel, BorderLayout.EAST);
|
|
|
|
|
panel.add(inputPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
return panel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendChatMessage() {
|
|
|
|
|
String message = chatInput.getText().trim();
|
|
|
|
|
if (message.isEmpty()) return;
|
|
|
|
|
|
|
|
|
|
chatArea.append("我: " + message + "\n");
|
|
|
|
|
chatInput.setText("");
|
|
|
|
|
|
|
|
|
|
// 异步调用 AI
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String response = aiService.chat(message);
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
chatArea.append("AI助手: " + response + "\n\n");
|
|
|
|
|
chatArea.setCaretPosition(chatArea.getDocument().getLength());
|
|
|
|
|
});
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
chatArea.append("AI助手: 抱歉,处理请求时出错了。\n\n");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showAIRecommendation() {
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
String recommendation = aiService.getRecommendation("GUI_USER");
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
chatArea.append("AI助手 [智能推荐]:\n" + recommendation + "\n\n");
|
|
|
|
|
chatArea.setCaretPosition(chatArea.getDocument().getLength());
|
|
|
|
|
});
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "获取推荐失败: " + e.getMessage(),
|
|
|
|
|
"错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startVoiceInput() {
|
|
|
|
|
JOptionPane.showMessageDialog(this,
|
|
|
|
|
"语音输入功能说明:\n\n1. 点击确定开始录音\n2. 说出您的问题\n3. 再次点击停止录音\n\n注:需要麦克风权限",
|
|
|
|
|
"语音输入", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
|
|
|
|
|
// 模拟语音输入(实际需要集成语音识别服务)
|
|
|
|
|
String simulatedVoice = JOptionPane.showInputDialog(this,
|
|
|
|
|
"模拟语音输入(请输入要说的内容):", "语音输入", JOptionPane.QUESTION_MESSAGE);
|
|
|
|
|
if (simulatedVoice != null && !simulatedVoice.isEmpty()) {
|
|
|
|
|
chatInput.setText(simulatedVoice);
|
|
|
|
|
sendChatMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|