📄 displayque.java
字号:
import java.io.*;
import java.util.LinkedList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
class Timing extends JLabel{
int t;
DisplayQue dq;
Font font = new Font("宋体",Font.BOLD,30);
java.util.Timer _timer=new java.util.Timer();
java.util.TimerTask task=new java.util.TimerTask(){
public void run(){
NumberFormat n=NumberFormat.getInstance();
n.setMinimumIntegerDigits(2);
int h,m,s;
String tt;
t=t-1;
if(t>0){
h=t/3600;
m=t%3600/60;
s=t%60;
setText("离考试结束还有: "+n.format(h)+":"+n.format(m)+":"+n.format(s));
}
else{
setText("TIME'S UP!");
cancel();
dq.handIn();
}
}
};
public Timing(int tt,DisplayQue d) {
this.t=tt*60;
dq=d;
setFont(font);
_timer.schedule(task,1000,1000);
}
}
class ForSingle extends JPanel{
final ButtonGroup ansGroup=new ButtonGroup();
Question que;
public ForSingle(Question q){
setLayout(new BorderLayout());
que=q;
JTextArea jl=new JTextArea(q.que);
jl.setEditable(false);
JPanel jp=new JPanel();
JRadioButton radioButton;
add(jl,BorderLayout.CENTER);
jp.add(radioButton=new JRadioButton(" A "));
radioButton.setActionCommand("A");
ansGroup.add(radioButton);
jp.add(radioButton=new JRadioButton(" B "));
radioButton.setActionCommand("B");
ansGroup.add(radioButton);
jp.add(radioButton=new JRadioButton(" C "));
radioButton.setActionCommand("C");
ansGroup.add(radioButton);
jp.add(radioButton=new JRadioButton(" D "));
radioButton.setActionCommand("D");
ansGroup.add(radioButton);
ansGroup.add(radioButton=new JRadioButton("unchoose"));
radioButton.setActionCommand("E");
radioButton.setSelected(true);
add(jp,BorderLayout.SOUTH);
}
public String getAnswer(){
String answer=ansGroup.getSelection().getActionCommand();
return answer;
}
public int judge(){
String a=getAnswer();
if(a.length()==0){
return 0;
}
else{
if(a.equals(que.ans)==true){
return que.point;
}
else{
return 0;
}
}
}
}
class ForMulti extends JPanel{
final JPanel ansPanel=new JPanel();
Question que;
public ForMulti(Question q){
setLayout(new BorderLayout());
que=q;
JTextArea jl=new JTextArea(q.que);
jl.setEditable(false);
JCheckBox checkBox;
add(jl,BorderLayout.CENTER);
ansPanel.add(checkBox=new JCheckBox(" A "));
checkBox.setActionCommand("A");
ansPanel.add(checkBox=new JCheckBox(" B "));
checkBox.setActionCommand("B");
ansPanel.add(checkBox=new JCheckBox(" C "));
checkBox.setActionCommand("C");
ansPanel.add(checkBox=new JCheckBox(" D "));
checkBox.setActionCommand("D");
add(ansPanel,BorderLayout.SOUTH);
}
public String getAnswer(){
StringBuffer sb=new StringBuffer();
Component[] components=ansPanel.getComponents();
for(int i=0;i<components.length;i++){
JCheckBox cb=(JCheckBox)components[i];
if(cb.isSelected()){
sb.append(cb.getActionCommand());
}
}
String answer=sb.toString();
return answer;
}
public int judge(){
String a=getAnswer();
if(a.length()==0){
return 0;
}
else{
if(a.equals(que.ans)==true){
return que.point;
}
else{
return 0;
}
}
}
}
class ForJudge extends JPanel{
final ButtonGroup ansGroup=new ButtonGroup();
Question que;
public ForJudge(Question q){
setLayout(new BorderLayout());
que=q;
JTextArea jl=new JTextArea(q.que);
jl.setEditable(false);
JPanel jp=new JPanel();
JRadioButton radioButton;
add(jl,BorderLayout.CENTER);
jp.add(radioButton=new JRadioButton(" T "));
radioButton.setActionCommand("T");
ansGroup.add(radioButton);
jp.add(radioButton=new JRadioButton(" F "));
radioButton.setActionCommand("F");
ansGroup.add(radioButton);
ansGroup.add(radioButton=new JRadioButton("unchoose"));
radioButton.setActionCommand("E");
radioButton.setSelected(true);
add(jp,BorderLayout.SOUTH);
}
public String getAnswer(){
String answer=ansGroup.getSelection().getActionCommand();
return answer;
}
public int judge(){
String a=getAnswer();
if(a.length()==0){
return 0;
}
else{
if(a.equals(que.ans)==true){
return que.point;
}
else{
return 0;
}
}
}
}
//------------------------------------------------------------------------------------------------
public class DisplayQue{
JScrollPane jsp=new JScrollPane();
LinkedList queList,paneList=new LinkedList();
Timing tt;
_Paper paper;
Question q;
int i,s,m,j,score;
ForSingle fs;
ForMulti fm;
ForJudge fj;
JButton jb=new JButton("我要交了");
JLabel title;
JLabel sjl=new JLabel("单选题");
JLabel mjl=new JLabel("多选题");
JLabel jjl=new JLabel("判断题");
JPanel jp=new JPanel();
JPanel ftt=new JPanel();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
public DisplayQue(){
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("paper/Fuck.pap"));
paper=(_Paper)in.readObject();
queList=paper.list;
int i=queList.size();
System.out.println(i);
}catch(Exception e){
System.out.print("Error: "+e);
System.exit(0);
}
jp.setLayout(gbl);
gbc.gridx=0;
gbc.gridy=0;
jp.add(title=new JLabel(paper.name),gbc);
gbc.gridy=GridBagConstraints.RELATIVE;
gbc.weightx=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
jp.add(sjl,gbc);
for(i=0;i<queList.size();i++){
q=(Question)queList.get(i);
if(q.type.equals("single")==true){
fs=new ForSingle(q);
jp.add(fs,gbc);
paneList.add(fs);
s++;
}
}
jp.add(mjl,gbc);
for(i=0;i<queList.size();i++){
q=(Question)queList.get(i);
if(q.type.equals("multi")==true){
fm=new ForMulti(q);
jp.add(fm,gbc);
paneList.add(fm);
m++;
}
}
jp.add(jjl,gbc);
for(i=0;i<queList.size();i++){
q=(Question)queList.get(i);
if(q.type.equals("judge")==true){
fj=new ForJudge(q);
jp.add(fj,gbc);
paneList.add(fj);
j++;
}
}
JPanel fjb=new JPanel();
fjb.add(jb);
jp.add(fjb,gbc);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
handIn();
tt.task.cancel();
}
});
ftt.add(tt=new Timing(paper.time,this));
}
public int getScore(){
int score=0;
for(i=0;i<paneList.size();i++){
if(i<s){
fs=(ForSingle)paneList.get(i);
score=score+fs.judge();
}
else if(i<(s+m)){
fm=(ForMulti)paneList.get(i);
score=score+fm.judge();
}
else{
fj=(ForJudge)paneList.get(i);
score=score+fj.judge();
}
}
return score;
}
public void handIn(){
int s=getScore();
JOptionPane.showMessageDialog(new Frame(),"你的分数是"+s);
}
public static void main(String[] args){
MotherFrame mf=new MotherFrame();
DisplayQue dq=new DisplayQue();
mf.getContentPane().add(new JScrollPane(dq.jp),BorderLayout.CENTER);
mf.getContentPane().add(dq.ftt,BorderLayout.NORTH);
mf.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -