سؤال

I am working on a console for my java application. i would like to be able to write damageShip into the console with some numbers after it. i would like to be able to filter out the first check if it is equal to any command and if it is take the numbers after it and call a function using these numbers. i have these two classes wich might be helpfull.
package Windows;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConsoleWindow {
static JFrame window = new JFrame("Console");
static JPanel panel = new JPanel(new GridLayout(1,2,5,5));
static JPanel panel2 = new JPanel(new GridLayout(1,1,5,5));
static JTextField cmd = new JTextField(50);
static JButton execute = new JButton("GO");
public static void openWindow(){
    panel.add(cmd);
    panel2.add(execute);
    window.setLayout(new FlowLayout());
    window.add(panel);
    window.add(panel2);
    window.pack();
    execute.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }

    });
    window.setVisible(true);
}
}


public class Stats {
public static int shipShields = 1000;
public static int crewNum = 100;
public static int shipHealth = 1000;
public static void killCrew(int amount){
    System.out.println("Killing "+amount+" crew");
}
public static void damageShip(int amount){
    System.out.println("Damaging ship for "+amount+" damage");
    if(shipShields > 0 && amount < shipShields){

    }

}
}
هل كانت مفيدة؟

المحلول

check commons-cli it does exactly what you are asking for, internally it is parsing String[] against pre defined options

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top