Is there a way to implement a KeyListener and MouseListener in the same applet? I already tried any ways I thought that would work and I tried Google. :\
my try:
C:\Users\Dan\Documents\DanJavaGen\tileGen.java:23: tileGen is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class tileGen extends JApplet implements KeyListener, MouseListener {
^
1 error
You can certainly implement both KeyListener and MouseListener in the same applet :)
You must have the following methods in the tileGen class:
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e)b{
}
public void mouseExited(MouseEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
If you already have some of these methods implemented you can leave them out.
Gentle hint: Capitalize your class name as TileGen :) Lower-case class names are — by convention — used only for generated or internal code.
Related
I'm having serious issues with adding a keylistener to my Java program.
I would like to avoid the addKeyListener() method, so I tried the following solutions:
public class game implements KeyListener{
#Override
public void keyTyped(KeyEvent e) {
return;
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println("test");
return;
}
#Override
public void keyReleased(KeyEvent e) {
return;
}
}
And:
public class game{
KeyListener listener = new KeyListener(){
#Override
public void keyTyped(KeyEvent e) {
return;
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println("test");
return;
}
#Override
public void keyReleased(KeyEvent e) {
return;
}
}
}
None of them worked for me.
Do I really have to use the addKeyListener() in a graphics class? I'm trying to avoid that.
Thanks in advance.
Is there a particular reason you want to avoid using addKeyListener()?
It's an essential part of KeyListener so you won't be able to avoid using it.
Should be relatively easy to implement though. If you're using JFrame, the following code within your constructor should work:
public class Game implements KeyListener{
public Game(){
JFrame jframe = new JFrame();
jframe.addKeyListener(this);
}
//implement other KeyListener methods
}
This way, the Game class itself acts as the listener and can implement all of the necessary methods.
Hope this helps!
In my java project, I want to check the input in each JTextField in a few different classes (with the exact same code)..
Right now I have the same code copied over and over and I was suggested with 2 options:
Create a method and call the method instead.
Create a new class that extends from another class (I don't know which yet) that has the method needed.
The code I'm using now is:
totalAmount.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent arg0) {
//do something
}
});
And the new class is:
public class Listener extends KeyAdapter {
public void keyTyped(KeyEvent arg0){
//do something
}
}
The problem is that I don't know if I'm extending the right class, and how to use the new class I've written...
Thanks in advance!
To do what you are wanting with your key adapter you would use
totalAmount.addKeyListener(new Listener());
and your code of your key adapter is correct.
public class Listener extends KeyAdapter {
public void keyTyped(KeyEvent arg0){
//do something
}
}
To get the text from a JTextField you could either use this code inside your keyAdapter
System.out.println(totalAmount);
or, preferably you could use a document listener. This would be done by
public class documentListener implements DocumentListener //This is a listener
{
public void changedUpdate(DocumentEvent e){
}
public void removeUpdate(DocumentEvent e){
int lengthMe = e.getDocument().getLength();
System.out.println(e.getDocument().getText(0,lengthMe));
}
public void insertUpdate(DocumentEvent e){
int lengthMe = e.getDocument().getLength();
System.out.println(e.getDocument().getText(0,lengthMe));
}
}
and it would be added to the JTextField with
totalAmount.getDocument().addDocumentListener(new documentListener());
Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?
closeFile.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
exit.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).
Use a MouseAdapter and override the methods that you want.
closeFile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
closeFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//your code
}
});
Note: You dont have to write 'implements MouseListener' during class definition.
For more information, search for adapter classes, more specifically for MouseAdapter class.
Hi I'm trying to program a game using java. This is my first time using java, I am used to C#. In C# I would call Mouse.getLocation() and create a rect using the mouses location. Then by using if(Mouse.Left().toString() == "Pressed") I would then check if the mouse rect intersected with any other objects and act accordingly.
I've noticed in java you aren't provided with methods like these. So I was wondering, is the best way to approach mouse input simply to add listeners on all my clickable objects? I understand listeners and have a good idea how to use them but I was just wanting to check if there are more efficient ways to handle input or ways geared more towards what I'm most conformable with.
let your frame implement the MouseListener interface
implement all abstract methods, but in your case it is probably the mouseClicked event
identify if the button clicked is a left click, using the SwingUtilities class
if it is a left click, then set the x and y, which is the location of your click relative to the frame, not the screen.
public class MouseListeningObject extends JFrame implements MouseListener {
int x, y;
public MouseListeningObject () {
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
x = e.getX();
y = e.getY();
}
}
#Override
public void mousePressed(MouseEvent e) {
// Some codes here
}
#Override
public void mouseReleased(MouseEvent e) {
// Some codes here
}
#Override
public void mouseEntered(MouseEvent e) {
// Some codes here
}
#Override
public void mouseExited(MouseEvent e) {
// Some codes here
}
}
You want your frame to implement MouseListener then add it in the constructor.
class MyFrame extends JFrame implements MouseListener {
MyFrame() {
addMouseListener(this);
}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
}
My goal was to simplify this code (all works fine):
package test;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JWindow;
public class A extends JWindow implements MouseListener, MouseMotionListener {
public A() {
addMouseListener(A.this);
addMouseMotionListener(A.this);
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseMoved(MouseEvent e) {
}
}
But if I decide to use MouseAdapter like this:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
transferFocusBackward();
//This method is being used, working fine
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, not working anymore
}
});
mouseDragged receiving no events. But if I add MouseMotionListener like this:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
});
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
});
Then everything backs to normal. So my question is - what is the purpose of mouseDragged, mouseMoved and mouseWheelMoved methods in MouseAdapter class if they do not work?
OK, I think I understand now: in MouseAdapter class documentation it is said that "Create a listener object using the extended class and then register it with a component using the component's addMouseListener, addMouseMotionListener, addMouseWheelListener methods". So now it looks like:
private class MouseListeners extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
#Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
}
and then:
MouseListeners listeners = new MouseListeners();
addMouseListener(listeners);
addMouseMotionListener(listeners);
Now everything is OK.