使用
invokeLater()启动GUI
后, 你得到的输入。
final String response = in.nextLine(); EventQueue.invokeLater(new Runnable() { @Override public void run() { new Client(response); } });
请注意,由于时序差异,您的示例在我的平台上运行良好。还可以考虑使用
args数组传递参数,或询问实现方法,如
FullScreenTest
附录:仔细阅读其他线程,可以使用以下方法
Namedframe在单独的JVM
中启动。
package cli;import java.awt.EventQueue;import java.io.IOException;import java.util.Scanner;import javax.swing.Jframe;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]); } }); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)