这是一篇关于JAVA的聊天室室小软件,用的swing的技术同时也用到了socket。今天发布出来,希望能帮到大家。
开发环境
开发工具:IDEA2021.3.1
JDK版本:JDK8
JDK其他版本会有问题
项目结构
启动后服务的Server。
启动客户端MainLauncher
下载地址:
链接:https://pan.baidu.com/s/1CqRtwb-5Vbj8J-UmrPFi6w
提取码:4ysl
一、运行画面展示
登录界面,选择性别,输入用户名
聊天界面,第一个进来的界面里只有一个人
再登录一个账号。
两个人可以相互通信的界面
二、代码部分
1.客户端启动代码
package com.bjpowernode.client;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MainLauncher extends Application {
private static Stage primaryStageObj;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStageObj = primaryStage;
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("views/LoginView.fxml"));
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setTitle("一起来聊天");
primaryStage.getIcons().add(new Image(getClass().getClassLoader().getResource("images/logo.png").toString()));
Scene mainScene = new Scene(root, 350, 420);
mainScene.setRoot(root);
primaryStage.setResizable(false);
primaryStage.setScene(mainScene);
primaryStage.show();
primaryStage.setOnCloseRequest(e -> Platform.exit());
}
public static void main(String[] args) {
launch(args);
}
public static Stage getPrimaryStage() {
return primaryStageObj;
}
}
LoginController类
package com.bjpowernode.client.login;
import com.bjpowernode.client.MainLauncher;
import com.bjpowernode.client.chat.ChatController;
import com.bjpowernode.client.chat.Listener;
import com.bjpowernode.client.util.Drag;
import com.bjpowernode.client.util.ThreadPoolUtil;
import com.trynotifications.util.ResizeHelper;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
public class LoginController implements Initializable {
@FXML private ImageView defaultView;
@FXML private ImageView girlView;
@FXML private ImageView boyView;
@FXML public TextField hostnameTextfield;
@FXML private TextField portTextfield;
@FXML private TextField usernameTextfield;
@FXML private ChoiceBox imagePicker;
@FXML private Label selectedPicture;
@FXML private BorderPane borderPane;
public static ChatController chatController;
private Scene scene;
private static LoginController instance;
public LoginController() {
instance = this;
}
public static LoginController getInstance() {
return instance;
}
/**
* 处理点击登录按钮事件
* @throws IOException
*/
public void loginButtonAction() throws IOException {
String hostname = hostnameTextfield.getText();
int port = Integer.parseInt(portTextfield.getText());
String username = usernameTextfield.getText();
String picture = selectedPicture.getText();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/ChatView.fxml"));
Parent window = (Pane) fxmlLoader.load();
chatController = fxmlLoader.getController();
//创建监听器对象
Listener listener = new Listener(hostname, port, username, picture,chatController);
//将监听器加入到线程池
ThreadPoolUtil.poolExecutor.execute(listener);
this.scene = new Scene(window);
}
/**
* 显示界面场景
*/
public void showScene() {
Platform.runLater(() -> {
Stage stage = (Stage) hostnameTextfield.getScene().getWindow();
stage.setResizable(true);
stage.setWidth(1040);
stage.setHeight(620);
stage.setOnCloseRequest((WindowEvent e) -> {
Platform.exit();
System.exit(0);
});
stage.setScene(this.scene);
stage.setMinWidth(800);
stage.setMinHeight(300);
ResizeHelper.addResizeListener(stage);
stage.centerOnScreen();
chatController.setUsernameLabel(usernameTextfield.getText());
chatController.setImageLabel(selectedPicture.getText());
});
}
@Override
public void initialize(URL location, ResourceBundle resources) {
imagePicker.getSelectionModel().selectFirst();
selectedPicture.textProperty().bind(imagePicker.getSelectionModel().selectedItemProperty());
selectedPicture.setVisible(false);
//处理鼠标拖拽界面
new Drag().handleDrag(borderPane);
//处理头像
imagePicker.getSelectionModel().selectedItemProperty().addListener(