这里有新的海报;如果我违反任何规则/搞砸任何事情,请告诉我,我会把这篇文章记下来。
我有一个带有JPanel(mainMenu)的MainMenu JFrame。 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 :(得分:1)
您引用的returnButton没有附加actionListener。所以只需添加它。
JButton returnButton = new JButton ("Return to Menu");
returnButton.addActionListener(this);