📄 studentframe.java
字号:
package com.svse.view;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import java.awt.HeadlessException;
import javax.swing.ButtonGroup;
import com.svse.bean.QuestionBean;
import java.util.ArrayList;
import com.svse.bean.QuestionBean;
public class StudentFrame extends JFrame {
TypeFrame typea;
public StudentFrame() {
try {
jbInit();
setDefaultCloseOperation(EXIT_ON_CLOSE);
//设置窗体的大小
this.setSize(400, 499);
//让窗体居中
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
//设置可见度
this.setVisible(true);
} catch (Exception exception) {
exception.printStackTrace();
}
}
public StudentFrame(TypeFrame TypeFrame) {
this();
this.typea = TypeFrame;
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
lbl_topTitle.setFont(new java.awt.Font("宋体", Font.BOLD, 16));
lbl_topTitle.setText("欢迎使用考试题库系统");
lbl_topTitle.setBounds(new Rectangle(115, 19, 170, 27));
lbl_title.setText("题干:");
lbl_title.setBounds(new Rectangle(54, 86, 46, 21));
txt_title.setEditable(false);
txt_title.setLineWrap(true);
txt_title.setBounds(new Rectangle(115, 64, 192, 65));
rad_optionA.setText("选项A:");
rad_optionA.setBounds(new Rectangle(28, 146, 79, 23));
txt_optionA.setEditable(false);
txt_optionA.setBounds(new Rectangle(115, 147, 192, 21));
txt_optionB.setEditable(false);
txt_optionB.setBounds(new Rectangle(115, 184, 192, 21));
rad_optionB.setText("选项B:");
rad_optionB.setBounds(new Rectangle(28, 183, 79, 23));
txt_optionC.setEditable(false);
txt_optionC.setBounds(new Rectangle(115, 223, 192, 21));
rad_optionC.setText("选项C:");
rad_optionC.setBounds(new Rectangle(28, 222, 79, 23));
txt_optionD.setEditable(false);
txt_optionD.setBounds(new Rectangle(115, 262, 192, 21));
rad_optionD.setText("选项D:");
rad_optionD.setBounds(new Rectangle(28, 261, 79, 23));
btn_top.setBounds(new Rectangle(112, 301, 83, 25));
btn_top.setText("上一题");
btn_top.addActionListener(new StudentFrame_btn_top_actionAdapter(this));
btn_next.setBounds(new Rectangle(215, 301, 83, 25));
btn_next.setText("下一题");
btn_next.addActionListener(new StudentFrame_btn_next_actionAdapter(this));
btn_submit.setBounds(new Rectangle(112, 339, 83, 25));
btn_submit.setText("交 卷");
btn_submit.addActionListener(new StudentFrame_btn_submit_actionAdapter(this));
btn_back.setBounds(new Rectangle(215, 339, 83, 25));
btn_back.setText("返 回");
btn_back.addActionListener(new StudentFrame_btn_back_actionAdapter(this));
this.getContentPane().add(lbl_topTitle);
this.getContentPane().add(txt_optionA);
this.getContentPane().add(txt_title);
this.getContentPane().add(txt_optionB);
this.getContentPane().add(txt_optionC);
this.getContentPane().add(txt_optionD);
this.getContentPane().add(btn_top);
this.getContentPane().add(btn_submit);
this.getContentPane().add(btn_back);
this.getContentPane().add(btn_next);
this.getContentPane().add(rad_optionD);
this.getContentPane().add(rad_optionA);
this.getContentPane().add(rad_optionB);
this.getContentPane().add(rad_optionC);
this.getContentPane().add(lbl_title);
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(rad_optionA);
buttonGroup1.add(rad_optionB);
buttonGroup1.add(rad_optionC);
buttonGroup1.add(rad_optionD);
}
JLabel lbl_topTitle = new JLabel();
JLabel lbl_title = new JLabel();
JTextArea txt_title = new JTextArea();
JRadioButton rad_optionA = new JRadioButton();
JTextField txt_optionA = new JTextField();
JTextField txt_optionB = new JTextField();
JRadioButton rad_optionB = new JRadioButton();
JTextField txt_optionC = new JTextField();
JRadioButton rad_optionC = new JRadioButton();
JTextField txt_optionD = new JTextField();
JRadioButton rad_optionD = new JRadioButton();
JButton btn_top = new JButton();
JButton btn_next = new JButton();
JButton btn_submit = new JButton();
JButton btn_back = new JButton();
ArrayList<QuestionBean> list;
int index = 0;
int i = 0;
//上一题
public void btn_top_actionPerformed(ActionEvent actionEvent) {
index--;
if (index < 0) {
index = 0;
JOptionPane.showMessageDialog(this, "这是第一题!");
}
this.showInfo();
}
//下一题
String answer;
String values;
public void btn_next_actionPerformed(ActionEvent actionEvent) {
answer = list.get(index).getAnswer();
values = (String) typea.cbo_answerWise.getSelectedItem();
if (values.equals("考试")) {
this.test();
this.btn_top.setEnabled(false);
} else {
this.practice();
}
if (index > list.size() - 1) {
index = list.size() - 1;
this.btn_next.setEnabled(false);
this.btn_top.setEnabled(false);
JOptionPane.showMessageDialog(this, "已答题完毕!");
}
this.showInfo();
}
//判断用户输入的答案是否正确(考试)
private void test() throws HeadlessException {
if (this.rad_optionA.isSelected() ||
this.rad_optionB.isSelected() ||
this.rad_optionC.isSelected() ||
this.rad_optionD.isSelected()) {
if (this.rad_optionA.isSelected() && answer.equals("A")) {
i++;
} else if (this.rad_optionB.isSelected() && answer.equals("B")) {
i++;
} else if (this.rad_optionC.isSelected() && answer.equals("C")) {
i++;
} else if (this.rad_optionD.isSelected() && answer.equals("D")) {
i++;
}
index++;
} else {
JOptionPane.showMessageDialog(this, "请选择!");
}
}
//判断用户选择的答案是否正确(练习)
private void practice() throws HeadlessException {
if (this.rad_optionA.isSelected() == false &&
this.rad_optionB.isSelected() == false &&
this.rad_optionC.isSelected() == false &&
this.rad_optionD.isSelected() == false) {
JOptionPane.showMessageDialog(this, "请选择!");
} else {
if (this.rad_optionA.isSelected() && answer.equals("A")) {
index++;
} else if (this.rad_optionB.isSelected() && answer.equals("B")) {
index++;
} else if (this.rad_optionC.isSelected() && answer.equals("C")) {
index++;
} else if (this.rad_optionD.isSelected() && answer.equals("D")) {
index++;
} else {
JOptionPane.showMessageDialog(this, "你答错了!");
}
}
}
//给文本框赋值
public void showInfo() {
this.txt_title.setText(list.get(index).getTitle());
this.txt_optionA.setText(list.get(index).getOptionA());
this.txt_optionB.setText(list.get(index).getOptionB());
this.txt_optionC.setText(list.get(index).getOptionC());
this.txt_optionD.setText(list.get(index).getOptionD());
}
//交卷按钮
public void btn_submit_actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "您答对了" + i + "道题!");
}
//返回
//返回按扭的单击事件
public void btn_back_actionPerformed(ActionEvent e) {
this.typea.setVisible(true);
this.setVisible(false);
}
}
class StudentFrame_btn_back_actionAdapter implements ActionListener {
private StudentFrame adaptee;
private ActionEvent e;
StudentFrame_btn_back_actionAdapter(StudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.btn_back_actionPerformed(e);
}
}
class StudentFrame_btn_submit_actionAdapter implements ActionListener {
private StudentFrame adaptee;
private ActionEvent e;
StudentFrame_btn_submit_actionAdapter(StudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.btn_submit_actionPerformed(e);
}
}
class StudentFrame_btn_next_actionAdapter implements ActionListener {
private StudentFrame adaptee;
private ActionEvent e;
StudentFrame_btn_next_actionAdapter(StudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.btn_next_actionPerformed(e);
}
}
class StudentFrame_btn_top_actionAdapter implements ActionListener {
private StudentFrame adaptee;
private ActionEvent e;
StudentFrame_btn_top_actionAdapter(StudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.btn_top_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -