简体   繁体   中英

JAVA - call a part of the main method from another class

At the moment I got two classes. public class Start and public class Frame_Main.

I am controlling public class Frame_Main through a GUI, where I type something in and press the button afterwards which activates the ActionListener. Afterwards the input becomes a output in the console.

After the input through the GUI I want to call the Start class and check if the input is the same as some user answers that I prepared, but I cant find a way to call the function to achive that, Eclipse always shows me error when I try to create a public void or class with the function in it.

Start:

public class Start {
public static void main(String[] args)
{
 //deleted some variables to make the code look cleaner
 auswahl = frame_main.auswahl;
 //I want to call this while loop 
    while(run){
        System.out.println("Welche Zahl ist vorhanden? \nDual, Hexa, Oktal oder Dezimal?");

        if((auswahl).equals("Dual") || (auswahl).equals("dual"))
        {
            System.out.println("Dual wurde ausgewählt");
            System.out.println("Dezimal: " + dezimalzahlen.dualUmrechnung());
            System.out.println("Oktal: " + oktaldezimalzahlen.dualUmrechnung());
            System.out.println("Hexa: " + hexadezimalzahlen.dualUmrechnung());
            System.out.println("-------------------------");
        }
        else 
        {
            if((auswahl).equals("Hexa") || (auswahl).equals("hexa"))
            {
                System.out.println("Hexa wurde ausgewählt");
                System.out.println("Dezimal: " + dezimalzahlen.hexadezimalUmrechnung());
                System.out.println("Dual: " + dualzahlen.hexadezimalUmrechnung());
                System.out.println("Oktal: " + oktaldezimalzahlen.hexadezimalUmrechnung());
                System.out.println("-------------------------");
            }
            else
            {
                if((auswahl).equals("Oktal") || (auswahl).equals("oktal"))
                {
                    System.out.println("Oktal wurde ausgewählt");
                    System.out.println("Dual: " + dualzahlen.oktaldezimalUmrechnung());
                    System.out.println("Dezimal: " + dezimalzahlen.oktaldezimalUmrechnung());
                    System.out.println("Hexa: " + hexadezimalzahlen.oktaldezimalUmrechnung());
                    System.out.println("-------------------------");
                }
                else
                {
                    if((auswahl).equals("Dezimal") || (auswahl).equals("dezimal") )
                    {
                        System.out.println("Dezimal wurde ausgewählt");
                        System.out.println("Dual: " + dualzahlen.dezimalUmrechnung());
                        System.out.println("Oktal: " + oktaldezimalzahlen.dezimalUmrechnung());
                        System.out.println("Hexa: " + hexadezimalzahlen.dezimalUmrechnung());
                        System.out.println("-------------------------");
                    }
                    else
                    {
                        if((auswahl).equals("Exit") || (auswahl).equals("exit"))
                        {
                            System.out.println("Das Programm wurde beendet.");
                            run = false;
                        }
                        else
                        {
                            System.out.println("Keine Übereinstimmung, bitte erneut versuchen");
                        }
                    }
                }
            }
        }
    }   
}

}

Frame_Main:

public String auswahl = "";
public void createTextField()
{

    jbutton1_go.addActionListener (new ActionListener()
    {
        public void actionPerformed (ActionEvent e)
        {
            if (jtextfield1_bezeichnung.getText().length() <= 0)
            {
                System.out.println("Error 404: Kein Zahlensystem wurde angegeben");
            }
            else
            {
                System.out.println ("Vorhandenes Zahlensystem: " + jtextfield1_bezeichnung.getText());
                auswahl = jtextfield1_bezeichnung.getText();
                //after getting the input value I would like to call the while loop
            }

            if(jtextfield1_zahl.getText().length() <= 0)
            {
                System.out.println("Error 404: Keine Zahl wurde angegeben");
            }
            else
            {
                System.out.println ("Vorhandene Zahl: " + jtextfield1_zahl.getText());
            }
        }
    });

}

Create a method for your checks, it makes no sense having them all in main. If you create a static method you can call it by:

Start.check(auswahl);

If not, you should instantiate Start. Here your start class:

public class Start {

public static void main(String[] args)
{
 //deleted some variables to make the code look cleaner
 auswahl = frame_main.auswahl;

}   


public static check(String auswahl) {
    //I want to call this while loop 
    while(run){
        System.out.println("Welche Zahl ist vorhanden? \nDual, Hexa, Oktal oder Dezimal?");

        if((auswahl).equals("Dual") || (auswahl).equals("dual"))
        {
            System.out.println("Dual wurde ausgewählt");
            System.out.println("Dezimal: " + dezimalzahlen.dualUmrechnung());
            System.out.println("Oktal: " + oktaldezimalzahlen.dualUmrechnung());
            System.out.println("Hexa: " + hexadezimalzahlen.dualUmrechnung());
            System.out.println("-------------------------");
        }
        else 
        {
            if((auswahl).equals("Hexa") || (auswahl).equals("hexa"))
            {
                System.out.println("Hexa wurde ausgewählt");
                System.out.println("Dezimal: " + dezimalzahlen.hexadezimalUmrechnung());
                System.out.println("Dual: " + dualzahlen.hexadezimalUmrechnung());
                System.out.println("Oktal: " + oktaldezimalzahlen.hexadezimalUmrechnung());
                System.out.println("-------------------------");
            }
            else
            {
                if((auswahl).equals("Oktal") || (auswahl).equals("oktal"))
                {
                    System.out.println("Oktal wurde ausgewählt");
                    System.out.println("Dual: " + dualzahlen.oktaldezimalUmrechnung());
                    System.out.println("Dezimal: " + dezimalzahlen.oktaldezimalUmrechnung());
                    System.out.println("Hexa: " + hexadezimalzahlen.oktaldezimalUmrechnung());
                    System.out.println("-------------------------");
                }
                else
                {
                    if((auswahl).equals("Dezimal") || (auswahl).equals("dezimal") )
                    {
                        System.out.println("Dezimal wurde ausgewählt");
                        System.out.println("Dual: " + dualzahlen.dezimalUmrechnung());
                        System.out.println("Oktal: " + oktaldezimalzahlen.dezimalUmrechnung());
                        System.out.println("Hexa: " + hexadezimalzahlen.dezimalUmrechnung());
                        System.out.println("-------------------------");
                    }
                    else
                    {
                        if((auswahl).equals("Exit") || (auswahl).equals("exit"))
                        {
                            System.out.println("Das Programm wurde beendet.");
                            run = false;
                        }
                        else
                        {
                            System.out.println("Keine Übereinstimmung, bitte erneut versuchen");
                        }
                    }
                }
            }
        }
    }
}

}

I think the way to go here would be called Refactoring

You have one console appliaction with a main() that prints your output to System.out .

Now you want to go GUI and cannot reuse your code, because System.out has no place in GUI applications.

Now, what do the two have in common?

They share a method where you put in some data and then print out some results.

Consider this method:

 public String showUmrechnungen(String auswahl, String zahl) {
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter();

    //Your code from main(). Replace System.out.println with out.println()

    out.flush();
    return sw.toString();
 }

You could use that in both of your situations:

 public static void main(String[] args) {
   Start instance = new Start();  
   while(running) {
      //Get the input
      String zahl = readZahl();
      String auswahl = readAuswahl();

      System.out.println(instance.showUmrechnungen(auswahl, zahl));        
   }
 }

And in your Swing-App:

 public void actionPerformed (ActionEvent e)
    {
       Start instance = new Start();
       JOptionPane.showMessageDialog(Frame_Main.this, 
           instance.showUmrechnungen(jtextfield1_bezeichnung.getText(),
                jtextfield1_zahl.getText()));                              
    }

Of cause, this is only the general idea of how to attack this situation. In your case, you'd have to think about input like "Exit" and what to do with that. So maybe it would be better to pass in a PrintStream and return a boolean for continue or not:

 public boolean showUmrechnungen(String auswahl, String zahl, PrintStream out) {
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM