Browse Source

Added the ability to add a message to the chat part of the window.

server
Cameron Weinfurt 2 years ago
parent
commit
6b497e5aed
  1. 36
      window/ChatWindow.java

36
window/ChatWindow.java

@ -3,13 +3,10 @@ package window;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.scene.control.Button;
public class ChatWindow extends Application {
@ -21,6 +18,10 @@ public class ChatWindow extends Application {
this.servername = servername;
}
public void addMessage(String user, String message) {
messageLabel.textProperty().setValue(messageLabel.textProperty().getValue() + '\n' + user + ": " + message);
}
@Override
public void start(Stage primaryStage) throws Exception{
AnchorPane root = new AnchorPane();
@ -33,13 +34,26 @@ public class ChatWindow extends Application {
AnchorPane.setLeftAnchor(statusBar, 0.0);
AnchorPane.setRightAnchor(statusBar, 0.0);
statusBar.setPrefHeight(20);
statusBar.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
Pane inputPane = new Pane();
AnchorPane inputPane = new AnchorPane();
AnchorPane.setBottomAnchor(inputPane, statusBar.getPrefHeight());
AnchorPane.setLeftAnchor(inputPane, 0.0);
AnchorPane.setRightAnchor(inputPane, 0.0);
inputPane.prefHeightProperty().setValue(40);
Button inputButton = new Button("Send");
AnchorPane.setRightAnchor(inputButton, 6.0);
AnchorPane.setBottomAnchor(inputButton, 6.0);
inputButton.prefWidthProperty().setValue(80);
TextField inputBox = new TextField();
AnchorPane.setLeftAnchor(inputBox, 6.0);
AnchorPane.setBottomAnchor(inputBox, 6.0);
inputBox.prefWidthProperty().bind(inputPane.widthProperty().subtract(inputButton.prefWidthProperty()).subtract(18.0));
inputPane.getChildren().addAll(inputBox, inputButton);
FlowPane usersPane = new FlowPane();
AnchorPane.setTopAnchor(usersPane, 0.0);
AnchorPane.setRightAnchor(usersPane, 0.0);
@ -52,11 +66,23 @@ public class ChatWindow extends Application {
messagePane.prefWidthProperty().bind(root.widthProperty().subtract(usersPane.widthProperty()));
messagePane.prefHeightProperty().bind(root.heightProperty().subtract(statusBar.heightProperty()).subtract(inputPane.heightProperty()));
messageLabel = new Label();
messageLabel.setTranslateX(10.0);
messageLabel.setTranslateY(5.0);
messageLabel.setWrapText(true);
messageLabel.prefWidthProperty().bind(messagePane.prefWidthProperty().subtract(20.0));
messagePane.setContent(messageLabel);
root.getChildren().addAll(statusBar, inputPane, usersPane, messagePane);
primaryStage.show();
for (int i = 0; i < 400; i++) {
addMessage("Anon", "");
}
}
public static void main(String[] args) {launch(args);}
private String servername;
private Label messageLabel;
}
Loading…
Cancel
Save