package view;

import backend.model.Questions;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainFrame extends JFrame{
    private JPanel mainPanel;
    private JMenuBar menuBar;
    private JMenu questionsMenu;
    private JMenu examMenu;

    public MainFrame(){
        initView();
        initListener();
    }
    private void initView(){
        //1.设置标题
        setTitle("主界面");
        //2.设置点击关闭时的动作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //3.中间层容器
        add(initPanel());
        //4.不允许窗口改变大小
        setResizable(false);
        //5.自动调整窗口大小 居中显示
        setSize(600,600);
        //pack()；//自动调整窗口大小
        setLocationRelativeTo(null);
        //6.注册监听器
        initListener();
        //7.显示窗口
        setVisible(true);
    }

    private void initListener(){
        questionsMenu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mainPanel.removeAll();
                mainPanel.add(new QuestionsPanel());
                mainPanel.revalidate();
                mainPanel.repaint();
            }
        });
        examMenu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mainPanel.removeAll();
                mainPanel.add(new ExamPanel());
                mainPanel.revalidate();
                mainPanel.repaint();
            }
        });
    }
    private JPanel initPanel(){
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
        setJMenuBar(initMenuBar());
        //默认为单词库界面
        mainPanel.add(new QuestionsPanel());
        mainPanel.setVisible(true);
        return mainPanel;
    }
    private JMenuBar initMenuBar(){
        menuBar = new JMenuBar();
        questionsMenu = new JMenu("问题库管理");
        examMenu = new JMenu("考试管理");
        menuBar.add(questionsMenu);
        menuBar.add(examMenu);
        return menuBar;
    }
    public static void main(String[] args) {
        new MainFrame().initView();
    }
}
