Can someone please explain this code for me?
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyChar() == 'a' ){
String myS = myField.getText();
myLabel.setText(myS);
myLabel2.setText(“”);
}
}
Can someone explain to me what this code actually does
I'm trying to figure out what it does.
Thanks
Grot
Re: Can someone please explain this code for me?
Where did you get the code?
When the method is called it makes a test of the contents of its arg and if the test is true it calls some methods.
Read the API doc for the KeyEvent class and its method that is used to see what they do.
I don't know what that is. It looks like definitions for a code pre-processing program.
Re: Can someone please explain this code for me?
I got this code from a book but it didn't explain it, I edited it there so you can see it better
Thanks for the help Norm! you're a legend!
Re: Can someone please explain this code for me?
Now what I called pre-processor code has changed to slanting "s that aren't available on my keyboard.
An empty String from my keyboard would look like: "" vs the “” in the posted code. I guess the pre-processor I mentioned is the one that formats the forum's replies.
Re: Can someone please explain this code for me?
I think “” is supposed to be "".
From when I see in this code is that
if a key 'a' is pressed something happens
and it creates a space.
Am I right?
Re: Can someone please explain this code for me?
Quote:
if a key 'a' is pressed
That is assuming there is a connection between key presses and the method. The code does not show there is any connection.
Quote:
it creates a space
It calls a method (setText) with an arg of an empty String: "" (no space between the ""s). Read the API doc for the method to see what it does.
Re: Can someone please explain this code for me?
Quote:
Originally Posted by
Grot
I think “” is supposed to be "".
From when I see in this code is that
if a key 'a' is pressed something happens
and it creates a space.
Am I right?
Yes, it's common to cut and paste from an e-book and it ends up as “”.
The code looks like it comes from a GUI, where it's looking for a key press (the event) and then it changes the labels on the GUI from a text field.
Which book is it from?
Re: Can someone please explain this code for me?
What does the String myS = myField.getText(); do?
Re: Can someone please explain this code for me?
It defines a String variable, calls a method and assigns what is returned by the method to the variable.
Read the API doc for the method to see what is in the String that is returned.
Re: Can someone please explain this code for me?