2017-06-03 7 views
0

Nouvelle affiche ici; si je suis en train de casser des règles ou de tout enfoncer, dites-le moi et je prendrai le poste.Ajout de JButton avec ActionListener à un JTabbedPane: ActionListener ne fonctionne pas

J'ai un JFrame MainMenu avec un JPanel (mainMenu) à l'intérieur. mainMenu a un bouton qui change l'affichage pour les instructions JTabbedPane. Cette partie fonctionne bien.

Le problème survient lorsque j'essaie d'ajouter un bouton et un nouvel onglet aux instructions. Bien que les nouveaux boutons et l'onglet soient ajoutés à la classe Instructions, le bouton ne fait rien en dépit d'un ActionListener pour ce bouton. Ci-dessous sont la classe Instructions (extends JTabbedPane) et MainMenu (extends JFrame) pour référence.

Merci d'avance.

directement ci-dessous: MainMenu classe `

/** 
* @Author Small Snake Studios (Nathan Lee) 
* @Date Feb 28, 2017 
* @Version 1.0 
* @Description This program is a JPanel for the Blockfinder Express game. Mostg of the work done this week has been in graphics rather than in coding this week. 
*/ 

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.FlowLayout; 
import java.awt.event.ActionListener; 
/** 
* This is a JPanel that represents the main menu of the program. Each of the buttons open up another JPanel in the main JFrame, 
* each JPanel representing a different screen. This JFrame is the main menu and you can open the other JPanels through this one. 
* Because it's a JFrame, it can hold and control the rest of the windows (splash screen, main menu, credits, the main game, instructions, 
* high scores and exit. 
**/ 

public class MainMenu extends JFrame implements ActionListener 
{ 

    /** 
    * This is a serialVersionUID. It is generated to avoid errors/warnings. 
    */ 
    private static final long serialVersionUID = -3088890058631223710L; 
    /*Name   use                   type 
    * splashScreen creates a splash screen that shows an animation       SplashScreen 
    * mainGame  a JPanel representing the main game          MainGame 
    * instructions a JPanel representing the instructions screen       Instructions 
    * quit   a JPanel representing the quit screen         Quit 
    * credits  a JPanel representing the credits screen        Credits 
    * mainMenu  a JPanel representing the main menu (built into the MainMenu class)  JPanel*/ 
    SplashScreen splashScreen = new SplashScreen(); 
    JTabbedPane instructions = new Instructions(); 
    JPanel credits = new Credits(); 
    JPanel highScores = new HighScores(); 
    JPanel mainMenu = new JPanel(); 
    JMenuBar myMenus = new JMenuBar(); 
    JButton play = new JButton ("Play!"); 
    JButton howToPlay = new JButton ("Learn!"); 
    JButton leaderboards = new JButton ("Leaderboards!"); 
    JButton creds = new JButton ("Credits!"); 
    JMenu quitMenu = new JMenu ("Quit"); 
    JMenuItem quitMenuItem = new JMenuItem ("Quit"); 

    public MainMenu() //constructor 
    { 
     super ("Blockfinder Express"); //constructor adds all of these buttons to the JPanel 
     init(); 
    } 

    public void actionPerformed (ActionEvent ae) 
    { 
     Frame f = null; 
     if (ae.getActionCommand().equals ("Play!")) 
     { if (f == null) 
     { 
      remove (mainMenu); 
      f = new Frame(); 
      repaint(); 
     } 
     } 
     if (ae.getActionCommand().equals("Quit")) 
     { 
      dispose(); 
      f.dispose(); 
     } 
     if (ae.getActionCommand().equals ("Return to Menu")) 
     { 
      System.out.println("returning to menu"); 
      remove (instructions); 
      add (mainMenu); 
      repaint(); 
      revalidate(); 
     } 
     if (ae.getActionCommand().equals ("Learn!")) 
     { 
      add (instructions); 
      JPanel exit = new JPanel(); 
      JButton returnButton = new JButton ("Return to Menu"); 
      exit.add(returnButton); 
      instructions.add("Exit", exit); 
      remove (mainMenu); 
      repaint(); 
      System.out.println ("Functional"); 
     } 
    } 
    public void init() 
    { 
     play.addActionListener(this); 
     howToPlay.addActionListener(this); 
     leaderboards.addActionListener (this); 
     creds.addActionListener (this); 
     mainMenu.add (play); 
     mainMenu.add (howToPlay); 
     mainMenu.add(leaderboards); 
     mainMenu.add(creds); 
     myMenus.add(quitMenu); 
     quitMenu.add(quitMenuItem); 
     quitMenuItem.addActionListener(this); 
     setJMenuBar (myMenus); 
     setSize (1000,900); 
     setResizable (false); 
     setVisible (true); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     add (splashScreen); 
     splashScreen.animate(); 
     remove (splashScreen); 
     add (mainMenu); 
     repaint(); 
     revalidate(); 
    } 

    public static void main (String [] args) 
    { 
     MainMenu mm = new MainMenu(); 
    } 
} 

` Ci-dessous: Instructions classe

/** 
* @author Nathan 
* @version 1.0 
* @studio Small Snake Studios 
* @purpose: This is a JPanel representing the instructions screen. It functions by using a picture 
* the instructions screen as the way to display it on the JPanel. The driver class/main menu 
* adds an exit button to the JPanel so I can implement an ActionListener that closes this 
* tab from there. Probably works better that way, too. Uses a CardLayout to go from one 
* instruction page to another, allowing for easy readability and preventing me from having 
* to cram a lot of info in one screen. The pages of the CardLayout are done via nested classes 
* so I can implement paint and graphics in each of them. 
* 
* Things to do: 
* - Add actual pictures for the three instructions screen 
* - Run this code with everything else to ensure it works 
*/ 

import java.awt.*; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 

import javax.swing.*; 
import java.io.*; 
import javax.imageio.ImageIO; 

public class Instructions extends JTabbedPane{ 
     class FirstScreen extends JPanel 
    { 
     /** 
      * 
      */ 
     private static final long serialVersionUID = -2674891692847531892L; 
     BufferedImage instructions; 
     public FirstScreen() 
     { 
      importInfo(); 
      repaint(); 
     } 
     public void importInfo(){ 
     try 
     { 
      instructions = ImageIO.read (new File ("Instructions1.png")); 
     } 
     catch (IOException e) 
     { 
     } 
     } 
     public void paint (Graphics g) 
     { 
      g.drawImage (instructions, 0, 0, null); 
     } 
    } 
     class SecondScreen extends JPanel 
    { 
     private static final long serialVersionUID = -2840353661877108337L; 
     BufferedImage instructions; 
     public SecondScreen() 
     { 
      importInfo(); 
      repaint(); 
     } 
     public void importInfo() 
     { 
      try 
      { 
      instructions = ImageIO.read (new File ("Instructions2.png")); 
      } 
      catch (IOException e) 
      { 
      } 
     } 
     public void paint (Graphics g) 
     { 
      g.drawImage (instructions, 0, 0, null); 
     } 
    } 
     class ThirdScreen extends JPanel 
     { 
     /** 
      * 
      */ 
     private static final long serialVersionUID = -394248748993045880L; 
     BufferedImage instructions; 
     public ThirdScreen() 
     { 
      importInfo(); 
      repaint(); 
     } 
     public void importInfo() 
     { 
      try 
      { 
       instructions = ImageIO.read (new File ("Instructions3.png")); 
      } 
      catch (IOException e) 
      { 
      } 
     }  
     public void paint (Graphics g) 
     { 
      g.drawImage (instructions, 0, 0, null); 
     } 
     } 
public Instructions() 
{ 
    super(); 
    FirstScreen card1 = new FirstScreen(); 
    SecondScreen card2 = new SecondScreen(); 
    ThirdScreen card3 = new ThirdScreen(); 
    add ("Page 1", card1); 
    add ("Page 2", card2); 
    add ("Page 3", card3); 
} 
} 
+0

De quel bouton parlez-vous? Le returnButton n'a pas d'écouteur attaché – petul

+0

Ouais, je suis bête. Idk comment j'ai oublié d'ajouter l'auditeur:/merci! – LuxTheOmnicaster

+0

Pas de problème, arrive tout le temps :) – petul

Répondre

1

Le returnButton vous faites référence n'a pas actionListener ci-joint. Alors, ajoutez-le.

JButton returnButton = new JButton ("Return to Menu"); 
returnButton.addActionListener(this);