Почему переменная не обновляется должным образом в Java?

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.applet.Applet;
import javax.swing.JTextArea;

//This class is where we set up all the questions.
class party{

  int panel_number = 0;
  int party_score = 0;
  JFrame frame = new JFrame();
  String target_word;
  String user_text;
  String label_text;
  JLabel question_frame;
  JPanel pane;
  JPanel panel;

  // public void title_page(){
  //   JOptionPane.showMessageDialog(null, "Welcome to The Party Project!");
  // }

  public void picture_questions(int question_number){
    String photo1;
    String photo2;
    String photo3;
    String photo4;
    String question_header;

      if (question_number == 1){
        question_header = "           Which party appeals to you the most?";
        photo1 = "Rooftop.jpg";
        photo2 = "CollegeParty.jpg";
        photo3 = "Business.jpg";
        photo4 = "PoolParty.jpg";
          image_question(photo1, photo2, photo3, photo4, question_header);
      }

      else if (question_number == 2){
        question_header = "             What is your favorite game?";
        photo1 = "twister.jpg";
        photo2 = "ellen.jpg";
        photo3 = "bp.jpg";
        photo4 = "cornhole.jpg";
        image_question(photo1, photo2, photo3, photo4, question_header);
      }

      else if (question_number == 3){
        question_header = "             Which musician do you want to party to?";
        photo1 = "GratefulDead.jpg";
        photo2 = "justin.jpg";
        photo3 = "Taylor.jpg";
        photo4 = "Beatles.jpg";
        image_question(photo1, photo2, photo3, photo4, question_header);
      }

      else if (question_number == 4){
        question_header = "             Which describes you at a party?";
        photo1 = "passedout.jpg";
        photo2 = "kissing.jpg";
        photo3 = "dancing.jpg";
        photo4 = "selfie.jpg";
        image_question(photo1, photo2, photo3, photo4, question_header);
      }

      else if (question_number == 5){
        question_header = "           Which is your favorite musician?";
        photo1 = "Queen.jpg";
        photo2 = "OneD.jpg";
        photo3 = "Kanye.jpg";
        photo4 = "Chainsmokers.jpg";
        image_question(photo1, photo2, photo3, photo4, question_header);
      }

      else if (question_number == 6){
        question_header = "            Which outfit would you wear to a party?";
        photo1 = "toga.jpg";
        photo2 = "Santa.jpg";
        photo3 = "Business.jpg";
        photo4 = "Onezie.jpg";
        image_question(photo1, photo2, photo3, photo4, question_header);
      }
}


  public void text_questions(int question_number){
    String answer1;
    String answer2;
    String answer3;
    String answer4;
    String question_header;

    if (question_number == 1){
      answer1 = "Drenched";
      answer2 = "<html><body> I like to get a good workout  in when I party but not too sweaty</html>";
      answer3 = "Not huge into the sweating scene";
      answer4 = "Don't make me take off my jacket";
      question_header = "How sweaty do you like to get when you party?";
      multiple_choice_question(answer1, answer2, answer3, answer4, question_header);
    }

    if (question_number == 2){
      answer1 = "<html><body> My computer so I could  code for the rest of my life</html>";
      answer2 = "My crush";
      answer3 = "A good book";
      answer4 = "My favorite drink";
      question_header = "If you were stranded on a desert island, what would you bring?";
      multiple_choice_question(answer1, answer2, answer3, answer4, question_header);
    }

    if (question_number == 3){
      answer1 = "Never eat pizza again";
      answer2 = "Have to refer to professors as 'Sire'";
      answer3 = "Kiss a live frog";
      answer4 = "Have to comment every line of your code";
      question_header = "Would you rather...";
      multiple_choice_question(answer1, answer2, answer3, answer4, question_header);
    }

    if (question_number == 4){
      answer1 = "Water";
      answer2 = "Beer";
      answer3 = "Wine";
      answer4 = "Moonshine";
      question_header = "What is your favorite drink (21+ only)";
      multiple_choice_question(answer1, answer2, answer3, answer4, question_header);
    }

    if (question_number == 5){
      answer1 = "<html><body>The environment, how could you care about anything else when our globe is heating up at an irreprable speed. No more skiing? WHAT ABOUT THE POLAR BEARS!?!?</html>";
      answer2 = "<html><body>Artificial intelligence, Have you seen 2001 a Space Oddesy? Her? Self-Driving Ubers? Computers are taking over and not even Matthew can save us!</html>";
      answer3 = "<html><body>Ending gun violence, No other country has even close to as much gun violence as we do. Every statistic shows that owning a gun makes your chances of dying from gun violence much higher!</html>";
      answer4 = "Dinosaurs";
      question_header = "What are you most passionate about?";
      multiple_choice_question(answer1, answer2, answer3, answer4, question_header);
    }
  }

  public void user_question(){
    String[] target = {"music", "food", "boys", "girls", "live music", "dancing", "drinks", "good", "fun", "people", "not crowded", "band", "college"};
    String question = "In a few words, describe your ideal party.";
    System.out.println("howdy");
    input_question(target, question);
  }

  public void image_question(String question_photo1, String question_photo2, String question_photo3, String question_photo4, String label_text){
    //This panel will display four images that the user will choose from.
    panel = new JPanel();
//    question_frame.setFont(new Font("Calibri", Font.PLAIN, 20));
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    frame.setAlwaysOnTop(true);
    panel.setLayout(new BorderLayout());
    question_frame = new JLabel(label_text);
    panel.add(question_frame, BorderLayout.NORTH);
    button1 = new JButton();
    JPanel inner = new JPanel();
    inner.setLayout(new GridLayout(0, 2));
    button1.setIcon(new ImageIcon(question_photo1)); //Width: 312 Height: 295
    button1.setPreferredSize(new Dimension(100, 100));
    button1.setActionCommand("1");
    inner.add(button1);
    button2 = new JButton();
    button2.setIcon(new ImageIcon(question_photo2));
    button2.setPreferredSize(new Dimension(100, 100));
    button2.setActionCommand("2");
    inner.add(button2);
    button3 = new JButton();
    button3.setIcon(new ImageIcon(question_photo3));
    button3.setPreferredSize(new Dimension(100, 100));
    button3.setActionCommand("3");
    inner.add(button3);
    button4 = new JButton();
    button4.setIcon(new ImageIcon(question_photo4));
    button4.setPreferredSize(new Dimension(100, 100));
    button4.setActionCommand("4");
    inner.add(button4);
    panel.add(inner, BorderLayout.CENTER);
    // ImageIcon icon = new ImageIcon("Alex.jpg");
    // fram`e.getContentPane().add(new JLabel(icon), BorderLayout.EAST);
    panel.setVisible(true);
    frame.add(panel);
    frame.toFront();
    ActionListener al = new click();
    button1.addActionListener(al);
    button2.addActionListener(al);
    button3.addActionListener(al);
    button4.addActionListener(al);
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setBackground(Color.WHITE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void multiple_choice_question(String AnswerA, String AnswerB, String AnswerC, String AnswerD, String label_text){
    //This panel displays a 4 answer multiple choice question that they user will choose from.
    frame = new JFrame();
    pane = new JPanel();
    pane.setLayout(new BorderLayout());
    question_frame = new JLabel(label_text);
    pane.add(question_frame, BorderLayout.NORTH);
    JPanel center = new JPanel();
    GridLayout grid = new GridLayout(0,2);
    center.setLayout(grid);
    pane.add(center, BorderLayout.CENTER);
    ActionListener al = new click();
    addAButton(AnswerA, center, al, "1",1);
    addAButton(AnswerB, center, al, "2",1);
    addAButton(AnswerC, center, al, "3",1);
    addAButton(AnswerD, center, al, "4",1);
    frame.add(pane);
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void input_question(String[] target, String question){
    //This panel will display a question and chance for the user to input their answer.
    frame = new JFrame();
    pane = new JPanel();
    JTextArea user_input = new JTextArea(25, 25);
    pane.setLayout(new BorderLayout());
    pane.add(user_input, BorderLayout.CENTER);
    question_frame = new JLabel(question);
    pane.add(question_frame, BorderLayout.NORTH);
    ActionListener al = new click();
    JButton button = new JButton("I'm Done");
    button.addActionListener(al);
    button.setActionCommand("5");
    pane.add(button, BorderLayout.SOUTH);
    frame.add(pane);
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for (int i = 0; i <target.length; i++){
      //user_input.toLowerCase();
      System.out.println(user_input.getText());
      if (user_input.getText().indexOf(target[i]) >=0){
        System.out.println("ho ho ho");
    }
  }
}

    public void party() {
    panel_number++;
    System.out.println(panel_number);
       if (panel_number == 1){
         picture_questions(1);
       }
       else if(panel_number == 2){
         picture_questions(2);
       }
       else if(panel_number == 3){
         picture_questions(3);
       }
       else if (panel_number == 4){
         picture_questions(4);
       }
       else if(panel_number == 5){
         picture_questions(5);
       }
       else if(panel_number == 6){
         text_questions(1);
       }
       else if(panel_number == 7){
         text_questions(2);
       }
       else if(panel_number == 8){
         text_questions(3);
       }
       else if(panel_number == 9){
         text_questions(4);
       }
       else if(panel_number == 10){
         text_questions(5);
       }
       else if(panel_number == 11){
         text_questions(6);
       }
      else if (panel_number == 12){
         user_question();
      }
  }

  private static void addAButton(String text, Container container, ActionListener al, String actionCommand, int allignment_number) {
       JButton button = new JButton(text);
       button.setAlignmentX(Component.CENTER_ALIGNMENT);
       container.add(button);
       button.addActionListener(al);
       button.setActionCommand(actionCommand);
  }

    public static void main(String args[]) {
        party myGUI = new party();
    myGUI.party();
    }



class ClassParty{
 //This class will read txt documents created by users and suggest a party that everyone would enjoy!
}

class click implements ActionListener{
   public void actionPerformed(ActionEvent event){
     party partier = new party();
      if (event.getActionCommand().equals("1")){
        party_score++;
      }
      else if  (event.getActionCommand().equals("2")){
        party_score++;
      }
      else if  (event.getActionCommand().equals("3")){
        party_score++;
      }
      else if (event.getActionCommand().equals("4")){
        party_score++;
      }
      else if (event.getActionCommand().equals("5")){
        frame.dispose();
      }
      frame.dispose();
      panel.setVisible(false);
      party();
      //System.out.println(party_score);
     /* creates a GUI that presents the user with a question with clickable answers that gives you the next question
    when you finish answering. It plays jeapordy theme music and has a picture Alex Trabeck. It has a status bar
    that tells you how close you are to finishing the program. */
  }
}
}

12-я панель не отображается, она только обновляет переменную до 11. Она должна показывать user_question после 11-й панели text_question. Так же user_question неправильно использует indexOf для определения целевого слова от пользователя.

1 ответ

Решение

У вас есть 6 вопросов с картинками и 5 текстовых вопросов.

Почему нет picture_questions(6);, но text_questions(6);?


Почему ваш ActionListener создать новый экземпляр партии? Это не нужно и сбивает с толку.

class click implements ActionListener{
   public void actionPerformed(ActionEvent event){
     party partier = new party();
     ....
   }
}

Относительно вашего использования indexOf() метод для проверки пользовательского ввода: код будет работать, но вы проверяете пользовательский ввод во время создания фрейма, то есть задолго до того, как у пользователя появилась возможность ввести свой текст.

Цикл для проверки того, что пользователь ввел, принадлежит слушателю действия.

Другие вопросы по тегам