gpt4 book ai didi

java - 将带有 ActionListener 的 JButton 添加到 JTabbedPane : ActionListener not working

转载 作者:行者123 更新时间:2023-12-02 12:58:54 26 4
gpt4 key购买 nike

这里有新海报;如果我违反了任何规则/搞砸了任何事情,请告诉我,我会删除该帖子。

我有一个 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);
}
}

最佳答案

您所指的 returnButton 没有附加任何 actionListener。所以只需添加它即可。

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

关于java - 将带有 ActionListener 的 JButton 添加到 JTabbedPane : ActionListener not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44348394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com