Sign in to follow this  
Smooferoo

[java] Can't get MouseListeners to work in an extended JPanel

Recommended Posts

I created a GamePanel Class, extending the JPanel class. I can't figure out two things: 1. I tried to get the GamePanel to be set as the content pane, but it keeps giving me an error. I ended up just having to add it as a separate pane, not set it as the content pane. 2. This is giving me even more trouble. I can't get the GamePanel to register any mouse input whatsoever. First I had the GamePanel class implement the MouseListener and MouseMotionListener interfaces, but that didn't work. Next, I created a GamePanelMouseListener class to create listener objects that each GamePanel would use to get mouse input. That isn't working either. Do you guys have any tips about why this isn't working? Or any tips about design? Anything you can offer is greatly appreciated. Here's my code:
/**
 * GamePanel class
 *
 * @author  
 * @version 1.00 06/09/25
 */
 
 import javax.swing.*;
 import java.util.*;
 import java.lang.*;
 import java.awt.*;
 import java.awt.event.*;
 
 
public class GamePanel extends JPanel
{
    
    public JLabel label = new JLabel ("Stupid Frame");
    GamePanelMouseListener listener;
   
    
    public GamePanel()
    {
    	super();
    	setBackground(Color.blue);
    	this.setLayout(new BorderLayout());
    	setSize(100,100);
    	add(label, BorderLayout.CENTER);
 	    GamePanelMouseListener listener = new GamePanelMouseListener();
    }
    
    public GamePanel(BorderLayout b)
    {
    	super(b);
    	setBackground(Color.green);
    	setSize(100,100);
    	add(label, BorderLayout.SOUTH);
    	GamePanelMouseListener listener = new GamePanelMouseListener();
    }
}

class GamePanelMouseListener
{
	
	public GamePanelMouseListener()
	{
		
	}
    
    public void mouseClicked (MouseEvent e) 
    {
		System.out.println("mouse was clicked");
    }
    
    public void mouseEntered (MouseEvent e) 
    {
    	
    }
    
    public void mouseExited (MouseEvent e) 
    {
    	
    }
    
    public void mousePressed (MouseEvent e) 
    {
    	
    }
    
    public void mouseReleased (MouseEvent e) 
    {
    	
    }
    
    public void mouseDragged (MouseEvent e) 
    {
    	
    }
    
    public void mouseMoved (MouseEvent e) 
    { 
    
    }
    
}

/**
 * AWT Sample application
 *
 * @author 
 * @version 1.00 06/09/25
 */

 import javax.swing.*;
 import java.util.*;
 import java.lang.*;
 import java.awt.*;
 import java.awt.event.*;

public class YayFun 
{
    
    public static void main(String[] args) 
    {
        
        GamePanel contentPane = new GamePanel(new BorderLayout());
        contentPane.setOpaque(true);
        
        
        // Create application frame.
        YayFunFrame frame = new YayFunFrame();
        frame.add(contentPane);
        
        // Show frame
        frame.setVisible(true);
        
    }
}

import java.awt.*;
import java.awt.event.*;

/**
 * Sample Frame.
 *
 * @author 
 * @version 1.00 06/09/25
 */
public class YayFunFrame extends Frame {
    
    /**
     * The constructor.
     */  
     public YayFunFrame() {
                
        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu();
        MenuItem menuFileExit = new MenuItem();
        
        menuFile.setLabel("File");
        menuFileExit.setLabel("Exit");
        
        // Add action listener.for the menu button
        menuFileExit.addActionListener
        (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    YayFunFrame.this.windowClosed();
                }
            }
        ); 
        menuFile.add(menuFileExit);
        menuBar.add(menuFile);
        
        setTitle("YayFun");
        setMenuBar(menuBar);
        setSize(new Dimension(400, 400));
        
        // Add window listener.
        this.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    YayFunFrame.this.windowClosed();
                }
            }
        );  
    }
    
    
    /**
     * Shutdown procedure when run as an application.
     */
    protected void windowClosed() {
    	
    	// TODO: Check if it is safe to close the application
    	
        // Exit application.
        System.exit(0);
    }
}

Share this post


Link to post
Share on other sites
Your GamePanel Class isn't adding the MouseListener and MouseMotionListener. You need somthing like this

addMouseListener("whatever class implements MouseListener")

and/or

addMouseMotionListener("whatever class implements MouseMotionListener")

What is the error you are getting when you try to implement these interfaces?

Share this post


Link to post
Share on other sites
I wasn't getting an error before. It was acting as if there was no mouse input at all. Now I added the addMouseListener method, and it isn't working. Now I have an error saying that the addMouseListener method can't be applied to the GamePanelMouseListener class. (which is the class implementing the interfaces). ARG!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this