服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - 基于socket和javaFX简单文件传输工具

基于socket和javaFX简单文件传输工具

2020-03-28 11:22uncle_zhang JAVA教程

这篇文章主要介绍了基于socket和javaFX简单文件传输工具的相关资料,需要的朋友可以参考下

本文实例介绍了基于socket和javaFX简单文件传输工具,分享给大家供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package application;
   
import java.io.File;
 
import org.james.component.ButtonBox;
import org.james.component.FileReceiverGrid;
import org.james.component.FileSenderGrid;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
 
 
public class Main extends Application {
   
  public static Stage primaryStage;
   
  @Override
  public void start(Stage primaryStage) {
    try {
      this.primaryStage = primaryStage;
      primaryStage.setFullScreen(false);
      primaryStage.setResizable(false);
       
      FileReceiverGrid fileReceiverGrid = new FileReceiverGrid();
      fileReceiverGrid.initialize();
      FileSenderGrid fileSenderGrid = new FileSenderGrid();
      fileSenderGrid.initialize();
       
      ButtonBox buttonBox = new ButtonBox();
      buttonBox.initialize();
       
      BorderPane root = new BorderPane();
      root.setTop(fileReceiverGrid);
      root.setBottom(buttonBox);
       
      buttonBox.getReceiveFileFunc().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
          buttonBox.getReceiveFileFunc().setDisable(true);
          buttonBox.getSendFileFunc().setDisable(false);
          root.setTop(fileReceiverGrid);
        }
      });
       
      buttonBox.getSendFileFunc().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
          buttonBox.getReceiveFileFunc().setDisable(false);
          buttonBox.getSendFileFunc().setDisable(true);
          root.setTop(fileSenderGrid);
        }
      });
       
      fileSenderGrid.getSelectFileBtn().setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
          FileChooser fileChooser = new FileChooser();
          fileChooser.setTitle("打开文件");
          File selectedFile = fileChooser.showOpenDialog(primaryStage);
          if(selectedFile != null){
            fileSenderGrid.setFile(selectedFile);
            fileSenderGrid.getFileNameLabel().setText(selectedFile.getPath());
          }
        }
      });
       
      Scene scene = new Scene(root,800,400);
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
      primaryStage.setScene(scene);
      primaryStage.show();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
   
   
  public static void main(String[] args) {
    launch(args);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐