为什么我的第一个面板运行两次?

时间:2016-12-17 23:56:42

标签: 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.applet.Applet;


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

  int num_questions = 10;
  int panel_number = 0;
  int party_score = 0;
  JFrame frame = new JFrame();
  String AnswerA;
  String AnswerB;
  String AnswerC;
  String AnswerD;
  JButton button1;
  JButton button2;
  JButton button3;
  JButton button4;
  String target_word;
  String user_text;
  String label_text;
  JLabel question_frame;
  JPanel pane;
  JPanel panel;


  public void image_question(){
    //This panel will display four images that the user will choose from.
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    label_text = "Which party appeals to you the most?";
    question_frame = new JLabel(label_text);
    question_frame.setFont(new Font("Calibri", Font.PLAIN, 20));
    panel.add(question_frame, BorderLayout.NORTH);
    button1 = new JButton();
    JPanel inner = new JPanel();
    inner.setLayout(new GridLayout(0, 2));
    button1.setIcon(new ImageIcon("ball.jpg")); //img1
    button1.setPreferredSize(new Dimension(100, 100));
    button1.setActionCommand("1");
    inner.add(button1);
    button2 = new JButton();
    button2.setIcon(new ImageIcon("christmas_party.jpg"));
    button2.setPreferredSize(new Dimension(100, 100));
    button2.setActionCommand("2");
    inner.add(button2);
    button3 = new JButton();
    button3.setIcon(new ImageIcon("college_party.jpg"));
    button3.setPreferredSize(new Dimension(100, 100));
    button3.setActionCommand("3");
    inner.add(button3);
    button4 = new JButton();
    button4.setIcon(new ImageIcon("kid_party.jpg"));
    button4.setPreferredSize(new Dimension(100, 100));
    button4.setActionCommand("4");
    inner.add(button4);
    panel.add(inner, BorderLayout.CENTER);
    //ImageIcon icon = new ImageIcon("Alex.jpg");
    //frame.getContentPane().add(new JLabel(icon), BorderLayout.EAST);
    //frame.setVisible(true);
    frame.add(panel);

    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(){
    //This panel displays a 4 answer multiple choice question that they user will choose from.
    pane = new JPanel();
    pane.setLayout(new BorderLayout());
    label_text = "What music would you prefer at a party?";
    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);
    AnswerA = "1";
    AnswerB = "2";
    AnswerC = "3";
    AnswerD = "4";
    ActionListener al = new click();
    addAButton(AnswerA, center, al, "1");
    addAButton(AnswerB, center, al, "2");
    addAButton(AnswerC, center, al, "3");
    addAButton(AnswerD, center, al, "4");
    frame.add(pane);
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void input_question(){
    //This panel will display a question and chance for the user to input their answer.
    user_text = JOptionPane.showInputDialog(null, "Describe your ideal party");
    target_word = "music";
    if (user_text.indexOf(target_word) >=0){
      System.out.println("ho ho ho");
    }
  }

    public void party() {
    panel_number++;
      if (panel_number == 1){
        image_question();
      }

      if(panel_number == 2){
        multiple_choice_question();
      }

      if(panel_number == 3){
        input_question();
      }
    /*  else if (panel_number == 4){
        image_question();
      }

      else if(panel_number == 5){
        multiple_choice_question();
      }

      else if(panel_number == 6){
        input_question();
      }*/

  }

  private static void addAButton(String text, Container container, ActionListener al, String actionCommand) {
       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++;
      }
      System.out.println(panel_number);
      frame.dispose();
      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. */

  }
}
}

当我运行我的代码时,它会运行image_question两次,然后运行input_question并退出。我无法让multiple_choice_question工作。

1 个答案:

答案 0 :(得分:0)

我使用debug修改它并逐步执行每个命令。调试效果很好,你应该尝试一下。调试很关键,因为它显示实际上已达到multiple_choice_question(),即使GUI看起来像第一个GUI。它缩小了bug的来源。

public void multiple_choice_question(){
    frame=new JFrame();    <----add this and everything seems to be working
    pane = new JPanel();
    pane.setLayout(new BorderLayout());
    label_text = "What music would you prefer at a party?";
     ...