首先,我很遗憾可能标题错了。我会解释我的问题。我有一个带有showinputdialog组件的joptionpane来输入服务器地址。我想点击取消按钮后,它将返回主窗口。但在我的情况下,它改为第二个joptionpane。
我的英语不好:) ... 有人可以帮帮我吗?
这是我的代码
private static int setPortNumber()
{
String portNumber = JOptionPane.showInputDialog(frame,
"Enter the Port number for server creation","Server Connection\n",
JOptionPane.OK_CANCEL_OPTION);
int PORT = Integer.parseInt(portNumber);
return PORT;
}
private static String setServerName()
{
server_address = JOptionPane.showInputDialog(frame,
"Enter Server Address or PC-Name.", "Server Connection",
JOptionPane.OK_CANCEL_OPTION);
return server_address;
}
private void networking() {
server_address = setServerName();
try {
PORT = setPortNumber();
if (server_address != null) {
sock = new Socket(InetAddress.getByName(server_address) ,
PORT);
}
else {
SocketException sc = new SocketException();
throw sc;
}
// Recieving input and output streams
InputStreamReader ir = new InputStreamReader(sock.getInputStream());
br = new BufferedReader(ir);
pw = new PrintWriter(sock.getOutputStream());
login.setEnabled(true);
incoming.append("Connected to Server.please login.\n");
connect.setEnabled(false);
pw.println("~##~");
pw.flush();
login.requestFocus();
}
答案 0 :(得分:2)
private void networking() {
server_address = setServerName();
if(server_address == null || server_address.equals("")){
//Handle what happens when the server name is empty (or the user clicked the cancel button. If you let the execution continue, then the try/catch block below will pop up the second jInputDialog
}
try {
PORT = setPortNumber();
if (server_address != null) {
sock = new Socket(InetAddress.getByName(server_address) ,
PORT);
}
else {
SocketException sc = new SocketException();
throw sc;
}
...
}
答案 1 :(得分:1)
运行此示例。它工作正常
import javax.swing.JOptionPane;
public class NetWorking {
public static String server_address;
public static void main(String[] args) {
networking();
}
private static int setPortNumber()
{
String portNumber = JOptionPane.showInputDialog(null,
"Enter the Port number for server creation","Server Connection\n",
JOptionPane.OK_CANCEL_OPTION);
int PORT = Integer.parseInt(portNumber);
return PORT;
}
private static void setServerName()
{
server_address = JOptionPane.showInputDialog(null,
"Enter Server Address or PC-Name.", "Server Connection",
JOptionPane.OK_CANCEL_OPTION);
}
private static void networking(){
setServerName();
if (server_address == null) {
return;
}
Integer port = setPortNumber();
if (port != null) {
System.out.println(port);
}
}
}