3 changed files with 175 additions and 23 deletions
@ -0,0 +1,105 @@ |
|||
package window; |
|||
|
|||
import javafx.application.Application; |
|||
import javafx.scene.Scene; |
|||
import javafx.scene.control.Alert; |
|||
import javafx.scene.control.Button; |
|||
import javafx.scene.control.Label; |
|||
import javafx.scene.control.TextField; |
|||
import javafx.scene.layout.FlowPane; |
|||
import javafx.stage.Stage; |
|||
|
|||
import java.io.IOException; |
|||
import java.net.ConnectException; |
|||
import java.net.Socket; |
|||
import java.net.UnknownHostException; |
|||
|
|||
public class LoginWindow extends Application { |
|||
|
|||
@Override |
|||
public void start(Stage stage) throws Exception { |
|||
this.stage = stage; |
|||
|
|||
FlowPane root = new FlowPane(); |
|||
stage.setMaximized(true); |
|||
stage.setTitle("Clype Login"); |
|||
stage.setScene(new Scene(root, 345, 100)); |
|||
|
|||
root.setPrefHeight(70); |
|||
root.setPrefWidth(345); |
|||
|
|||
Label servernameLabel = new Label(" Server name and port: "); |
|||
TextField servernameTextField = new TextField(); |
|||
Label usernameLabel = new Label(" Username: "); |
|||
TextField usernameTextField = new TextField(); |
|||
Button connectButton = new Button("Connect"); |
|||
|
|||
servernameTextField.setTranslateY(6); |
|||
usernameTextField.setTranslateY(6); |
|||
servernameLabel.setTranslateY(6); |
|||
usernameLabel.setTranslateY(6); |
|||
connectButton.setTranslateX(6); |
|||
connectButton.setTranslateY(6); |
|||
|
|||
connectButton.setOnMouseClicked(mouseEvent -> { |
|||
String[] servernameAndPort = parseServernameAndPort(servernameTextField.getText()); |
|||
int port; |
|||
|
|||
try { |
|||
port = Integer.parseInt(servernameAndPort[1]); |
|||
|
|||
if (port > 65536 || port < 0) { |
|||
Alert alert = new Alert(Alert.AlertType.ERROR); |
|||
alert.setTitle("Parsing error"); |
|||
alert.setContentText("The port you specified isn't valid! It must be between 0 and 65535"); |
|||
alert.show(); |
|||
} |
|||
else { |
|||
Socket socket = new Socket(servernameAndPort[0], port); |
|||
ChatWindow chatWindow = new ChatWindow(servernameAndPort[0], usernameTextField.getText(), socket, this); |
|||
Stage chatWindowStage = new Stage(); |
|||
chatWindow.start(chatWindowStage); |
|||
} |
|||
} catch (NumberFormatException nfe) { |
|||
Alert alert = new Alert(Alert.AlertType.ERROR); |
|||
alert.setTitle("Parsing error"); |
|||
alert.setContentText("The port you specified isn't an integer!"); |
|||
alert.show(); |
|||
} catch (UnknownHostException e) { |
|||
Alert alert = new Alert(Alert.AlertType.ERROR); |
|||
alert.setTitle("Unknown Host"); |
|||
alert.setContentText("This server does not exist."); |
|||
alert.show(); |
|||
} catch(ConnectException ce) { |
|||
Alert alert = new Alert(Alert.AlertType.ERROR); |
|||
alert.setTitle("Connection Problems"); |
|||
alert.setContentText("There was an issue connecting to the server."); |
|||
alert.show(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
stage.hide(); |
|||
}); |
|||
|
|||
root.getChildren().addAll(servernameLabel, servernameTextField, usernameLabel, usernameTextField, connectButton); |
|||
stage.show(); |
|||
} |
|||
|
|||
public void show() { |
|||
stage.show(); |
|||
} |
|||
|
|||
private String[] parseServernameAndPort(String servernameAndPort) { |
|||
int colonLoc = servernameAndPort.indexOf(':'); |
|||
if (colonLoc == -1) { |
|||
return new String[]{servernameAndPort, "25519"}; |
|||
} |
|||
else { |
|||
return new String[]{servernameAndPort.substring(0, colonLoc - 1), servernameAndPort.substring(colonLoc + 1)}; |
|||
} |
|||
} |
|||
|
|||
void main() {launch();} |
|||
|
|||
private Stage stage; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue