【Java实战小项目】考试系统


视图

欢迎界面

package com.zzxx.exam.ui;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

/**
 * 闪屏
 */
public class WelcomeWindow extends JWindow {
   

    public WelcomeWindow() {
   
        init();
    }

    private void init() {
   
        setSize(430, 300);
        JPanel pane = new JPanel(new BorderLayout());
        ImageIcon ico = new ImageIcon(getClass().getResource("pic/welcome.png"));
        JLabel l = new JLabel(ico);
        pane.add(BorderLayout.CENTER, l);
        pane.setBorder(new LineBorder(Color.GRAY));
        setContentPane(pane);
        setLocationRelativeTo(null);
    }
}

登陆界面

package com.zzxx.exam.ui;

import com.zzxx.exam.entity.ClientContext;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 登录界面 是一个具体窗口框
 */
public class LoginFrame extends JFrame {
   
    private static final long serialVersionUID = 1L;
    private ClientContext clientContext;
    public LoginFrame() {
   
        init();
    }

    /**
     * 初始化界面组件和布局的
     */
    private void init() {
   
        this.setTitle("登录系统");
        JPanel contentPane = createContentPane();
        this.setContentPane(contentPane);
        // 必须先设大小后居中
        setSize(300, 220);
        setLocationRelativeTo(null);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
   
            public void windowClosing(WindowEvent e) {
   

            }
        });
    }

    private JPanel createContentPane() {
   
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(8, 8, 8, 8));
        p.add(BorderLayout.NORTH, new JLabel("登录考试系统", JLabel.CENTER));
        p.add(BorderLayout.CENTER, createCenterPane());
        p.add(BorderLayout.SOUTH, createBtnPane());
        return p;
    }

    private JPanel createBtnPane() {
   
        JPanel p = new JPanel(new FlowLayout());
        JButton login = new JButton("Login");
        JButton cancel = new JButton("Cancel");
        p.add(login);
        p.add(cancel);

        getRootPane().setDefaultButton(login);

        login.addActionListener(new ActionListener() {
   
            public void actionPerformed(ActionEvent e) {
   
                //需要控制器对象
                clientContext.login();
            }
        });

        cancel.addActionListener(new ActionListener() {
   
            public void actionPerformed(ActionEvent e) {
   
                isExit();
            }
        });
        return p;
    }
    public void isExit(){
   
        int res = JOptionPane.showConfirmDialog(null, "是否退出", "警告", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
    // 点击“是”后执行这个代码块
            System.exit(0);
        } else {
   // 点击“否”后执行这个代码块
            return;
        }
    }

    private JPanel createCenterPane() {
   
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(8, 0, 0, 0));
        p.add(BorderLayout.NORTH, createIdPwdPane());
        message = new JLabel("", JLabel.CENTER);
        p.add(BorderLayout.SOUTH, message);
        return p;
    }

    private JPanel createIdPwdPane() {
   
        JPanel p = new JPanel(new GridLayout(2, 1, 0, 6));
        p.add(createIdPane());
        p.add(createPwdPane());
        return p;
    }
    private JTextField idField;
    private JPanel createIdPane() {
   
        JPanel p = new JPanel(new BorderLayout(6, 0));
        p.add(BorderLayout.WEST, new JLabel("编号:"));
        JTextField idField = new JTextField();
        p.add(BorderLayout.CENTER, idField);
        this.idField = idField;
        return p;
    }

    /**
     * 简单工厂方法, 封装的复杂对象的创建过程, 返回一个对象实例
     */
    private JPasswordField pwdField;
    private JPanel createPwdPane() {
   
        JPanel p = new JPanel(new BorderLayout(6, 0));
        p.add(BorderLayout.WEST, new JLabel("密码:"));
        JPasswordField pwdField = new JPasswordField();
        pwdField.enableInputMethods(true);
        p.add(BorderLayout.CENTER, pwdField);
        this.pwdField = pwdField;
        return p;
    }

    private JLabel message;

    public int getID() throws NumberFormatException{
   
        return Integer.parseInt(idField.getText());
    }
    public String getPasswd(){
   
        return new String(pwdField.getPassword());
    }
    public void changeMessage(String message){
   
        this.message.setText(message);
    }

    public void setClientContext(ClientContext clientContext) {
   
        this.clientContext = clientContext;
    }
}

主菜单界面

package com.zzxx.exam.ui;

import com.zzxx.exam.entity.ClientContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 主菜单界面
 */
public class MenuFrame extends JFrame {
   
    private ClientContext clientContext;

    public MenuFrame() {
   
        init();
    }

    private void init() {
   
        setTitle("指针信息在线测评");
        setSize(600, 400);
        setContentPane(createContentPane());
        setLocationRelativeTo(null);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
   
            public void windowClosing(WindowEvent e) {
   

            }
        });
    }

    private JPanel createContentPane() {
   
        JPanel pane = new JPanel(new BorderLayout());

        ImageIcon icon = new ImageIcon(this.getClass().getResource("pic/title.png"));

        pane.add(BorderLayout.NORTH, new JLabel(icon));

        pane.add(BorderLayout.CENTER, createMenuPane());

        pane.add(BorderLayout.SOUTH, new JLabel("指针信息--版权所有 盗版必究", JLabel.RIGHT));

        return pane;
    }
    private JLabel info; // 记录用户的信息
    private JPanel createMenuPane() {
   
        JPanel pane = new JPanel(new BorderLayout());
        // 务必将 info 引用到界面控件对象
        info = new JLabel("XXX 同学您好!", JLabel.CENTER);

        pane.add(BorderLayout.NORTH, info);
        pane.add(BorderLayout.CENTER, createBtnPane());

        return pane;
    }

    private JPanel createBtnPane() {
   
        JPanel pane = new JPanel(new FlowLayout());
        JButton start = createImgBtn("pic/exam.png", "开始");
        JButton result = createImgBtn("pic/result.png", "分数");
        JButton msg = createImgBtn("pic/message.png", "考试规则");
        JButton exit = createImgBtn("pic/exit.png", "离开");

        pane.add(start);
        pane.add(result);
        pane.add(msg);
        pane.add(exit);

        getRootPane().setDefaultButton(start);

        start.addActionListener(new ActionListener() {
   
            public void actionPerformed(ActionEvent e) {
   
                clientContext.menuTostart();
                clientContext.changeExaminfo();
            }
        });

        msg.addActionListener(new ActionListener() {
   
            public void actionPerformed(ActionEvent arg0) {
   
                clientContext.menuTomsg();
                clientContext.showRules();
            }
        });

        exit.addActionListener(new ActionListener() {
   
            public void actionPerformed(ActionEvent e) {
   
                isExit();
            }
        });

        return pane;
    }

    public void isExit(){
   
        int res = JOptionPane.showConfirmDialog(null, "是否退出", "警告", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
    // 点击“是”后执行这个代码块
            System.exit(0);
        } else {
   // 点击“否”后执行这个代码块
            return;
        }
    }

    // 创建图片按钮的方法
    private JButton createImgBtn(String img, String txt) {
   
        ImageIcon ico = new ImageIcon(this.getClass().getResource(img));

        JButton button = new JButton(txt, ico);
        // button.setIcon(ico);
        // 设置文本相对于图标的垂直位置
        button.setVerticalTextPosition(JButton.BOTTOM);
        // 设置文本相对于图标的水平位置
        button.setHorizontalTextPosition(JButton.CENTER);

        return button;
    }
    public void changeInfo(String name){
   
        this.info.setText(name + "同学您好!");
    }

    public void setClientContext(ClientContext clientContext) {
   
        this.clientContext = clientContext;
    }
}

考试规则界面

package com.zzxx.exam.ui;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 主菜单界面
 */
public class MsgFrame extends JFrame {
   
    
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值