2017-06-03 52 views
0

新的海报在这里;如果我打破了任何规则/搞砸了任何事情,只要告诉我,我会把这个帖子记下来。将ActionButton添加到JTabbedPane中:ActionListener不起作用

我有一个MainMenu JFrame,里面有一个JPanel(mainMenu)。 mainMenu有一个按钮,可将显示更改为JTabbedPane指令。这部分工作正常。

当我试图向说明添加按钮和新选项卡时,问题就出现了。尽管新的按钮和选项卡已添加到Instructions类,但该按钮实际上并没有执行任何操作,尽管该按钮有一个ActionListener。

下面是Instructions类(扩展JTabbedPane)和MainMenu(扩展JFrame)以供参考。

在此先感谢。

正下方:MainMenu的类 `

/** 
* @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(); 
    } 
} 

` 下面:说明类

/** 
* @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

您的意思是哪个按钮? returnButton没有监听器 – petul

+0

是的,我很笨。 Idk我忘了添加听众:/谢谢! – LuxTheOmnicaster

+0

没问题,一直发生:) – petul

回答

1

你指的是returnButton已经没有ActionListener的连接。所以只需添加它。

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