2016-12-17 49 views
0

我的工作我的最后一个项目,我们正在做一个测验,看看什么样的政党的人喜欢......但我不能让我的按顺序运行,并与该右图!为什么我的第一板运行两次?

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工作。

回答

0

我使用调试,并通过每个命令加紧固定。调试很好,你应该尝试一下。调试是关键,因为它表明multiple_choice_question()实际上是得到达到,即使GUI看起来像第一个GUI。它缩小了错误的来源。

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

THANK YOU SO MUCH –

相关问题