Skip to content

Instantly share code, notes, and snippets.

@rcd27
Created July 4, 2016 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcd27/074c6741edb0bd603654e40909be6b8b to your computer and use it in GitHub Desktop.
Save rcd27/074c6741edb0bd603654e40909be6b8b to your computer and use it in GitHub Desktop.
chat client
package rcd27.com.vk.chatclientApp;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class MainClass extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("ChatClient");
initRootLayout();
initClientItSelf();
}
/**
* Отображаем основное окно
*/
public void initRootLayout() {
try {
// Загружаем корневое окно из fxml файла.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainClass.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
//Отображаем сцену, которую содержит корневое окно.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Показываем собственно окно чата
*/
public void initClientItSelf() {
try {
//Загружаем окно самого чата
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainClass.class.getResource("view/WindowItself.fxml"));
AnchorPane clientItself = (AnchorPane) loader.load();
//Ставим окно чата в центр корневого окна
rootLayout.setCenter(clientItself);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Возвращает главную сцену
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="310.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="rcd27.com.vk.chatclientApp.view.RootLayoutController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="menuClose" mnemonicParsing="false" onAction="#handleClose" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem fx:id="menuAbout" mnemonicParsing="false" onAction="#handleAbout" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
package rcd27.com.vk.chatclientApp.view;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
import rcd27.com.vk.chatclientApp.MainClass;
import java.io.Console;
public class RootLayoutController {
private Stage rootLayoutStage;
@FXML
MenuItem menuClose;
@FXML
MenuItem menuAbout;
private MainClass mainClass;
public RootLayoutController() {
}
private void initialize() {
}
public void setMainClass(MainClass mainClass) {
this.mainClass = mainClass;
}
@FXML
private void handleClose() {
//TODO: тут не понятно чё писать, чтобы окно закрылось.
System.console();//0o
}
@FXML
private void handleAbout() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.initOwner(rootLayoutStage);
alert.setTitle("About");
alert.setHeaderText("Alfa-beta-gamma-version of Chat Client. ");
alert.setContentText("Written by rcd aka Stanislav Z. \n Java 2 program on geekbrains.ru \n professor: Alexander Fisunov");
alert.showAndWait();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="rcd27.com.vk.chatclientApp.view.WindowItselfController">
<children>
<SplitPane dividerPositions="0.9" layoutX="88.0" layoutY="109.0" opacity="0.75" orientation="VERTICAL" prefHeight="400.0" prefWidth="300.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextArea fx:id="textArea" editable="false" layoutX="32.0" layoutY="-9.0" prefHeight="354.0" prefWidth="298.0" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<HBox layoutY="-1.0" prefHeight="35.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextField fx:id="textField" alignment="CENTER" nodeOrientation="RIGHT_TO_LEFT" onAction="#handleSend" prefHeight="30.0" prefWidth="225.0">
<HBox.margin>
<Insets />
</HBox.margin>
<font>
<Font size="18.0" />
</font>
</TextField>
<Button fx:id="button" layoutX="212.0" layoutY="-1.0" mnemonicParsing="false" onAction="#handleSend" prefHeight="43.0" prefWidth="79.0" text="Send" />
</children>
</HBox>
</children>
</AnchorPane>
</items>
<opaqueInsets>
<Insets />
</opaqueInsets>
</SplitPane>
</children>
</AnchorPane>
package rcd27.com.vk.chatclientApp.view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import rcd27.com.vk.chatclientApp.MainClass;
public class WindowItselfController {
@FXML
private TextField textField;
@FXML
private TextArea textArea;
@FXML
private Button button;
private MainClass mainClass;
public WindowItselfController() {
}
private void initialize() { //вот это я не знаю пока зачем, в учебнике так пишут
}
public void setMainClass(MainClass mainClass) {
this.mainClass = mainClass;
}
/**
* Аналог листенера, который вешаем в джава свинге
*/
@FXML
private void handleSend() {
textArea.setText(textField.getText());
textField.setText("");
textField.requestFocus();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment