视图
欢迎界面
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 = 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.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 {