如何使用JButton在一个主JPanel上的多个JPanel之间切换?

时间:2016-05-27 17:02:44

标签: java swing jpanel jbutton actionlistener

这是我的主要面板,我试图通过单击名为“next”的JButton在多个其他JPanel之间进行更改,但是当我点击JButton时没有任何反应。 HomePanel一直保持原位。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCAPanel extends JPanel
{
  private HomePanel homeP;
  private ChoicePanel choiceP;
  private JButton next, quit;
  int x;
  public JCAPanel()
{
  x = 0;
  setLayout(new BorderLayout());

  homeP = new HomePanel();

  JPanel subpanel = new JPanel(new BorderLayout());
  next = new JButton("Next");
  next.addActionListener(new ListenerNext());
  quit = new JButton("Quit");
  quit.addActionListener(new ListenerQuit());
  subpanel.add(next, BorderLayout.EAST);
  subpanel.add(quit, BorderLayout.WEST);

  add(subpanel, BorderLayout.SOUTH);
  add(homeP, BorderLayout.CENTER);
}
public class ListenerNext implements ActionListener
{
   public void actionPerformed(ActionEvent e)
   {
      x++;
      if(x == 1)
      {
         choiceP = new ChoicePanel();
         add(choiceP, BorderLayout.CENTER);
      }
   }
}

0 个答案:

没有答案