简体   繁体   English

JAVA-从另一个类调用main方法的一部分

[英]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. 公共类Start和公共类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. 我通过GUI控制公共类Frame_Main,在其中键入一些内容,然后按一下按钮,以激活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. 通过GUI输入之后,我想调用Start类,并检查输入是否与我准备的某些用户答案相同,但是我找不到一种方法来实现该功能,Eclipse在我总是显示错误尝试创建一个包含函数的公共void或类。

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: 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 . 您有一个带有main()控制台应用程序,该应用程序将输出打印到System.out

Now you want to go GUI and cannot reuse your code, because System.out has no place in GUI applications. 现在您要转到GUI, 并且不能重用您的代码,因为System.out在GUI应用程序中没有位置。

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: 在您的Swing应用程序中:

 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: 因此,也许最好传入一个PrintStream并返回一个布尔值以继续或不继续:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:从另一个类的主方法调用主方法 - Java: Call a main method from a main method in another class 如何从Java中的另一个类调用Main方法作为进程 - How to call Main Method from another Class in Java as a Process 从另一个类(不是主类)调用方法 - Call a method from another class(not main class) 如何在JAVA中使用另一个类对象从主类调用主类的方法 - how to call a method of main class from main class with another class object in JAVA 从另一个中的main方法调用变量-Java - call a variable from main method in another - java java中如何在没有main方法的情况下从一个类调用toString()方法到另一个类 - How to call toString() Method from one class to another class without main method in java 从jar(不是main)调用java类方法 - Call java class method from jar (not main) Java:如何在另一个扩展Thread的类中调用方法,并且该方法是我的主类中的Thread Array的一部分? - Java : How do I call a method in a different class that extends Thread and is a part of a Thread Array from my main class? 为什么不从另一个类中调用一个类的静态方法“ main”? - Why not call the static method “main” of a class from another class? 无法从另一个类到主类调用静态方法 - Can't call a static method from another class to main class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM