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();
}
}