Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. public class Main {
  2.  
  3.  
  4. /**
  5. * @param args the command line arguments
  6. */
  7. public static void main(String[] args) throws Exception {
  8. // TODO code application logic here
  9. List <Questions> questions = new ArrayList<>();
  10. List <String> answers = new ArrayList<>();
  11. String question = "";
  12. Questions quest = new Questions(question, answers);
  13. question = quest.getQuestion();
  14. System.out.println(question);
  15.  
  16. UserInterface ui = new UserInterface(questions);
  17.  
  18. SwingUtilities.invokeLater(() -> {
  19. try {
  20. UserInterface.createAndShowGui();
  21. } catch (FileNotFoundException ex) {
  22. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24. });
  25.  
  26. }
  27.  
  28. }
  29. public class Questions {
  30.  
  31. private String question;
  32. private List<String> answers;
  33. private File file;
  34. private List<String> questionsFromFile;
  35. private ListIterator<String> itr;
  36.  
  37. public Questions(String question, List<String> answers) throws FileNotFoundException{
  38.  
  39. this.file = new File("src/house.txt");
  40. this.question = question;
  41. this.answers = answers;
  42. this.questionsFromFile = new CopyOnWriteArrayList<>();
  43. this.itr = this.questionsFromFile.listIterator();
  44.  
  45. try (Scanner reader = new Scanner(this.file)) {
  46. while (reader.hasNextLine()){
  47. String line = reader.nextLine();
  48. if (line.startsWith("QUESTION: ")){
  49. this.question = line.substring(10);
  50. this.questionsFromFile.add(this.question);
  51. // System.out.println(this.question);
  52. } else if (line.startsWith("ANSWER: ")){
  53. String answer = line.substring(8);
  54. this.answers.add(answer);
  55. // System.out.println(this.answers);
  56. }
  57. }
  58. // System.out.println(this.question);
  59. // only prints the last question 8 times in a row
  60. }
  61. }
  62. public String getQuestion(){
  63. while (this.itr.hasNext()){
  64. String obj = (String)this.itr.next();
  65. return obj;
  66. }
  67. return "";
  68. }
  69. public List<String> getAnswers() {
  70. return answers;
  71. }
  72. @Override
  73. public String toString() {
  74. return "Question " + question;
  75. }
  76. }
  77. public class UserInterface extends JPanel {
  78. public static final Object QUESTION = "question";
  79. // fill label with blank text to expand it
  80. private JLabel resultLabel = new JLabel(String.format("%150s", " "));
  81.  
  82. // CardLayout to allow swapping of question panels
  83. private CardLayout cardLayout = new CardLayout();
  84. private JPanel centerPanel = new JPanel(cardLayout);
  85. private String questio;
  86. private List<String> answers = new ArrayList<>();
  87. private Questions quest;
  88.  
  89. public UserInterface(List<Questions> questions) throws FileNotFoundException {
  90. this.quest = new Questions(questio, answers);
  91. centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
  92. for (Questions question : questions) {
  93. centerPanel.add(createQPanel(question), question.getQuestion());
  94.  
  95.  
  96. JPanel bottomPanel = new JPanel(new BorderLayout());
  97.  
  98. // add button that allows swapping question panels
  99.  
  100. bottomPanel.add(new JButton(new AbstractAction("Next") {
  101.  
  102. @Override
  103. public void actionPerformed(ActionEvent e) {
  104. cardLayout.next(centerPanel);
  105. }
  106. }),
  107.  
  108. BorderLayout.LINE_START);
  109. add(bottomPanel, BorderLayout.LINE_START);
  110. bottomPanel.add(resultLabel);
  111.  
  112. setLayout(new BorderLayout());
  113. add(bottomPanel, BorderLayout.PAGE_END);
  114. add(centerPanel);
  115. }
  116. }
  117. public JPanel bottomPanel(){
  118. JPanel bottomPanel = new JPanel(new BorderLayout());
  119.  
  120. return bottomPanel;
  121. }
  122. private JPanel createQPanel(Questions question) {
  123. JPanel radioPanel = new JPanel(new GridLayout(0, 1));
  124. radioPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
  125. ButtonGroup buttonGroup = new ButtonGroup();
  126. ItemListener myItemListener = new MyItemListener(this);
  127. for (String answer : question.getAnswers()) {
  128. JRadioButton answerButton = new JRadioButton(answer);
  129.  
  130. // this is present in case you want to extract the Question
  131. // object from the JRadioButton, useful for if you want to
  132. // test if the selected answer is correct
  133. answerButton.putClientProperty(QUESTION, question);
  134.  
  135. // add listener to the JRadioButton
  136. answerButton.addItemListener(myItemListener);
  137.  
  138. // add to button group so only one can be selected
  139. buttonGroup.add(answerButton);
  140.  
  141. // add to JPanel for display
  142. radioPanel.add(answerButton);
  143. }
  144.  
  145. JPanel qPanel = new JPanel(new BorderLayout());
  146. qPanel.add(new JLabel(question.getQuestion()), BorderLayout.PAGE_START);
  147. qPanel.add(radioPanel);
  148. return qPanel;
  149. }
  150.  
  151. // public method that the item listener will use to display selection
  152. public void displayResult(String selectedText) {
  153. resultLabel.setText(selectedText);
  154. }
  155. public void displayFinalResults(){
  156.  
  157. }
  158. public static void createAndShowGui() throws FileNotFoundException {
  159.  
  160. List<Questions> questions = new ArrayList<>();
  161. for (int i = 1; i < 7; i++) {
  162.  
  163. String question = "Question " + i;
  164. List<String> answers = new ArrayList<>();
  165.  
  166. questions.add(new Questions(question, answers));
  167. }
  168.  
  169. UserInterface mainPanel = new UserInterface(questions);
  170.  
  171. JFrame frame = new JFrame("User Interface");
  172. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  173. frame.getContentPane().add(mainPanel);
  174. frame.pack();
  175. frame.setLocationRelativeTo(null);
  176. frame.setVisible(true);
  177. }
  178. }
  179. public class MyItemListener implements java.awt.event.ItemListener {
  180.  
  181. private UserInterface ui;
  182.  
  183. public MyItemListener(UserInterface ui) {
  184. this.ui = ui;
  185. }
  186.  
  187. @Override
  188. public void itemStateChanged(ItemEvent e) {
  189. JRadioButton source = (JRadioButton) e.getSource();
  190.  
  191. String selected = "The JRadioButton " + source.getText();
  192. selected += e.getStateChange() == ItemEvent.SELECTED ? " has been selected"
  193. : " has been unselected";
  194. // so e.getStateChange() checks the integer value of the selected button
  195. // to get the actual Question object get the client property:
  196. Questions question = (Questions) source.getClientProperty(UserInterface.QUESTION);
  197. // now can get answer Strings and check if correct one selected
  198. System.out.println(question);
  199. // this s the thing that deals with the correct answers. i don't think you need it.
  200. // String correctAnswer = question.getAnswers().get(question.getCorrectIndex());;
  201. // if (source.getText().equals(correctAnswer)) {
  202. // selected += " and is correct";
  203. // } else {
  204. // selected += " and is incorrect";
  205. // }
  206.  
  207. // tell the GUI to display the result
  208. ui.displayResult(selected);
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement