Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package quizzes;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- public class Main extends JFrame implements ActionListener {
- String[][] perguntas = new String[3][6];
- JRadioButton[] jrPerguntas = new JRadioButton[4];
- JLabel jlPergunta = new JLabel("");
- JLabel jlPontuacao = new JLabel("Pontuacao: 0");
- int posAtual = 0;
- int pontos = 0;
- JButton jbResponder = new JButton("Responder");
- ButtonGroup bgOp = new ButtonGroup();
- public Main() {
- super("Quizz");
- setLayout(new GridLayout(7, 1));
- carregaPerguntas();
- for (int id = 0; id < 4; id++) {
- jrPerguntas[id] = new JRadioButton();
- bgOp.add(jrPerguntas[id]);
- }
- montaTela();
- jlPontuacao.setHorizontalAlignment(JTextField.RIGHT);
- add(jlPergunta);
- add(jrPerguntas[0]);
- add(jrPerguntas[1]);
- add(jrPerguntas[2]);
- add(jrPerguntas[3]);
- add(jlPontuacao);
- add(jbResponder);
- jbResponder.addActionListener(this);
- pack();
- setLocationRelativeTo(null);
- setVisible(true);
- }
- public void carregaPerguntas() {
- Connection conn = null;
- PreparedStatement stmt = null;
- ResultSet rs = null;
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost/quizzes";
- String user = "root";
- String pass = "";
- String sql1 = "SELECT COUNT(*) as qtd FROM Perguntas";
- String sql2 = "SELECT * FROM perguntas as p, respostas as r WHERE p.COD_Pergunta = r.COD_Pergunta";
- Class.forName(driver);
- conn = DriverManager.getConnection(url, user, pass);
- stmt = conn.prepareStatement(sql1);
- rs = stmt.executeQuery();
- rs.next();
- int qtdP = rs.getInt("qtd");
- stmt = conn.prepareStatement(sql2);
- rs = stmt.executeQuery();
- String pergunta = rs.getString("perguntas");
- System.out.println(pergunta);
- String resposta = rs.getString("respostas");
- System.out.println(resposta);
- int codPergunta = rs.getInt("COD_Pergunta");
- int codResposta = rs.getInt("COD_Resposta");
- rs.next();
- for (int i = 1; i < qtdP; i++) {
- perguntas[i][0] = rs.getString("perguntas");
- for (int j = 1; j < 4; j++) {
- if (rs.getString("CertoErrado") == "e") {
- perguntas[i][j] = rs.getString("respostas");
- } else {
- perguntas[i][4] = String.valueOf(j);
- }
- rs.next();
- }
- }
- }
- catch (ClassNotFoundException ex) {
- System.out.println("Driver não encontrado!");
- }
- catch (SQLException ex) {
- System.out.println("Erro de banco: " + ex.getMessage());
- }
- finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException ex) {
- }
- }
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException ex) {
- }
- }
- }
- }
- public void montaTela() {
- jlPergunta.setText(perguntas[posAtual][0]);
- for (int id = 0; id < 4; id++) {
- jrPerguntas[id].setText(perguntas[posAtual][id + 1]);
- }
- }
- public static void main(String[] args) {
- new Main();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- int respostaCerta = Integer.parseInt(perguntas[posAtual][5]);
- if (jrPerguntas[respostaCerta - 1].isSelected()) {
- pontos++;
- } else {
- pontos--;
- }
- jlPontuacao.setText("Pontuacao: " + pontos);
- if (posAtual < 2)
- posAtual++;
- else
- JOptionPane.showMessageDialog(this, "Game Over");
- montaTela();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement