Java 我在使用JFrame打印多个数组的表时遇到问题
我一直试图打印我在代码中要求的数组中的所有信息,但一旦用户不想评估更多的人,我就无法找到一种方法将所有信息打印到表中 我试着使用我在这里看到的一些解决方案,但至少我还不能理解它们 我只需要将Java 我在使用JFrame打印多个数组的表时遇到问题,java,arrays,swing,jframe,Java,Arrays,Swing,Jframe,我一直试图打印我在代码中要求的数组中的所有信息,但一旦用户不想评估更多的人,我就无法找到一种方法将所有信息打印到表中 我试着使用我在这里看到的一些解决方案,但至少我还不能理解它们 我只需要将JOptionPane请求的多个数组中保存的信息全部保存并打印到列表中 import javax.swing.*; import java.awt.*; public class ProyectoFinal extends JFrame { public static voi
JOptionPane
请求的多个数组中保存的信息全部保存并打印到列表中
import javax.swing.*;
import java.awt.*;
public class ProyectoFinal extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
int opcfinal = 0;
int n = 0; //SE DETERMINA EL TAMAñO DEL ARREGLO, PARA ALMACENAR LOS DATOS DE NOMBRE Y CEDULA
String[] name = new String[1000]; // SE CREA UN ARREGLO PARA GUARDAR LOS NOMBRES CON PLAZAS ILIMATADAS PARA NO TENER PROBLEMAS DE COMPILACION
String[] cedula = new String[1000]; // SE CREA UN ARREGLO PARA GUARDAR LOS NUMEROS DE CEDULA CON PLAZAS ILIMTADAS PARA NO TENER PROBLEMAS DE COMPILACION
int[] puntos = new int[1000]; // SE CREA EL ARREGLO QUE ALMACENARA LOS PUNTOS DE LOS ESTUDIANTES EVALUADOS
int[] rescorrect = new int[1000]; // SE CREA EL ARREGLO QUE GUARDARA LA CANTIDAD DE RESPUESTAS CORRECTAS QUE TUVO EL ESTUDIANTE
int[] status = new int[1000]; // SE GUARDA EL VALOR DE TIPO INTEGER QUE DETERMINARA SI EL TEXTO A MOSTRAR SERA APROBADO/RECHAZADO
do { // SE CREA UN CICLO REPETITIVO QUE EVALUE LA OPCION DEL USUARIO EN EL SHOWCONFIRMDIALOG
name[n] = JOptionPane.showInputDialog(null, "Escriba su nombre: ","Instituto Superior Informático Panameño", JOptionPane.QUESTION_MESSAGE);// SE PIDE POR TECLADO EL NOMBRE
cedula[n] = JOptionPane.showInputDialog(null, "Digite su cedula", "Instituto Superior Informático Panameño", JOptionPane.QUESTION_MESSAGE);// SE PIDE POR TECLADO LA CEDULA
for (int i = 0; i<1; i++) { // SE CREA UN CICLO REPETITIVO CON AUMENTO DE 1 PARA EVALUAR LAS PREGUNTAS UNA SOLA VEZ POR ESTUDIANTE Y GUARDAR LOS VALORES DENTRO DE LOS ARREGLOS DECLARADOS
int p1 = Integer.parseInt(JOptionPane.showInputDialog(null, "En programacion, una variable es:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Informacion Almacenada \n2) Un numero \n3) Un Contador", "Pregunta 1", JOptionPane.QUESTION_MESSAGE));
if (p1== 1) { // SE CREA UN CONDICIONAL QUE EVALUA SI LA RESPUESTA ES CORRECTA
puntos[i] = 6 + puntos[i]; // SE AUMENTA LOS PUNTOS EN CASO DE LA RESPUESTA SER CORRECTA
rescorrect[i] = 1 + rescorrect[i]; // SE ALMACENA LA INFORMACION DE QUE EL ESTUDIANTE TUVO UNA RESPUESTA CORRECTA Y SE SUMA A LAS ANTERIORES PARA LLEVAR UN CONTROL.
}else if(p1!=1) { // Y SE CREA LA CONDICION ACERCA DE QUE HACER EN CASO DE QUE LA RESPUESTA SEA INCORRECTA
puntos[i] = puntos[i]; //DE TENER UNA RESPUESTA EQUIVOCADA, LOS PUNTOS SE QUEDAN COMO ESTABAN Y LA CANTIDAD DE RESPUESTAS CORRECTAS NO AUMENTA.
}
int p2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es un hacker?:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Un delincuente informatico \n2) Un programa de Facebook \n3) Experto en Tecnologia ", "Pregunta 2", JOptionPane.QUESTION_MESSAGE));
if (p2==3) {
puntos[i] = 6 + puntos[i];
rescorrect[i] = 1 + rescorrect[i];
}else if(p2!=3) {
puntos[i] = puntos[i];
}
int p3 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es un malware? :" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Software Malicioso \n2) Virus de Celular \n3) Un Trino de un Politico en Campaña ", "Pregunta 3", JOptionPane.QUESTION_MESSAGE));
if (p3== 1) {
puntos[i] = 6 + puntos[i];
rescorrect[i] = 1 + rescorrect[i];
}else if(p3!=1) {
puntos[i] = puntos[i];
}
int p4 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es Python? :" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Una Serpiente \n2) Un Lenguaje de Programacion \n3) Un arma ", "Pregunta 4", JOptionPane.QUESTION_MESSAGE));
if (p4== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 6 + puntos[i];
}else if(p4!=2) {
puntos[i] = puntos[i];
}
int p5 = Integer.parseInt(JOptionPane.showInputDialog(null, "La Ram es:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Capacidad de almacenamiento \n2) Chip de ayuda al Procesador \n3) Rama de la tecnologia que estudia el hardware ", "Pregunta 5", JOptionPane.QUESTION_MESSAGE));
if (p5== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 6 + puntos[i];
}else if(p5!=2) {
puntos[i] = puntos[i];
}
int p6 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es minecraft?" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) VideoJuego de supervivencia \n2) Metodo chino de almacenamiento de datos \n3) Bomba atomica ", "Pregunta 6", JOptionPane.QUESTION_MESSAGE));
if (p6== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 6 + puntos[i];
}else if(p6!=1) {
puntos[i] = puntos[i];
}
int p7 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es el machine learning:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Enseñar a las maquinas a hacer cosas \n2) Programar maquinas para que usen algoritmos \n3) Todo aquello que podemos aprender de una maquina ", "Pregunta 7", JOptionPane.QUESTION_MESSAGE));
if (p7== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 6 + puntos[i];
}else if(p7!=2) {
puntos[i] = puntos[i];
}
int p8 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es Realidad Aumentada:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Tecnologia de inmersion \n2) Protocolo de Mejora a la vista humana \n3) Vision de un entorno fisico al mundo real, a traves de un dispositivo ", "Pregunta 8", JOptionPane.QUESTION_MESSAGE));
if (p8== 3) {
puntos[i] = 6 + puntos[i];
rescorrect[i] = 1 + rescorrect[i];
}else if(p8!=3) {
puntos[i] = puntos[i];
}
int p9 = Integer.parseInt(JOptionPane.showInputDialog(null, "Para que se utilizan los Bit-Coins:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Apostar en Casinos \n2) Realizar Pagos Online de forma segura \n3) Comprar entradas de cine ", "Pregunta 9", JOptionPane.QUESTION_MESSAGE));
if (p9== 2) {
puntos[i] = 6 + puntos[i];
rescorrect[i] = 1 + rescorrect[i];
}else if(p9!=2) {
puntos[i] = puntos[i];
}
int p10 = Integer.parseInt(JOptionPane.showInputDialog(null, "Que es un String:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Una variable de tipo cadena \n2) Una cuerda \n3) Un culumpio ", "Pregunta 10", JOptionPane.QUESTION_MESSAGE));
if (p10== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 6 + puntos[i];
}else if(p10!=1) {
puntos[i] = puntos[i];
}
int p11 = Integer.parseInt(JOptionPane.showInputDialog(null, "El orden de los factores no altera:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) La Formula Cuadratica \n2) El Producto \n3) La Division ", "Pregunta 11", JOptionPane.QUESTION_MESSAGE));
if (p11== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p11!=2) {
puntos[i] = puntos[i];
}
int p12 = Integer.parseInt(JOptionPane.showInputDialog(null, "En la jerarquia de Operadores, cual de los siguientes signos tiene prioridad?:" // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) * \n2) - \n3) + ", "Pregunta 12", JOptionPane.QUESTION_MESSAGE));
if (p12== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p12!=1) {
puntos[i] = puntos[i];
}
int p13 = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuanto vale Pi?: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) 3.1315 \n2) 3.1416 \n3) 3.1415 ", "Pregunta 13", JOptionPane.QUESTION_MESSAGE));
if (p13== 3) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p13!=3) {
puntos[i] = puntos[i];
}
int p14 = Integer.parseInt(JOptionPane.showInputDialog(null, "Como se denomina un Triangulo con sus 3 lados iguales: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Isosceles \n2) Escaleno \n3) Equilatero ", "Pregunta 14", JOptionPane.QUESTION_MESSAGE));
if (p14== 3) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p14!=3) {
puntos[i] = puntos[i];
}
int p15 = Integer.parseInt(JOptionPane.showInputDialog(null, "Como se denomina un Triangulo con sus 3 lados diferentes: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Isosceles \n2) Escaleno \n3) Equilatero ", "Pregunta 15", JOptionPane.QUESTION_MESSAGE));
if (p15== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p15!=2) {
puntos[i] = puntos[i];
}
int p16 = Integer.parseInt(JOptionPane.showInputDialog(null, "What DOB stands for?: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Date of Birth \n2) Date of Bird \n3) Donuts Online Business ", "Pregunta 16", JOptionPane.QUESTION_MESSAGE));
if (p16== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p16!=1) {
puntos[i] = puntos[i];
}
int p17 = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the plural form for Child?: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Childs \n2) Childes \n3) Children ", "Pregunta 17", JOptionPane.QUESTION_MESSAGE));
if (p17== 3) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p17!=3) {
puntos[i] = puntos[i];
}
int p18 = Integer.parseInt(JOptionPane.showInputDialog(null, "English Language haves it's origins from: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) United States \n2) German \n3) British ", "Pregunta 18", JOptionPane.QUESTION_MESSAGE));
if (p18== 2) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p18!=2) {
puntos[i] = puntos[i];
}
int p19 = Integer.parseInt(JOptionPane.showInputDialog(null, "To be Verb for He: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) Is \n2) are \n3) Am ", "Pregunta 19", JOptionPane.QUESTION_MESSAGE));
if (p19== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p19!=1) {
puntos[i] = puntos[i];
}
int p20 = Integer.parseInt(JOptionPane.showInputDialog(null, "What is TOEFL: " // SE PIDE LA RESPUESTA POR TECLADO SEGUN OPCIONES.
+ "\n1) English Test \n2) STD \n3) Cookie brand ", "Pregunta 20", JOptionPane.QUESTION_MESSAGE));
if (p20== 1) {
rescorrect[i] = 1 + rescorrect[i];
puntos[i] = 4 + puntos[i];
}else if(p20!=1) {
puntos[i] = puntos[i];
}
if (puntos[i]>=71) { // SE CREA LA CONDICIONAL PARA DETERMINAR SI EL ESTUDIANTE FUE APROBADO/RECHAZADO
status[i] = 1;
}else if(puntos[i]<71) {
status[i] = 0;
}
opcfinal = JOptionPane.showConfirmDialog(null, "Desea Continuar?"); //DETERMINA SI HAY OTRA PERSONA A EVALUAR O NO.
++n;
//ESTO CIERRA EL CICLO DE PREGUNTAS
}
} while(opcfinal == 0);
Object[][] info = {
{puntos[n],name[n],cedula[n],status[n]}
};
String[] header = {"Estudiante No","Nombre","Cedula","Puntos Obtenidos","Status"};
JTable table = new JTable(info, header);
Font font = new Font("Verdana", Font.PLAIN,12);
table.setFont(font);
table.setRowHeight(n);
JFrame frame = new JFrame();
frame.setSize(600,400);
frame.add(new JScrollPane(table));
frame.setVisible(true);
for (int i = 0; i < 1; i++) {
JOptionPane.showMessageDialog(null, puntos[i]);
}
}
}
import javax.swing.*;
导入java.awt.*;
公共类ProyectoFinal扩展JFrame{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
int OPC最终=0;
int n=0;//厄尔尼诺·德尔·阿雷格罗的东南方向,位于诺姆布雷和塞杜拉附近
String[]name=new String[1000];//在ILIMATADAS广场上,没有任何编译问题
String[]cedula=新字符串[1000];//这是一个不存在编译问题的问题
int[]puntos=new int[1000];///SE CREA EL ARREGLO QUE ALMACENARA LOS puntos DE LOS ESTUDIANTES EVALUADOS
int[]rescorrect=new int[1000];//这是一种新的教学方法,可以帮助学生纠正错误
int[]status=new int[1000];//SE GUARDA EL VALOR DE TIPO INTEGER QUE DETERMINARA SI EL TEXTO A MOSTRAR SERA APROBADO/RECHAZADO
您是否需要在展示确认对话框中重复评估USUARIO OPCION DEL USUARIO
名称[n]=JOptionPane.showInputDialog(null,“描述名称:”,“高级信息研究所”,JOptionPane.QUESTION_MESSAGE);//请输入以下名称
cedula[n]=JOptionPane.showInputDialog(null,“Digite su cedula”,“Instituto Superior Informationático Panameño”,JOptionPane.QUESTION_MESSAGE);//请输入cedula
对于(int i=0;i=71){//SE CREA LA conditional PARA determinator SI EL ESTUDIANTE FUE APROBADO/RECHAZADO
状态[i]=1;
}else if(puntos[i]简介
因为这是一个老问题,所以我决定通过完全重新组织/重写OP的代码来解决这个问题
我创建了一个完整的GUI
这是GUI的学生页面。学生在这里输入自己的姓名和学生ID,GUI的大部分文本是西班牙语。当我抄写最后几个问题时,我很惊讶
这是GUI的问答页面。加载到应用程序模型中的问题数是将被询问的问题数。这些问题是按照加载到应用程序模型中的顺序提问的。答案是随机排列的,因此它们以随机顺序出现
学生通过选择右侧的一个单选按钮,然后左键单击“继续”按钮来回答问题
这是中间的问题之一。总共有20个问题
测试完成后,学生返回学生小组。学生的分数返回小组底部
要访问学生成绩的机密表,您必须输入“111111”作为学生ID号
这是学生成绩的秘密表格
解释
当我创建一个复杂的Swing应用程序时,我使用这个模式。这个模式允许我分离关注点,一次只关注应用程序的一个部分
模型
我从创建应用程序模型开始。我创建了一个Student
类、一个Question
类和一个ProjectModel
类。这三个类都是普通的Java getter/setter类
ProjectModel
类是主要的应用程序模型。它包含一个java.util.List
的Student
实例和一个java.util.List
的类的Question
实例。本答案其余部分中对List
的所有引用均指java.util.List
ProjectModel
类的createQuestionsFactory
方法创建所有的测试问题。该方法有127行。通过创建一个Question
类和一个Question
实例列表,我们可以创建一个问题面板并重用它。我可以引用正确的答案,而不是c选择它,但我复制了正确的答案,这样代码就更容易验证和理解。一个真正的系统会有面板,用于输入测试和问题,将测试保存到数据库,以及检索测试
问题
类包括一个字符串
问题、一个列表
字符串
可能的答案、一个字符串
正确答案和一个int
值。我们将这些字段组合在一个类中,因此不必单独跟踪这些值
一个问题可能答案的数量不是固定的,尽管所有问题都应该有相同数量的可能答案。如果问题可能答案的数量不同,则视图代码必须更加复杂
Student
类将与学生相关的字段保存在一起。当我们填充学生分数的JTable
时,该类将变得非常重要
看法
该视图由一个JFrame
组成,主JPanel
。主JPanel
使用CardLayout
从一个JPanel
切换到另一个
三个下属的JPanel
是一个学生JPanel
,一个问题JPanel
,以及一个
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class ProyectoFinal implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ProyectoFinal());
}
private int questionCount;
private DefaultTableModel tableModel;
private JLabel questionTitleLabel;
private JLabel studentMessageLabel;
private JPanel mainPanel;
private JTextField nameField;
private JTextField cedulaField;
private JTextField questionField;
private JTextField[] answerFields;
private ProjectModel model;
private Question question;
private Student student;
public ProyectoFinal() {
this.model = new ProjectModel();
this.questionCount = 0;
}
@Override
public void run() {
JFrame frame = new JFrame("Proyecto Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = createMainPanel();
frame.add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createMainPanel() {
JPanel panel = new JPanel(new CardLayout());
panel.add(createStudentPanel(), "Student");
panel.add(createQuestionPanel(), "Question");
panel.add(createTablePanel(), "Table");
return panel;
}
public void clearStudentPanel(String message) {
nameField.setText("");
cedulaField.setText("");
studentMessageLabel.setText(message);
}
public JPanel createStudentPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 0;
int count = model.getQuestions().size();
String instructions = "<html>Esta es una prueba automatizada. Después "
+ "de enviar su nombre y número de identificación "
+ "<br />de estudiante, se le mostrarán " + count + " preguntas "
+ "de opción múltiple. Sus respuestas se calificarán "
+ "<br />y se les asignará una calificación.";
JLabel instructionsLabel = new JLabel(instructions);
instructionsLabel.setFont(panel.getFont().deriveFont(14f));
instructionsLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(instructionsLabel, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridwidth = 1;
gbc.gridy++;
JLabel nameLabel = new JLabel("Escriba su nombre:");
panel.add(nameLabel, gbc);
gbc.gridx++;
nameField = new JTextField(40);
panel.add(nameField, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel cedulaLabel = new JLabel("<html>Escriba su número de"
+ "<br />identificación de estudiante:");
panel.add(cedulaLabel, gbc);
gbc.gridx++;
cedulaField = new JTextField(40);
panel.add(cedulaField, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy++;
JButton button = new JButton("Enviar");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String cedula = cedulaField.getText().trim();
String name = nameField.getText().trim();
if (cedula.equals("111111")) {
updateTablePanel();
CardLayout layout = (CardLayout) mainPanel.getLayout();
layout.show(mainPanel, "Table");
return;
}
if (cedula.isEmpty() || name.isEmpty()) {
return;
}
ProyectoFinal.this.student = new Student(cedula, name);
model.addStudent(student);
CardLayout layout = (CardLayout) mainPanel.getLayout();
layout.show(mainPanel, "Question");
}
});
panel.add(button, gbc);
gbc.gridy++;
studentMessageLabel = new JLabel(" ");
studentMessageLabel.setFont(panel.getFont().deriveFont(14f));
studentMessageLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(studentMessageLabel, gbc);
return panel;
}
public void updateQuestionPanel() {
this.question = model.getQuestion(questionCount);
questionTitleLabel.setText("Pregunta " + (questionCount + 1));
questionField.setText(question.getQuestion());
List<String> answers = question.getShuffledAnswers();
for (int i = 0; i < answers.size(); i++) {
answerFields[i].setText(answers.get(i));
}
}
public JPanel createQuestionPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 3;
gbc.gridx = 0;
gbc.gridy = 0;
this.question = model.getQuestion(questionCount);
questionTitleLabel = new JLabel("Pregunta 1");
questionTitleLabel.setFont(panel.getFont().deriveFont(24f));
questionTitleLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(questionTitleLabel, gbc);
gbc.gridy++;
JLabel instructionsLabel = new JLabel("Seleccione la respuesta "
+ "más apropiada y haga clic izquierdo en el botón Continuar");
instructionsLabel.setFont(panel.getFont().deriveFont(14f));
instructionsLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(instructionsLabel, gbc);
gbc.gridwidth = 1;
gbc.gridy++;
JLabel questionLabel = new JLabel("Pregunta:");
panel.add(questionLabel, gbc);
gbc.gridwidth = 2;
gbc.gridx++;
questionField = new JTextField(60);
questionField.setEditable(false);
questionField.setText(question.getQuestion());
panel.add(questionField, gbc);
List<String> answers = question.getShuffledAnswers();
gbc.gridwidth = 1;
ButtonGroup buttonGroup = new ButtonGroup();
answerFields = new JTextField[answers.size()];
for (int i = 0; i < answers.size(); i++) {
String answer = answers.get(i);
gbc.gridx = 0;
gbc.gridy++;
JLabel numberLabel = new JLabel((i + 1) + ")");
panel.add(numberLabel, gbc);
gbc.gridx++;
answerFields[i] = new JTextField(60);
answerFields[i].setEditable(false);
answerFields[i].setText(answer);
panel.add(answerFields[i], gbc);
gbc.gridx++;
JRadioButton radioButton = new JRadioButton();
buttonGroup.add(radioButton);
panel.add(radioButton, gbc);
}
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 0;
gbc.gridy++;
JButton button = new JButton("Continuar");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Enumeration<AbstractButton> list = buttonGroup.getElements();
int index = 0;
Iterator<AbstractButton> iterator = list.asIterator();
while (list.hasMoreElements()) {
JRadioButton button = (JRadioButton) iterator.next();
if (button.isSelected()) {
String answer = question.getAnswer(index);
if (question.isAnswerCorrect(answer)) {
student.addPuntos(question.getPuntos());
student.addRescorrect();
}
}
index++;
}
buttonGroup.clearSelection();
questionCount++;
if (questionCount < model.getQuestions().size()) {
updateQuestionPanel();
} else {
questionCount = 0;
String message = "Ha completado su prueba " +
student.getName() + ". Su puntaje de prueba es " +
student.getPuntos() + ".";
clearStudentPanel(message);
ProyectoFinal.this.student = null;
CardLayout layout = (CardLayout) mainPanel.getLayout();
layout.show(mainPanel, "Student");
}
}
});
panel.add(button, gbc);
return panel;
}
public void updateTablePanel() {
for (int i = 0; i < tableModel.getRowCount(); i++) {
tableModel.removeRow(0);
}
for (int i = 0; i < model.getStudents().size(); i++) {
Student student = model.getStudents().get(i);
Object[] object = new Object[tableModel.getColumnCount()];
object[0] = student.getCedula();
object[1] = student.getName();
object[2] = student.getRescorrect();
object[3] = student.getPuntos();
object[4] = student.getStatus();
tableModel.addRow(object);
}
}
public JPanel createTablePanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
String[] header = { "Estudiante No", "Nombre", "Rescorrect",
"Puntos Obtenidos", "Status" };
tableModel = new DefaultTableModel() {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int c) {
if (c < 2) {
return String.class;
} else {
return Integer.class;
}
}
};
for (String string : header) {
tableModel.addColumn(string);
}
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Regresar a las pruebas de los estudiantes");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
questionCount = 0;
clearStudentPanel("");
ProyectoFinal.this.student = null;
CardLayout layout = (CardLayout) mainPanel.getLayout();
layout.show(mainPanel, "Student");
}
});
panel.add(button, BorderLayout.AFTER_LAST_LINE);
return panel;
}
public class ProjectModel {
private List<Student> students;
private List<Question> questions;
public ProjectModel() {
this.students = new ArrayList<>();
this.questions = createQuestionsFactory();
}
private List<Question> createQuestionsFactory() {
List<Question> questions = new ArrayList<>();
String question = "En programacion, una variable es?";
String[] answers = { "Informacion Almacenada", "Un numero",
"Un Contador" };
String correctAnswer = "Informacion Almacenada";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es un hacker?";
answers = new String[] { "Un delincuente informatico",
"Un programa de Facebook",
"Experto en Tecnologia" };
correctAnswer = "Experto en Tecnologia";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es un malware?";
answers = new String[] { "Software Malicioso", "Virus de Celular",
"Un Trino de un Politico en Campaña" };
correctAnswer = "Software Malicioso";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es Python?";
answers = new String[] { "Una Serpiente", "Un Lenguaje de Programacion",
"Un arma" };
correctAnswer = "Un Lenguaje de Programacion";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "La Ram es?";
answers = new String[] { "Capacidad de almacenamiento",
"Chip de ayuda al Procesador",
"Rama de la tecnologia que estudia el hardware" };
correctAnswer = "Chip de ayuda al Procesador";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es minecraft?";
answers = new String[] { "VideoJuego de supervivencia",
"Metodo chino de almacenamiento de datos",
"Bomba atomica" };
correctAnswer = "VideoJuego de supervivencia";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es el machine learning?";
answers = new String[] { "Enseñar a las maquinas a hacer cosas",
"Programar maquinas para que usen algoritmos",
"Todo aquello que podemos aprender de una maquina" };
correctAnswer = "Programar maquinas para que usen algoritmos";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es Realidad Aumentada?";
answers = new String[] { "Tecnologia de inmersion",
"Protocolo de Mejora a la vista humana",
"Vision de un entorno fisico al mundo real, a "
+ "traves de un dispositivo" };
correctAnswer = "Vision de un entorno fisico al mundo "
+ "real, a traves de un dispositivo";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Para que se utilizan los Bit-Coins?";
answers = new String[] { "Apostar en Casinos",
"Realizar Pagos Online de forma segura",
"Comprar entradas de cine" };
correctAnswer = "Realizar Pagos Online de forma segura";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "Que es un String?";
answers = new String[] { "Una variable de tipo cadena", "Una cuerda",
"Un culumpio" };
correctAnswer = "Una variable de tipo cadena";
questions.add(new Question(question, createList(answers), correctAnswer, 6));
question = "El orden de los factores no altera?";
answers = new String[] { "La Formula Cuadratica", "El Producto",
"La Division" };
correctAnswer = "El Producto";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "En la jerarquia de Operadores, cual de los "
+ "siguientes signos tiene prioridad?";
answers = new String[] { "*", "-", "+" };
correctAnswer = "*";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "Cuanto vale Pi?";
answers = new String[] { "3.1315", "3.1514", "3.1415" };
correctAnswer = "3.1415";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "Como se denomina un Triangulo con sus 3 lados iguales?";
answers = new String[] { "Isosceles", "Escaleno", "Equilatero" };
correctAnswer = "Equilatero";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "Como se denomina un Triangulo con sus 3 lados diferentes?";
answers = new String[] { "Isosceles", "Escaleno", "Equilatero" };
correctAnswer = "Escaleno";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "What does the abbreviation DOB stand for?";
answers = new String[] { "Date of Birth", "Date of Bird",
"Donuts Online Business" };
correctAnswer = "Date of Birth";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "What is the plural form for child?";
answers = new String[] { "Childs", "Childes", "Children" };
correctAnswer = "Children";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "The English Language originates from?";
answers = new String[] { "United States", "Great Britain",
"Germany" };
correctAnswer = "Germany";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "What is the singular form of the verb \"to be\"?";
answers = new String[] { "Is", "Are", "Am" };
correctAnswer = "Is";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
question = "What is TOEFL?";
answers = new String[] { "Test of English as a foreign language", "An STD",
"A cookie brand" };
correctAnswer = "Test of English as a foreign language";
questions.add(new Question(question, createList(answers), correctAnswer, 4));
return questions;
}
private List<String> createList(String[] values) {
List<String> output = new ArrayList<>();
for (String s : values) {
output.add(s);
}
return output;
}
public List<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
this.students.add(student);
}
public List<Question> getQuestions() {
return questions;
}
public Question getQuestion(int index) {
return questions.get(index);
}
}
public class Question {
private final int puntos;
private final List<String> answers;
private final String correctAnswer;
private final String question;
public Question(String question, List<String> answers,
String correctAnswer, int puntos) {
this.question = question;
this.answers = answers;
this.correctAnswer = correctAnswer;
this.puntos = puntos;
}
public List<String> getAnswers() {
return answers;
}
public String getAnswer(int index) {
return answers.get(index);
}
public List<String> getShuffledAnswers() {
Collections.shuffle(answers);
return answers;
}
public boolean isAnswerCorrect(String answer) {
return answer.equals(correctAnswer);
}
public String getQuestion() {
return question;
}
public int getPuntos() {
return puntos;
}
}
public class Student {
private int puntos;
private int rescorrect;
private final String name;
private final String cedula;
public Student(String cedula, String name) {
this.cedula = cedula;
this.name = name;
this.puntos = 0;
this.rescorrect = 0;
}
public int getPuntos() {
return puntos;
}
public void addPuntos(int puntos) {
this.puntos += puntos;
}
public int getRescorrect() {
return rescorrect;
}
public void addRescorrect() {
this.rescorrect++;
}
public int getStatus() {
return (puntos >= 71) ? 1 : 0;
}
public String getName() {
return name;
}
public String getCedula() {
return cedula;
}
}
}