Java 将带有ActionListener的JButton添加到JTabbedPane:ActionListener不工作
新海报在这里;如果我违反了任何规则/把事情搞砸了,只要告诉我,我就把帖子拿下来 我有一个主菜单JFrame,里面有一个JPanel(主菜单)。主菜单有一个按钮,用于将显示更改为JTabbedPane指令。这部分工作正常 当我试图在说明中添加按钮和新选项卡时,问题就出现了。尽管新的按钮和选项卡被添加到Instructions类中,但尽管该按钮有一个ActionListener,但该按钮实际上并没有做任何事情 下面是说明类(扩展JTabbedPane)和主菜单(扩展JFrame)以供参考 提前谢谢 正下方:MainMenu类 ` ` 以下:课堂说明Java 将带有ActionListener的JButton添加到JTabbedPane:ActionListener不工作,java,swing,actionlistener,jtabbedpane,Java,Swing,Actionlistener,Jtabbedpane,新海报在这里;如果我违反了任何规则/把事情搞砸了,只要告诉我,我就把帖子拿下来 我有一个主菜单JFrame,里面有一个JPanel(主菜单)。主菜单有一个按钮,用于将显示更改为JTabbedPane指令。这部分工作正常 当我试图在说明中添加按钮和新选项卡时,问题就出现了。尽管新的按钮和选项卡被添加到Instructions类中,但尽管该按钮有一个ActionListener,但该按钮实际上并没有做任何事情 下面是说明类(扩展JTabbedPane)和主菜单(扩展JFrame)以供参考 提前谢谢
/**
* @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);
你指的是哪个按钮?returnButton没有监听装置啊,我很笨。Idk我怎么忘了添加侦听器:/谢谢!没问题,经常发生:)
JButton returnButton = new JButton ("Return to Menu");
returnButton.addActionListener(this);