在actionlistener事件上交换jpanel

时间:2016-12-06 23:57:53

标签: java user-interface jframe jpanel

我正在编写一个gui程序,基本上会在单击某些按钮时删除并添加jpanel。我试图创建一个jpanels层次结构: 这是层次结构

Opener Mammal Tiger Tiger_caged //terminating button Tiger_riding //terminating button Reptile Frog Frog_eating //terminating button Frog_null //terminating button

初始jpanel是opener,在启动时添加到JFrame。 Opener将有哺乳动物和爬行动物。 点击后,每个按钮都会调用swap(currentJPanel),然后调用removeall()repaint()revalidate(),然后使用x按钮添加下一个JPanel,每个按钮都有调用swap(currentJPanel)方法。这将持续到您使用按钮到达终止JPanel时,单击该按钮时会执行x操作,然后调用reset(),询问用户是想继续游戏还是获得结果。如果选择了继续游戏,则removeall()repaint()revalidate()再次添加opener

我创建了一个名为content的jPanel来放置我的每个JPanel,将opener添加到content,并将content添加到JFrame。在单击某个按钮之前,不应将未打开的按钮添加到JFrame。以下是我目前的代码。当我运行gui时,我创建的所有按钮都是可见的。启动时只能看到Reptile和Mammal按钮。此外,单击其中一个按钮时,不会删除或添加任何内容。当前工作的唯一按钮是getResult按钮,它是唯一一个不调用swap()的按钮。我最初的想法是,我无法将jButton传递给方法,但是当我调用swap()时没有出现错误,所以我不确定。任何帮助和/或建议将不胜感激。



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class DreamInterpreter extends JFrame implements ActionListener{
   private final LayoutManager layoutMain;

   private String result = "";
   private JLabel question = new JLabel("What was in your dream?");
   private String reset = "What was in your dream?";
   private JTextArea showResult;

//BUTTONS
   private JButton exit = new JButton("Exit");
   private JButton getResult= new JButton("INTERPRET MY DREAM");
   private JButton continueGame = new JButton("Add more to my dream");

   private JButton animal = new JButton("Animal");
      private JButton mammal = new JButton("Mammal");
         private JButton tiger= new JButton("Tiger");
         private JButton tiger_caged= new JButton("The tiger is in a cage");
         private JButton tiger_riding= new JButton("The tiger is being ridden");
      private JButton reptile = new JButton("Reptile");
         private JButton frog= new JButton("Frog");
         private JButton frog_eating= new JButton("Eating");
         private JButton frog_null= new JButton("None of these");
   private JButton person = new JButton("Person");
   private JButton location = new JButton("Location");


//PANELS

   //DEFAULT PANEL
   JPanel standard;
   JPanel content;

   JPanel opener;
   JPanel jContinueGame;
   
   JPanel jAnimal, jPerson, jLocation;
      JPanel jMammal, jReptile;
         JPanel jTiger;
         JPanel jFrog;
   

   public DreamInterpreter(){

      super("Dream Interpreter");
      layoutMain = new BorderLayout(); // add components with add(component, BorderLayout.CENTER (NORTH, SOUTH, EAST, WEST)
      setLayout(layoutMain);

      exit.addActionListener(this);
      getResult.addActionListener(this);
      continueGame.addActionListener(this);

      animal.addActionListener(this);
      person.addActionListener(this);
      location.addActionListener(this);

      mammal.addActionListener(this);
      reptile.addActionListener(this);

      frog.addActionListener(this);
      frog_eating.addActionListener(this);
      frog_null.addActionListener(this);

      tiger.addActionListener(this);
      tiger_caged.addActionListener(this);
      tiger_riding.addActionListener(this);

//FORMATTING LAYOUT
      showResult = new JTextArea(15,20);
//DEFAULT
      standard = new JPanel();
      standard.setLayout(new FlowLayout(FlowLayout.CENTER,10,8));
//HEADER
      JPanel header = new JPanel();
      header.add(question);
//OPENER
      opener = standard;
      opener.add(animal);
      opener.add(location);
      opener.add(person);
//CONTENT
      content = new JPanel();
      content.add(opener);
//CONTINUE GAME

      jContinueGame = standard;
      jContinueGame.add(continueGame);
      jContinueGame.add(getResult);

//DECLARE OTHER JPANELS
      
      jAnimal = jPerson = jLocation = standard;
      jMammal = jReptile = standard;
      jFrog = jTiger = standard;
      
      add(header, BorderLayout.NORTH);
      add(content, BorderLayout.CENTER);
      
//ADDING

   //jAnimal
         jAnimal.add(mammal);
         jAnimal.add(reptile);
            jMammal.add(tiger);
               jTiger.add(tiger_caged);
               jTiger.add(tiger_riding);
            jReptile.add(frog);
               jFrog.add(frog_eating);
               jFrog.add(frog_null);
   //jPerson
   //jLocation

   }

   public void actionPerformed(ActionEvent event){

      if(event.getSource() == animal){
         swap(jAnimal);
         question.setText("What kind of animal was in your dream?");
      }
         if(event.getSource() == mammal){
            swap(jMammal);
            question.setText("What kind of mammal was in your dream?");
         }
            if(event.getSource() == tiger){
               swap(jTiger);
               question.setText("What was the tiger doing?");
            }
               if(event.getSource() == tiger_caged){
                  result += "";
                  reset();
               }
               if(event.getSource() == tiger_riding){
                  result+="";
                  reset();
               }
         if(event.getSource() == reptile){
            swap(jReptile);
            question.setText("What kind of reptile was in your dream?");
         }
            if(event.getSource()==frog){
               swap(jFrog);
            }
               if(event.getSource()==frog_eating){
                  result+="";
                  reset();
               }
               if(event.getSource()==frog_null){
                  result+="";
                  reset();
               }

         if(event.getSource() == continueGame){
            swap(opener);
            question.setText("What was in your dream?");
         }
         if(event.getSource() == getResult){
            content.removeAll();
            content.revalidate();
            content.repaint();
            content.add(showResult);
            showResult.setText(""+result);
         }

   }
   public static void main(String[] args){
      DreamInterpreter dreamInterpreter = new DreamInterpreter();
      dreamInterpreter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      dreamInterpreter.setSize(450, 350);
      dreamInterpreter.setVisible(true);
   }

   public void reset(){
      content.removeAll();
      content.revalidate();
      content.repaint();
      question.setText("What would you like to do?");
      content.add(jContinueGame);
   }
   
   public void swap(JPanel panel){
      content.removeAll();
      content.revalidate();
      content.repaint();
      content.add(panel);
   }

}




1 个答案:

答案 0 :(得分:0)

粗略地看,我怀疑你的问题可能就在这里:

  //DECLARE OTHER JPANELS
  jAnimal = jPerson = jLocation = standard;
  jMammal = jReptile = standard;
  jFrog = jTiger = standard;

这里的效果不是创建多个JPanels(),而是让所有变量引用相同的JPanel实例。因此,当您使用不同的变量添加按钮时,实际上是将它们添加到同一个面板中。