You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.1 KiB
105 lines
3.1 KiB
package view;
|
|
import controller.MainController;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class MainFrame extends JFrame {
|
|
private MainController controller;
|
|
|
|
public MainFrame(MainController controller) {
|
|
this.controller = controller;
|
|
initializeWindow();
|
|
createComponents();
|
|
setVisible(true);
|
|
}
|
|
|
|
private void initializeWindow() {
|
|
setTitle("数学学习系统 - 主界面");
|
|
setSize(300, 250);
|
|
setLocationRelativeTo(null);
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
setResizable(false);
|
|
}
|
|
|
|
private void createComponents() {
|
|
JPanel panel = new JPanel(new GridLayout(5, 1, 10, 10));
|
|
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
|
|
|
JLabel titleLabel = new JLabel("选择考试难度", JLabel.CENTER);
|
|
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
|
|
|
JButton primaryBtn = new JButton("小学");
|
|
JButton middleBtn = new JButton("初中");
|
|
JButton highBtn = new JButton("高中");
|
|
JButton logoutBtn = new JButton("退出登录");
|
|
|
|
// 设置按钮字体
|
|
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
|
|
primaryBtn.setFont(buttonFont);
|
|
middleBtn.setFont(buttonFont);
|
|
highBtn.setFont(buttonFont);
|
|
logoutBtn.setFont(buttonFont);
|
|
|
|
// 设置按钮颜色
|
|
primaryBtn.setBackground(new Color(173, 216, 230)); // 浅蓝色
|
|
middleBtn.setBackground(new Color(144, 238, 144)); // 浅绿色
|
|
highBtn.setBackground(new Color(255, 182, 193)); // 浅粉色
|
|
logoutBtn.setBackground(new Color(240, 240, 240)); // 浅灰色
|
|
|
|
// 按钮事件监听
|
|
primaryBtn.addActionListener(e -> {
|
|
setVisible(false); // 隐藏主界面
|
|
controller.startExam("小学");
|
|
});
|
|
|
|
middleBtn.addActionListener(e -> {
|
|
setVisible(false); // 隐藏主界面
|
|
controller.startExam("初中");
|
|
});
|
|
|
|
highBtn.addActionListener(e -> {
|
|
setVisible(false); // 隐藏主界面
|
|
controller.startExam("高中");
|
|
});
|
|
|
|
logoutBtn.addActionListener(e -> {
|
|
int result = JOptionPane.showConfirmDialog(
|
|
this,
|
|
"确定要退出登录吗?",
|
|
"确认退出",
|
|
JOptionPane.YES_NO_OPTION,
|
|
JOptionPane.QUESTION_MESSAGE
|
|
);
|
|
|
|
if (result == JOptionPane.YES_OPTION) {
|
|
controller.logout();
|
|
}
|
|
});
|
|
|
|
// 添加组件到面板
|
|
panel.add(titleLabel);
|
|
panel.add(primaryBtn);
|
|
panel.add(middleBtn);
|
|
panel.add(highBtn);
|
|
panel.add(logoutBtn);
|
|
|
|
add(panel);
|
|
}
|
|
|
|
/**
|
|
* 显示主界面(用于从考试界面返回时调用)
|
|
*/
|
|
public void showFrame() {
|
|
setVisible(true);
|
|
toFront(); // 确保窗口在前台
|
|
requestFocus(); // 请求焦点
|
|
}
|
|
|
|
/**
|
|
* 重写 dispose 方法,确保程序正确退出
|
|
*/
|
|
@Override
|
|
public void dispose() {
|
|
super.dispose();
|
|
}
|
|
} |