簡體   English   中英

為什么我的第一個面板運行兩次?

[英]Why is my first panel running twice?

我正在完成我的最后一個項目,我們正在做一個測驗,以找出人們喜歡什么樣的聚會……但是我無法讓我按順序在正確的面板上進行活動!

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

我使用調試並逐步執行每個命令來修復它。 調試效果很好,您應該嘗試一下。 調試是關鍵,因為即使GUI看起來像第一個GUI,它也表明實際上達到了multi_choice_question()。 它縮小了漏洞的來源。

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?";
     ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM