Guest User

Untitled

a guest
Oct 20th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.*;
  6.  
  7. class NewClass4 extends JFrame {
  8.  
  9. JTextArea t0, t1, t2, t3, t4;
  10. JRadioButton r1, r2, r3, r4;
  11. JLabel l, l5, l1, l2, l3, l4, l6, l7;
  12. JButton b1, b2;
  13. JScrollPane p;
  14. JPanel jp;
  15. String s = "ERROR!!";
  16. static int i = 0;
  17.  
  18. public NewClass4() {
  19. Container cp;
  20.  
  21. JFrame f = new JFrame("Teacher Interface");
  22. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. cp = f.getContentPane();
  24. JPanel jp = new JPanel();
  25. i++;
  26. try {
  27. // create a mysql database connection
  28. String myDriver = "com.mysql.jdbc.Driver";
  29. String myUrl = "jdbc:mysql://localhost:3307/quiz";
  30. Class.forName(myDriver);
  31. Connection conn = DriverManager.getConnection(myUrl, "root", "");
  32. String query = "insert into questions (sno, question, o1, o2, o3, o4, copt, subj)"
  33. + " values (?, ?, ?, ?, ?, ?, ?, ?)";
  34.  
  35. PreparedStatement preparedStmt = conn.prepareStatement(query);
  36.  
  37. String s = "Subject";
  38.  
  39. f.setSize(610, 320);
  40. l6 = new JLabel(s);
  41. l6.setBounds(40, 10, 140, 40);
  42. l5 = new JLabel("Question " + i);
  43. l5.setBounds(100, 30, 140, 40);
  44. t0 = new JTextArea(8, 30);
  45. preparedStmt.setString(2, "Question Text");
  46. p = new JScrollPane(t0);
  47. p.setHorizontalScrollBarPolicy(
  48. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  49. p.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  50. p.setBounds(20, 60, 555, 50);
  51.  
  52. t1 = new JTextArea();
  53. // preparedStmt.setString(3,t1.getText());
  54. t1.setBounds(45, 143, 110, 22);
  55.  
  56. t2 = new JTextArea();
  57. preparedStmt.setString(4, t2.getText());
  58. t2.setBounds(180, 143, 110, 22);
  59.  
  60. t3 = new JTextArea(2, 10);
  61. preparedStmt.setString(5, t3.getText());
  62. t3.setBounds(315, 143, 110, 22);
  63.  
  64. t4 = new JTextArea(2, 10);
  65. t4.setBounds(450, 143, 110, 22);
  66. preparedStmt.setString(6, t4.getText());
  67.  
  68. l = new JLabel("Correct Option :");
  69. l.setBounds(150, 190, 100, 15);
  70. l1 = new JLabel("A");
  71. l1.setBounds(30, 140, 110, 25);
  72.  
  73. l2 = new JLabel("B");
  74. l2.setBounds(165, 140, 110, 25);
  75. l3 = new JLabel("C");
  76. l3.setBounds(300, 140, 110, 25);
  77. l4 = new JLabel("D");
  78. l4.setBounds(435, 140, 110, 25);
  79. l7 = new JLabel(s);
  80. r1 = new JRadioButton("A");
  81. r1.setBounds(255, 190, 40, 15);
  82. r1.setBackground(Color.WHITE);
  83. r2 = new JRadioButton("B");
  84. r2.setBounds(295, 190, 40, 15);
  85. r2.setBackground(Color.WHITE);
  86. r3 = new JRadioButton("C");
  87. r3.setBounds(335, 190, 40, 15);
  88. r3.setBackground(Color.WHITE);
  89. r4 = new JRadioButton("D");
  90. r4.setBounds(375, 190, 40, 15);
  91. r4.setBackground(Color.WHITE);
  92.  
  93. // f.add(js);
  94.  
  95. jp.add(l6);
  96. jp.add(p);
  97. jp.add(l5);
  98.  
  99. jp.add(l1);
  100. jp.add(t1);
  101.  
  102. jp.add(l2);
  103. jp.add(t2);
  104. jp.add(l3);
  105. jp.add(t3);
  106. jp.add(l4);
  107. jp.add(l7);
  108.  
  109. jp.add(t4);
  110. jp.add(l);
  111. jp.add(r1);
  112. jp.add(r2);
  113. jp.add(r3);
  114. jp.add(r4);
  115.  
  116. ButtonGroup bg = new ButtonGroup();
  117. bg.add(r1);
  118. bg.add(r2);
  119. bg.add(r3);
  120. bg.add(r4);
  121.  
  122. b1 = new JButton("Next");
  123. b1.setBounds(120, 230, 100, 30);
  124. b1.setBackground(Color.WHITE);
  125. jp.add(b1);
  126. b1.addActionListener(new ActionListener() {
  127.  
  128. public void actionPerformed(ActionEvent e) {
  129. // String n=t1.getText();
  130. // String n1=t2.getText();
  131. // String n2=t3.getText();
  132. // String n3=t4.getText();
  133. // if(e.getSource()==b1){
  134. // if(n.isEmpty()==true&&n1.isEmpty()==true&&n2.isEmpty()==true&&n3.isEmpty()==true){
  135. // l7.setBounds(120,250,110,25);
  136. // jp.add(l7);
  137. //
  138. // }
  139.  
  140. String f2 = t1.getText();
  141. String f1 = t2.getText();
  142. String f3 = t3.getText();
  143. String f4 = t4.getText();
  144. try {
  145.  
  146. preparedStmt.setInt(1, i);
  147.  
  148. preparedStmt.setString(2, t0.getText());
  149.  
  150. preparedStmt.setString(3, f2);
  151.  
  152. // t2 = new JTextArea();
  153. preparedStmt.setString(4, f1);
  154.  
  155. // t3 = new JTextArea(2, 10);
  156. preparedStmt.setString(5, f3);
  157.  
  158. // t4 = new JTextArea(2, 10);
  159. preparedStmt.setString(6, f4);
  160.  
  161. if (r1.isSelected())
  162. preparedStmt.setInt(7, 1);
  163. else if (r2.isSelected())
  164. preparedStmt.setInt(7, 2);
  165. else if (r3.isSelected())
  166. preparedStmt.setInt(7, 3);
  167. else
  168. preparedStmt.setInt(7, 0);
  169.  
  170. preparedStmt.setString(8, "NULL");
  171.  
  172. preparedStmt.execute();
  173. t2.setText(f1);
  174. t3.setText(f3);
  175. t4.setText(f4);
  176. } catch (Exception e1) {
  177. System.err.println("Got an exception!");
  178. System.err.println(e1.getMessage());
  179. }
  180.  
  181. cp.setVisible(false);
  182. NewClass4 obj = new NewClass4();
  183. dispose();
  184.  
  185. }
  186. });
  187. b2 = new JButton("Finish");
  188. b2.setBounds(350, 230, 100, 30);
  189. b2.setBackground(Color.WHITE);
  190. jp.add(b2);
  191. b2.addActionListener(new ActionListener() {
  192.  
  193. public void actionPerformed(ActionEvent e) {
  194. f.setVisible(false);
  195. NewClass12 obj = new NewClass12();
  196. dispose();
  197.  
  198. }
  199.  
  200. });
  201.  
  202. jp.setLayout(null);
  203. jp.setBackground(Color.BLUE);
  204. f.setVisible(true);
  205. cp.add(jp);
  206. // cp.setSize(610, 320);
  207. } catch (Exception e) {
  208. System.err.println("Got an exception!");
  209. System.err.println(e.getMessage());
  210. }
  211. }
  212. }
  213.  
  214. import javax.swing.JFrame;
  215.  
  216. class runningTeacher { //Driver for Class4
  217.  
  218. public static void main(String[] args) {
  219. NewClass4 obj = new NewClass4();
  220. obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  221. }
  222. }
Add Comment
Please, Sign In to add comment