- 考试UI的实现
代码如下:
package com.zjipc.kaoshi.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class ExamFrame extends JFrame {
public static void main(String[] args) {
ExamFrame window = new ExamFrame();
window.setVisible(true);
}
private JLabel examInfo1;
private JTextArea questionArea;
private JButton prev;
private JButton next;
private JButton send;
private Option[] options = new Option[4];
private JLabel questionCount;
private JLabel timer;
public ExamFrame() {
init();
}
private void init() {
setTitle("ZJIPC认证软件开发工程师考试平台");
setSize(650, 400);
try {
Image image = ImageIO.read(getClass().getResource("../images/inco.jpg"));
setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(ExamFrame.this, "确定要退出吗?", "离开系统",
JOptionPane.YES_NO_OPTION)) {
System.exit(0);
}
}
});
setResizable(false);
setLocationRelativeTo(null);
add(createContentPanel());
}
private Component createContentPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new EmptyBorder(0, 8, 0, 8));
ImageIcon icon = new ImageIcon(getClass().getResource("../images/exam_title.png"));
panel.add(BorderLayout.NORTH, new JLabel(icon));
panel.add(BorderLayout.CENTER, createCenterPanel());
panel.add(BorderLayout.SOUTH, createBottomPanel());
return panel;
}
private Component createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout());
examInfo1 = new JLabel("姓名: 马小明 考号:1001 开始时间:120分钟 开始科目:Java初级工程师认证考试 题目数量:50", JLabel.CENTER);
panel.add(BorderLayout.NORTH, examInfo1);
panel.add(BorderLayout.CENTER, createQuestionsPanel());
panel.add(BorderLayout.SOUTH, createOptionsPanel());
return panel;
}
private Component createQuestionsPanel() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("题目"));
Font font = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 15);
questionArea = new JTextArea();
questionArea.setFont(font);
questionArea.setText("问题\nA.\nB.\nC\nD.");
questionArea.setLineWrap(true);
questionArea.setEditable(false);
pane.getViewport().add(questionArea);
return pane;
}
private Component createOptionsPanel() {
JPanel panel = new JPanel(new FlowLayout());
Option a = new Option(0, "A");
Option b = new Option(1, "B");
Option c = new Option(2, "C");
Option d = new Option(3, "D");
options[0] = a;
options[1] = b;
options[2] = c;
options[3] = d;
panel.add(a);
panel.add(b);
panel.add(c);
panel.add(d);
return panel;
}
private Component createBottomPanel() {
JPanel panel = new JPanel(new BorderLayout());
String info = "题目:50道题中的第1道题";
questionCount = new JLabel(info);
panel.add(BorderLayout.WEST, questionCount);
panel.add(BorderLayout.CENTER, createBtnPanel());
String limitTime = "剩余时间119分钟";
timer = new JLabel(limitTime);
panel.add(BorderLayout.EAST, timer);
return panel;
}
private Component createBtnPanel() {
JPanel panel = new JPanel(new FlowLayout());
prev = new JButton("<<上一题");
next = new JButton("下一题>>");
send = new JButton("交卷");
panel.add(prev);
panel.add(next);
panel.add(send);
return panel;
}
private class Option extends JCheckBox {
int value;
public Option(int val, String txt) {
super(txt);
this.value = val;
}
}
}