Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception {
- // TODO code application logic here
- List <Questions> questions = new ArrayList<>();
- List <String> answers = new ArrayList<>();
- String question = "";
- Questions quest = new Questions(question, answers);
- question = quest.getQuestion();
- System.out.println(question);
- UserInterface ui = new UserInterface(questions);
- SwingUtilities.invokeLater(() -> {
- try {
- UserInterface.createAndShowGui();
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- });
- }
- }
- public class Questions {
- private String question;
- private List<String> answers;
- private File file;
- private List<String> questionsFromFile;
- private ListIterator<String> itr;
- public Questions(String question, List<String> answers) throws FileNotFoundException{
- this.file = new File("src/house.txt");
- this.question = question;
- this.answers = answers;
- this.questionsFromFile = new CopyOnWriteArrayList<>();
- this.itr = this.questionsFromFile.listIterator();
- try (Scanner reader = new Scanner(this.file)) {
- while (reader.hasNextLine()){
- String line = reader.nextLine();
- if (line.startsWith("QUESTION: ")){
- this.question = line.substring(10);
- this.questionsFromFile.add(this.question);
- // System.out.println(this.question);
- } else if (line.startsWith("ANSWER: ")){
- String answer = line.substring(8);
- this.answers.add(answer);
- // System.out.println(this.answers);
- }
- }
- // System.out.println(this.question);
- // only prints the last question 8 times in a row
- }
- }
- public String getQuestion(){
- while (this.itr.hasNext()){
- String obj = (String)this.itr.next();
- return obj;
- }
- return "";
- }
- public List<String> getAnswers() {
- return answers;
- }
- @Override
- public String toString() {
- return "Question " + question;
- }
- }
- public class UserInterface extends JPanel {
- public static final Object QUESTION = "question";
- // fill label with blank text to expand it
- private JLabel resultLabel = new JLabel(String.format("%150s", " "));
- // CardLayout to allow swapping of question panels
- private CardLayout cardLayout = new CardLayout();
- private JPanel centerPanel = new JPanel(cardLayout);
- private String questio;
- private List<String> answers = new ArrayList<>();
- private Questions quest;
- public UserInterface(List<Questions> questions) throws FileNotFoundException {
- this.quest = new Questions(questio, answers);
- centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
- for (Questions question : questions) {
- centerPanel.add(createQPanel(question), question.getQuestion());
- JPanel bottomPanel = new JPanel(new BorderLayout());
- // add button that allows swapping question panels
- bottomPanel.add(new JButton(new AbstractAction("Next") {
- @Override
- public void actionPerformed(ActionEvent e) {
- cardLayout.next(centerPanel);
- }
- }),
- BorderLayout.LINE_START);
- add(bottomPanel, BorderLayout.LINE_START);
- bottomPanel.add(resultLabel);
- setLayout(new BorderLayout());
- add(bottomPanel, BorderLayout.PAGE_END);
- add(centerPanel);
- }
- }
- public JPanel bottomPanel(){
- JPanel bottomPanel = new JPanel(new BorderLayout());
- return bottomPanel;
- }
- private JPanel createQPanel(Questions question) {
- JPanel radioPanel = new JPanel(new GridLayout(0, 1));
- radioPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
- ButtonGroup buttonGroup = new ButtonGroup();
- ItemListener myItemListener = new MyItemListener(this);
- for (String answer : question.getAnswers()) {
- JRadioButton answerButton = new JRadioButton(answer);
- // this is present in case you want to extract the Question
- // object from the JRadioButton, useful for if you want to
- // test if the selected answer is correct
- answerButton.putClientProperty(QUESTION, question);
- // add listener to the JRadioButton
- answerButton.addItemListener(myItemListener);
- // add to button group so only one can be selected
- buttonGroup.add(answerButton);
- // add to JPanel for display
- radioPanel.add(answerButton);
- }
- JPanel qPanel = new JPanel(new BorderLayout());
- qPanel.add(new JLabel(question.getQuestion()), BorderLayout.PAGE_START);
- qPanel.add(radioPanel);
- return qPanel;
- }
- // public method that the item listener will use to display selection
- public void displayResult(String selectedText) {
- resultLabel.setText(selectedText);
- }
- public void displayFinalResults(){
- }
- public static void createAndShowGui() throws FileNotFoundException {
- List<Questions> questions = new ArrayList<>();
- for (int i = 1; i < 7; i++) {
- String question = "Question " + i;
- List<String> answers = new ArrayList<>();
- questions.add(new Questions(question, answers));
- }
- UserInterface mainPanel = new UserInterface(questions);
- JFrame frame = new JFrame("User Interface");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(mainPanel);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
- public class MyItemListener implements java.awt.event.ItemListener {
- private UserInterface ui;
- public MyItemListener(UserInterface ui) {
- this.ui = ui;
- }
- @Override
- public void itemStateChanged(ItemEvent e) {
- JRadioButton source = (JRadioButton) e.getSource();
- String selected = "The JRadioButton " + source.getText();
- selected += e.getStateChange() == ItemEvent.SELECTED ? " has been selected"
- : " has been unselected";
- // so e.getStateChange() checks the integer value of the selected button
- // to get the actual Question object get the client property:
- Questions question = (Questions) source.getClientProperty(UserInterface.QUESTION);
- // now can get answer Strings and check if correct one selected
- System.out.println(question);
- // this s the thing that deals with the correct answers. i don't think you need it.
- // String correctAnswer = question.getAnswers().get(question.getCorrectIndex());;
- // if (source.getText().equals(correctAnswer)) {
- // selected += " and is correct";
- // } else {
- // selected += " and is incorrect";
- // }
- // tell the GUI to display the result
- ui.displayResult(selected);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement