java - 将输入从 JButton 传递到控制台

标签 java swing user-interface console-application actionlistener

statsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

              //Return the string "stats" to gameLoop() as cmd
            }
        }); 

public void gameLoop(){
        Scanner lineScanner = new Scanner(System.in);
        cmd = "";

        System.out.print(getBoard().printBoard(false));

        while (!cmd.equals("quit")) {
            System.out.print(">");
            Scanner wordScanner = new Scanner(lineScanner.nextLine());

        if (wordScanner.hasNext()) {
            cmd = wordScanner.next();
            if (cmd.equals("board")) {
                System.out.print(getBoard().printBoard(false));
                } else if (cmd.equals("ships")) {
            System.out.print(getBoard().printBoard(true));
                } else if (cmd.equals("help")) {
                    printHelp();    
                } else if (cmd.equals("stats")) {
                    printStats();
                } else if (cmd.equals("fire")) {
            if(fire(wordScanner)) { 
            printStats();
            cmd = "quit";
                    }
                } else if (cmd.equals("quit")) {    
                } else if (!cmd.equals("")) {
                    System.out.println(ILLEGAL_COMMAND);
                }
            }

        }
    }

我想做的是,当用户单击 statsButton 时,gameLoop 中的 String cmd 将更改为“stats”。 statsButton 和 gameLoop() 位于两个不同的类中。任何人都可以告诉我该怎么做? (我已经尝试过 pipedreader/pipedwriter)但我似乎无法正确使用。

*我基本上是在尝试在不更改原始控制台应用程序的情况下将我的控制台应用程序变成 GUI 应用程序。

编辑:我试过的

Class textBased 
PipedInputStream in = new PipedInputStream()

public void gameLoop(){
    try{
        in.connect(GUIclass.out);

    Scanner lineScanner = new Scanner(in);`

Class GUIclass  
    PipedOutputStream out = new PipedOutputStream();
    PrintWriter writer;

  public GUIclass(){
    final PrintWriter writer = new PrintWriter(out);
    statsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

              writer.println("stats");
             }
          }); 

那是我尝试写的,但它似乎不起作用。

最佳答案

关于

I'm basically trying to make my console application into a GUI application without changing the original console application..."

我的建议很简单:“不要这样做”。

这两个应用程序具有完全不同的结构,一个是线性的,另一个是事件驱动的,并且不能直接相互转换。最好从头开始制作一个新的 GUI 程序。现在,如果您的非 GUI 应用程序包含一些结构良好且行为良好的面向对象类,那么一定要在 GUI 的“模型”或逻辑部分中使用这些类,但不要尝试直接转换一种类型的程序流程应用到另一个。

编辑
根据您发布的要求:

"You should be able to play your Battleship game through your GUI interface. In addition, the text-based front-end you wrote for project 1 should still "work" and be playable."

如果我错了请纠正我,但我敢打赌您这里涉及多个类,其中只有一个是“基于文本的前端”。如果是这样,请按照我上面的建议使用非前端类作为 GUI 的模型,但不要将基于文本的前端用于任何与 GUI 相关的内容,也不要尝试在您的 GUI 中模拟它。

关于java - 将输入从 JButton 传递到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8107903/

相关文章:

java - 线程运行无法正常工作

java - 为什么我不能向我的 JComboBox 添加多个数组?

Java - 反射,转换到一个未知的对象?

python - PyQt4:将操作系统日期/时间打印到标签

java - Swing:向组件及其所有装饰添加监听器?

java - Libgdx BitmapFont NullPointerException

java - apache-camel throttle 无法按预期工作

java - 如何使用 javax.lang.model 读取文件?

java - java中如何避免形状超出框架的边界

java - JTable 不会动态添加行