如何在我的扫描仪之前调用GUI代码? [英] How is GUI code being invoked before my Scanner?

查看:86
本文介绍了如何在我的扫描仪之前调用GUI代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打开GUI窗口之前从命令行获取输入时遇到了一些麻烦。我之前在Apple Exchange上问过这个问题,但是在我们确定它是一个编程问题后发送到这里。基本上我在打开一个窗口之前运行扫描仪来获取用户输入,但它启动程序,在我的Mac上切换空间,然后我必须切换回工作区,其中有终端来回答问题。这是原始问题的链接。

I'm having some trouble with getting input from the command line before opening a GUI window. I asked this question previously on Apple Exchange but was sent here after we determined it to be a Programming problem. Basically I'm running a Scanner to get user input before I open up a window but it starts the program, switching spaces on my Mac, and then I have to switch back to the workspace with the terminal in it to answer the question. Here's a link to the original question.

https://apple.stackexchange.com/questions/45058/lion-fullscreen-desktop-switching-quirk/45065#comment51527_45065

以下是我测试过的代码......

Here's the code I've tested with...

public class Client extends JFrame {

  public static void main(String[]args) {
    Scanner in = new Scanner(System.in);
    System.out.printf("\nGive me a size for the screen: ");
    String response = in.nextLine();
    new Client(response);
  }

  public Client(String title) {
    super(title);
    super.setVisible(true);
  }

}


推荐答案

使用 invokeLater() 在获得输入后启动GUI

Use invokeLater() to start the GUI after you get the input.

    final String response = in.nextLine();
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Client(response);
        }
    });

请注意,由于时序差异,您的示例在我的平台上运行良好。还可以考虑使用 args 数组来传递参数,或者询问实现,如 FullScreenTest

Note that your example runs fine on my platform due to timing differences. Also consider using the args array to pass parameters, or ask the implementation, as shown in FullScreenTest

附录:阅读其他主题更近一点,您可以使用以下方法启动 NamedFrame 在一个单独的JVM中。

Addendum: Reading your other thread a little closer, you can use the following approach that launches a NamedFrame in a separate JVM.

package cli;

import java.awt.EventQueue;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;

/** @see https://stackoverflow.com/q/9832252/230513 */
public class CommandLineClient {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Give me a name for the screen: ");
        final String response = in.nextLine();
        try {
            ProcessBuilder pb = new ProcessBuilder(
                "java", "-cp", "build/classes", "cli.NamedFrame", response);
            Process proc = pb.start();
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
    }
}

class NamedFrame extends JFrame {

    public NamedFrame(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new NamedFrame(args[0]);
            }
        });
    }
}

这篇关于如何在我的扫描仪之前调用GUI代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆