如何在我的扫描仪之前调用GUI代码?

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

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

这是我测试过的代码……

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。

  final String response = in.nextLine(); EventQueue.invokeLater(new Runnable() { @Override public void run() { new Client(response); } }); 

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

附录:仔细阅读其他线程 ,您可以使用以下方法在单独的JVM中启动NamedFrame

 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]); } }); } } 

代码似乎没问题。 客户端中是否存在您未在此处显示的任何类级别内容(例如静态成员等?)

链接中的整个切换工作区描述是一个操作系统级别的东西,而不是特定于java。

是否有java命令的选项或mac可以使用的东西?