簡體   English   中英

將帶有ActionListener的JButton添加到JTabbedPane:ActionListener不起作用

[英]Adding JButton with ActionListener to a JTabbedPane: ActionListener not working

這里有新海報; 如果我違反任何規則/增加任何內容,請告訴我,我將刪除該職位。

我有一個帶有JPanel(mainMenu)的MainMenu JFrame。 mainMenu有一個按鈕,可將顯示更改為JTabbedPane指令。 這部分工作正常。

當我嘗試向說明中添加按鈕和新標簽時,就會出現問題。 盡管新的按鈕和選項卡已添加到“指令”類,但該按鈕實際上具有任何功能,盡管該按鈕具有ActionListener。

下面是指令類(擴展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);
}
}

您所指的returnButton沒有附加的actionListener。 因此,只需添加它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM