延迟处理直到GUI输入

时间:2014-06-22 07:53:12

标签: java swing user-interface netbeans

以下是从用户输入代理设置的代码。

 public static void setProxy()
     {   
      java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new proxy().setVisible(true);
        }
    });
// Control should halt here till user gives input as it assigns value to the variables
    String host = prox;
    String port = prt;
    System.out.println("Using proxy: " + host + ":" + port);

2 个答案:

答案 0 :(得分:2)

你没有正确地做到这一点。 GUI应用程序的主要方法应该只做一件事:启动GUI。其余的逻辑将由用户与GUI交互的事件触发。

因此,假设您的GUI显示包含2个文本字段的框架以进入主机和端口以及要继续的按钮,您应该在GUI中有按钮上的动作侦听器:

proceedButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String host = hostTextField.getText();
        String port = portTextField.getText();
        doSomethingWithHostAndPort(host, port);
    }
});

如果doSomethingWithHostAndPort()做了很长时间,那么它应该在一个单独的线程中进行,以避免冻结GUI。

答案 1 :(得分:0)

如果您想要精确答案,则需要提供更多信息。但是我要做一些假设,你输入信息后按下一个按钮,该按钮附有一个点击事件,你可以在这里添加变量赋值并开始处理。