Author |
Message |
xHoly-Divinity
|
Posted: Fri Apr 06, 2007 6:07 pm Post subject: Mouse Listener |
|
|
Can I use mouseListener in an applet (runnable), or do i have to use a JFrame/JApplet? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ericfourfour
|
Posted: Fri Apr 06, 2007 11:50 pm Post subject: RE:Mouse Listener |
|
|
Mouse listeners work with all components I believe. |
|
|
|
|
|
xHoly-Divinity
|
Posted: Sat Apr 07, 2007 11:35 am Post subject: Re: Mouse Listener |
|
|
Hmm.... is it possible to use a mouseListener in parallel with a runnable? Cuz i need both keyboard and mouse input? |
|
|
|
|
|
Ultrahex
|
Posted: Sat Apr 07, 2007 1:22 pm Post subject: Re: Mouse Listener |
|
|
xHoly-Divinity,
hey school chump,
yes you can use any component and use a mouselistener, keylistener, mousemotionlistener etc....
and yes you can run them in parallel using two threads, however you do not need this for the program you are doing.
just implement both into the same component / add them to the same component, and you will be fine, both will run at same time.
If you don't trust me.....
Java: |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DoubleListen
{
public static void main (String args []) {
MyFrame a = new MyFrame ();
}
}
class MyFrame extends JFrame implements KeyListener, MouseListener
{
public MyFrame () {
JLabel hello = new JLabel("Hello World");
this. getContentPane(). add(hello );
this. addKeyListener(this);
this. addMouseListener(this);
pack ();
show ();
}
public void keyTyped (KeyEvent e ) {
System. out. println("KEY TYPED: " + e. getKeyChar());
}
public void keyPressed (KeyEvent e ) {
System. out. println("KEY PRESSED: " + e. getKeyChar());
}
public void keyReleased (KeyEvent e ) {
System. out. println("KEY RELEASED: " + e. getKeyChar());
}
public void mousePressed (MouseEvent e ) {
System. out. println("Mouse pressed; # of clicks: " + e. getClickCount());
}
public void mouseReleased (MouseEvent e ) {
System. out. println("Mouse released; # of clicks: " + e. getClickCount());
}
public void mouseEntered (MouseEvent e ) {
System. out. println("Mouse entered");
}
public void mouseExited (MouseEvent e ) {
System. out. println("Mouse exited");
}
public void mouseClicked (MouseEvent e ) {
System. out. println("Mouse clicked (# of clicks: " + e. getClickCount() + ")");
}
} |
|
|
|
|
|
|
wtd
|
Posted: Sat Apr 07, 2007 1:38 pm Post subject: RE:Mouse Listener |
|
|
Learn about interfaces, and learn to read the java.sun.com API docs and you won't need to ask this question. |
|
|
|
|
|
xHoly-Divinity
|
Posted: Tue Apr 10, 2007 5:09 pm Post subject: Re: Mouse Listener |
|
|
Thanks ultrahex . Hehe, i have it wrkin, i just don't wana display it in class just in case Jone gets mad and makes me do more work, lol.
Do u think I can do the whole thing in applet and not use JFrames? |
|
|
|
|
|
ericfourfour
|
Posted: Tue Apr 10, 2007 9:10 pm Post subject: RE:Mouse Listener |
|
|
You are not required to use swing when working with applets. |
|
|
|
|
|
wtd
|
Posted: Wed Apr 11, 2007 12:03 am Post subject: RE:Mouse Listener |
|
|
When Swing is available, why use vanilla AWT? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
xHoly-Divinity
|
Posted: Wed Apr 11, 2007 5:35 pm Post subject: Re: RE:Mouse Listener |
|
|
wtd @ Wed Apr 11, 2007 1:03 am wrote: When Swing is available, why use vanilla AWT?
considering it... maybe towards the end of the project I can 'convert' it to swing bcuz right now I want to get the functionality and everything working and so if there's time in the end (since i've very little experience with swing =/) |
|
|
|
|
|
xHoly-Divinity
|
Posted: Wed Apr 11, 2007 6:07 pm Post subject: Re: Mouse Listener |
|
|
Is it possible to get actionListener and keyListener working together? I tried but when I add the actionListner, the keyListener doesn't work |
|
|
|
|
|
HellblazerX
|
Posted: Thu Apr 12, 2007 1:00 pm Post subject: RE:Mouse Listener |
|
|
The two should work together. When you say that the keyListener doesn't work, what do you mean by it? |
|
|
|
|
|
xHoly-Divinity
|
Posted: Thu Apr 12, 2007 9:04 pm Post subject: Re: Mouse Listener |
|
|
ahhhh figured it out... when u make the button e.g. add (myButton), u have to add it to the ActionListener AND the KeyListener. I forgot to add to KeyListener so that's why it didn't work |
|
|
|
|
|
HellblazerX
|
Posted: Fri Apr 13, 2007 7:23 am Post subject: Re: Mouse Listener |
|
|
Lol! Now it seems that my keyListener isn't working. I implemented the interface, set up my methods, and added the keyListener. Did I forget anything, cuz I don't think I did. |
|
|
|
|
|
|