Guest User

Untitled

a guest
Nov 19th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package Quiz;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.Statement;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.border.EmptyBorder;
  14. import javax.swing.JButton;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. public class MainWindow extends JFrame {
  18.  
  19. private JPanel contentPane;
  20. private JButton anwser1;
  21. private JButton anwser2;
  22.  
  23. private JPanel panel;
  24.  
  25. private JLabel messageLabel;
  26. private JLabel messageLabel2;
  27. /**
  28. * Launch the application.
  29. */
  30.  
  31.  
  32. /**
  33. * Create the frame.
  34. */
  35. public MainWindow() {
  36. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. setBounds(100, 100, 450, 300);
  38. contentPane = new JPanel();
  39. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  40. contentPane.setLayout(new BorderLayout(0, 0));
  41. setContentPane(contentPane);
  42. JPanel panel = new JPanel();
  43. contentPane.add(panel, BorderLayout.CENTER);
  44. try{
  45. String url = "*************";
  46. String username = "*************";
  47. String password = "*************";
  48. Connection conn = DriverManager.getConnection(url,username,password);
  49. Statement st = conn.createStatement();
  50. st.executeQuery("select * from Quiz");
  51. ResultSet rs = st.getResultSet();
  52. while(rs.next()) {
  53. messageLabel = new JLabel(rs.getString("Question"));
  54. anwser1 = new JButton(rs.getString("Wrong"));
  55. anwser2 = new JButton(rs.getString("Answer"));
  56. anwser1.addActionListener(new ButtonListener());
  57. anwser2.addActionListener(new ButtonListener());
  58. panel = new JPanel();
  59. panel.add(messageLabel);
  60. panel.setBounds(100, 130, 150, 30);
  61. panel.add(anwser1);
  62. panel.add(anwser2);
  63. add(panel);
  64. setVisible(true);
  65. setTitle("Quiz");
  66. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67. }
  68. anwser1.addActionListener(new ButtonListener());
  69. anwser2.addActionListener(new ButtonListener());
  70. panel = new JPanel();
  71. panel.add(messageLabel);
  72. panel.add(anwser1);
  73. panel.add(anwser2);
  74. add(panel);
  75.  
  76. setVisible(true);
  77. }
  78. catch(Exception e){
  79. System.out.println(e);
  80. }
  81. }
  82. private class ButtonListener implements ActionListener
  83. {
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. try{
  87. String url = "*************";
  88. String username = "*********";
  89. String password = "************";
  90. Connection conn = DriverManager.getConnection(url,username,password);
  91. Statement st = conn.createStatement();
  92. st.executeQuery("select * from Quiz");
  93. ResultSet rs = st.getResultSet();
  94.  
  95. while(rs.next()) {
  96. String actionCommand = e.getActionCommand();
  97.  
  98. if (actionCommand.equals(rs.getString("Wrong")))
  99. {
  100. JOptionPane.showMessageDialog(null, "Wrong Answer");
  101.  
  102. }
  103. else if (actionCommand.equals(rs.getString("Answer")))
  104. {
  105. JOptionPane.showMessageDialog(null, "Correct");
  106. }
  107. }
  108. }
  109. catch(Exception a){
  110. System.out.println(a);
  111. }
  112. }
  113. }
  114.  
  115. public static void main(String[] args) {
  116. EventQueue.invokeLater(new Runnable() {
  117. public void run() {
  118. try {
  119. MainWindow frame = new MainWindow();
  120. frame.setVisible(true);
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. });
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment