0
I created a class, Inside it I need to call some methods but it has to be what is typed inside the Jtextpanel, someone knows how to do?
This is my class Pick:
public class Escolha {
public static String hey; //NAO EXISTE NO CÓDIGO É SO PRA DAR SENTINDO PRO PROXIMO CÓDIGO
public static void Ola() {
try {
Robot robot = new Robot();
robot.delay(5000);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_A);
} catch (AWTException e) {
e.printStackTrace();
}
}`
public static void Tchau() {
try {
Robot robot = new Robot();
robot.delay(5000);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_U);
} catch (AWTException e) {
e.printStackTrace();
}
}`
In my program I have a Button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Escolha.ola();
}
It works, but I wanted to put a jtextfield that which method will be executed Ex1:(How it’s working)
Ex2:(What I want him to do) Here he reads Jtextfield and executes the Choice.Hello Why is Written Hello in the box
Ex3:(What I want him to do) Here he reads the Jtextfield and executes the Choice.Bye Why It’s Written Goodbye in the box
A solution would be :
String t1 = jTextField1.getText();
if(t1=="Ola")
{
Escolha.ola();
}
if(t1=="tchau")
{
Escolha.tchau;
}
But did not want to create Several if’s, would not have another way?
peciso chamar um void
- call a void, what is this?– user28595
Dude, I want a variable know there something For example : If I type in Jtextfield -> Hello he will run the public Static void Ola() and if I type : Bye he will run the public Static void Bye()
– Frederico Sampaio
I still do not understand, your explanation is half vague. Try to edit with a [mcve], so that the code can seen working in practice.
– user28595
Okay, I’ll do that
– Frederico Sampaio
@diegofm see if you understand now
– Frederico Sampaio
The solution is already in the question. If you need to check what is typed, there is no escaping from a logical comparison, such as if. Would have to do with switch, but I see no sense in using for only 2 parameters.
– user28595
@diegofm is because this program is just one example, in the original program I will have to create more than 100 Voids like this Choice.ola, and then I will have to do more 100 Comparisons
– Frederico Sampaio
@diegofm , just one more question, if my program has more than 100 comparisons is it possible that it gets slow because of them? Or is it still too little to slow him down ?
– Frederico Sampaio
By "voids" you mean ne no return methods? Because the name would be "method", void is only the type of return, this was confusing your question before you explain here. As for the comparisons, if it is with switch, you will have to make 100 cases, that is, it will be the same work as making 100 ifs. As for performance, impossible to say, only testing in practice with a smaller sample, type 10.
– user28595
I understand, I’m sorry for the stupidity there, I’m very beginner Java , and thanks for Help/Patience
– Frederico Sampaio
Don’t worry, they are valid doubts, after all, we all one day had to learn from the beginning :D
– user28595