|
-
Mouse Listening/ Get point/ Better class structure
Hi, I'm Nescafe. This is my first time to the forums. I'm a sophomore in college and intending to go into CPSC in the near future. Nonetheless, my breadth of knowledge concerning Java is still limited thought it is the language I have had the most experience with.
Below is some code I recently wrote. I am intending to write a simple GUI that will report when the mouse enters my JPanel component, exits the component and while within the component reports the points of the mouse pointer. I have been successful with the first two scenarios but can't get the mouse point of the mouse pointer. I extended the MouseAdapter class and I thought that the mouseMoved() method would be called when the mouse is within my component. However, this does not seem to be the case.
Furthermore, on a side not, you will see that I commented out an anonymouse class I had with in my constructor. Is this a good idea? Is the anonymouse class a bad idea w/in the constructor as oppsed to creating the inner class that I have: MouseMotionListener. In other words, do you guys have any reccomendations on how to structure to code in a better way, i.e. better structure, etc? Sorry about the long post but thanks for the help!!
HTML Code:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class MouseMotion{
private JPanel mousePanel;
private JFrame frame;
private JLabel mouseLabel;
public MouseMotion(){
frame = new JFrame("Moust Motion Listener");
frame.setLayout(new BorderLayout());
frame.setSize(300,300);
frame.setLocation(200,250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mouseLabel = new JLabel("Listening for mouse movement......");
//add border to mouseLabel
//mouseLabel.setBorder(BorderFactory.createTitledBorder("Is Mouse Moving?"));
mousePanel = new JPanel();
mousePanel.setBackground(Color.gray);
//setPreferredSize-- not working correctly!!
mousePanel.setSize(new Dimension(
frame.getWidth(), frame.getHeight() * (1/3)));
//add mouse listener to panel
mousePanel.addMouseListener(new MouseMoveListener());
/*
mousePanel.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent evt){
//System.out.println("mouse entered component");
mouseLabel.setText("Mouse entered component");
}
public void mouseExited(MouseEvent evt){
mouseLabel.setText("<html>Mouse has <font color = red>left </font>component</html>");
}
});
*/
frame.getContentPane().add(mousePanel, BorderLayout.CENTER);
frame.getContentPane().add(mouseLabel, BorderLayout.SOUTH);
//add border to JLabel later
frame.setVisible(true);
}
//MouseAdapter class
private class MouseMoveListener extends MouseAdapter{
public void mouseEntered(MouseEvent evt){
mouseLabel.setText("<html>Mouse has <font color = blue>entered</font> component</html>");
}
public void mouseExited(MouseEvent evt){
mouseLabel.setText("<html>Mouse has <font color = red>exited</font> component</html>");
}
public void mouseMoved(MouseEvent evt){
//System.out.println("mouseMoved event");
}
}
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
new MouseMotion();
}
}
Similar Threads
-
By Ad van Klink in forum .NET
Replies: 1
Last Post: 09-01-2002, 08:52 AM
-
By Wai-Yin Chee in forum .NET
Replies: 8
Last Post: 09-27-2001, 05:04 PM
-
By Patrick Ireland in forum .NET
Replies: 5
Last Post: 05-10-2001, 06:19 PM
-
By Patrick Ireland in forum .NET
Replies: 3
Last Post: 05-07-2001, 03:04 PM
-
By Patrick Ireland in forum .NET
Replies: 0
Last Post: 04-26-2001, 10:01 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks