java - 我怎样才能让这段代码在 Eclipse 之外运行并允许用户在 JFrame 或类似的东西中输入/输出

标签 java

我为编程类(class)编写了这段代码,它完整且正确,我只是出于我自己的知识对此感兴趣。我怎样才能让这个程序在 Eclipse 之外运行并将扫描器和输出放入 JFrame 或类似的东西中?任何意见和建议都意义重大。我对 java 很陌生,这是我的第三个程序。

这是主类

    import java.util.Scanner;
    public class SeqGenTest 
    {

/**
 * Instance Variables passed to SeqGen.java
 */
String max;
String minmax;
String maxdiv;
boolean loop = true;
int a;
int b;
int c;
/**
 * main method that allows user input to select and execute commands
 * based upon the input of said user
 * @param max, minmax, maxdiv,
 * @return ints based upon calculation
 */
public static void main(String[] args) 
{
    boolean loop = true;
    while(loop = true)
    {
    System.out.println("Enter a command (max, minmax, maxdiv, end)");
    Scanner inpt = new Scanner(System.in);
    String userInpt = inpt.nextLine();

    if (userInpt.equalsIgnoreCase("max"))
    {
        System.out.println("Please enter your Max (intergers)");
        int maximum = 0; //= inpt.nextInt();
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (maximum <= 0)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (maximum);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("minmax"))
        {
        System.out.println("Please enter your min, max (intergers)");
        int minimum = 0;
        int maximum = 0;
        try {
            minimum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (minimum > maximum)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (minimum, maximum);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("maxdiv"))
        {
        System.out.println("Please enter your min, max, div (intergers)");
        int minimum = 0;
        int maximum = 0;
        int divisor = 0;
        try {
            minimum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            divisor = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (maximum < minimum || minimum < 1 || divisor > maximum)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200 || divisor < -9 || divisor > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201 && divisor > 0 && divisor < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (minimum, maximum, divisor);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("end"))
        {
        System.out.println("Program Terminated Normally");
        inpt.close();
        System.exit(0);
        }
    else
        {
        System.out.println("Error, bounds exception. System will now     exit");
        inpt.close();
        System.exit(0);
        }

    }   
    }
    }

这里是“其他类(class)?”我不确定正确的术语是什么。

    public class SeqGen
{
//instance vars
int minmax;
int max;
int maxdiv;
int current;
boolean hasNextValue;
boolean nextValue;
boolean isMax;
boolean isMinMax;
boolean isMaxDiv;
int getNextValue;
/**
 * Constructor for objects of class SeqGen
 */
public SeqGen(int a)
{
     minmax = 1;
     max = a;
     maxdiv = 1;
     current = minmax;
     isMax = true;

}

/**
 * Constructor for objects of class SeqGen 
 */
public SeqGen(int b, int a)
{
     minmax = b;
     max = a;
     maxdiv = 1;
     current = minmax;
     isMinMax = true;
}
public SeqGen(int b, int a, int c)
{
    minmax = b;
    max = a;
    maxdiv = c;
    current = minmax;
    isMaxDiv = true;

}

public boolean hasNextValue()
{
    if (current <= max) {return true; }
    else {return false;}
}
/**
 * Computes math based upon user entered commands
 * @return correct computation (int)
 */
public int getNextValue()
{
    while (current < max)
    {
        if (current%maxdiv == 0)
       {
         return current++;
       }
    else
       {
         current++;
       }
    }   
    return current++;

}
}

最佳答案

基本上我可以想到两种选择:

  • 扩展 JFrame 类并使用带有 JTextArea 的嵌入式控制台,如本 answer 所示。 .

  • 扩展JFrame类并创建一个框架,其中有一个按钮来执行从具有所有不同命令的JCombobox中选择的命令,一堆JTextField来获取用户输入的值并使用JLabel打印结果。

    public class SeqGenTest extends JFrame{
    
        public SeqGenTest(){
    
            //set up all you UI here.
    
            //use these to show the frame.
            setSize(600, 600);
            setVisible(true);
        }
    }
    

其他信息:

关于java - 我怎样才能让这段代码在 Eclipse 之外运行并允许用户在 JFrame 或类似的东西中输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927513/

相关文章:

java - 带有 hessian over HTTP 的 Spring RPC 在 maven tomcat 插件上运行良好,但在 Tomcat 服务器上返回 http 500 错误

Java 字符串与流

java - 如何为 EJB 及其接口(interface)编写 Javadoc?

java - 在 Java 中创建网格点列表

java - 家庭作业问题。为日历约会程序编写方法时遇到问题

Java 类型泛型作为 GSON 的参数

java - 将 Map<Object, Object> 转换为 Json 字符串

java - 如何在 Java ME 中将 double 转换为字符串

java - 制作一个记录器以跳过 logback 中的根部分

java - 如何在 Java 中进行 LZW 解码?