Java Class and Variable Problem

Asked

Viewed 56 times

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)

Aqui ele Executa normal o Escolha.ola

Ex2:(What I want him to do) Here he reads Jtextfield and executes the Choice.Hello Why is Written Hello in the box

Obs:Se tiver escrito Algo além de Ola e Tchau ele não pode executar

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

inserir a descrição da imagem aqui

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?

  • 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()

  • 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.

  • Okay, I’ll do that

  • @diegofm see if you understand now

  • 1

    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.

  • @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

  • @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 ?

  • 1

    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.

  • I understand, I’m sorry for the stupidity there, I’m very beginner Java , and thanks for Help/Patience

  • Don’t worry, they are valid doubts, after all, we all one day had to learn from the beginning :D

Show 6 more comments

1 answer

0

Hi. Yes, there is.

I created a class to show an example:

package minhaaplicacao;

import javax.swing.Joptionpane;

public class Minhaclasse { public void nameMensage(String msg){ Joptionpane.showMessageDialog(null, msg); } }

And this is the method that calls the "calling method" in the class "Minhaclasse ":

 public void chamarEsteMetodo(String classe, String metodo){
        try {
            Class minhaClasse = Class.forName(classe);
            Object objDaMinhaClasse = minhaClasse.newInstance();
            Method meuMetodo = objDaMinhaClasse.getClass().getMethod(metodo, String.class);
            meuMetodo.invoke(objDaMinhaClasse,"Minha mensagem");
        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException ex) {
            System.err.println(ex);
        }
    }
With this, you can put as many parameters as you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.