单击取消按钮showInputDialogue [英] Clicking the cancel button showInputDialogue

查看:974
本文介绍了单击取消按钮showInputDialogue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于按下inputDialoguebox的取消按钮的问题。我之前已经问过类似的问题,所以如果我好像重复自己,我会道歉。

I have a question in regards to pressing the cancel button of my inputDialoguebox. I have asked a similar question before so I apologize if I seem to repeat myself.

我遇到的主要问题是我的代码执行无论我是否按下取消,即使我没有添加任何输入也会进行套接字连接。

The main problem I have is that my code executes regardless of me pressing cancel and a socket connection does get made even if I don't add any input.

为什么会发生这种情况?如何避免这种情况?

Why does this happen and how can I avoid this?

String input = "";
           try
           {
               InetAddress host = InetAddress.getLocalHost();
               String hostAddress = host.getHostAddress();

               //setting label to host number so as to know what number to use
               labHostName.setText("(" + hostAddress + ")");

               input = JOptionPane.showInputDialog(null,"Please enter host name to access server(dotted number only)...see number on frame", "name", JOptionPane.INFORMATION_MESSAGE); 

               if(input != null && "".equals(input))//input != null && input.equals(""))   
               {
                   throw new EmptyFieldsException();



               }
               else if(input != null && !input.equals(hostAddress))
               {
                   throw new HostAddressException();


               }

               else
               {

                    clientSocket = new Socket(input, 7777);

因此,即使我这样做,代码仍然是即使是clientocket连接也是如此按取消。这可能是因为我在同一台机器上将服务器和客户端作为两个单独的程序?我怎样才能避免这种情况发生?

So with the code being the way it is at the moment the clientsocket connection is made even if I do press cancel. Is the reason for this perhaps because I have the Server and Client as two seperate programs on the same machine? How can I avoid this from happening?

推荐答案

当你点击取消按钮 showInputDialog(...)的$ c>,总是得到一个空值,没有条件满足,因此总是建立一个新的连接。
所以你可以这样添加这样的条件:

When you click on the Cancel Button of the showInputDialog(...) , you always get a null value, for which no condition is satisfied, hence a new connection is always established. So you can add this condition like this :

if(input == null || (input != null && ("".equals(input))))   
{
    throw new EmptyFieldsException();
}

这篇关于单击取消按钮showInputDialogue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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