package Server;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import Utils.ToServer;
/**
* 入口文件:
* 本文件依赖JavaFX,不要是实现表格功能,对于新手来说相对复杂,如果不要表格只要预览的情况下可以简化或去除本文件
*
*/
public class Windows extends Application {
public static Windows windows = new Windows();
public static LinkedHashMap<String, ToServer> pcData = new LinkedHashMap<String, ToServer>();
public static Map<String, Boolean> connect = new HashMap<>();
public TableView<Billing> table = new TableView<Billing>();
public final ObservableList<Billing> tableData = FXCollections.observableArrayList();
public static void main(String[] args) {
new Thread(Server::server).start();
windows.launch(args);
}
/**
* 实现表格初始化
* @param stage *
*/
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setWidth(600);
stage.setHeight(600);
table.setEditable(false);
table.setPrefWidth(575);
table.setPrefHeight(500);
stage.setResizable(false);
//table.setMinSize(1000,600);
//table.setMaxSize(1000,600);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn idCol = new TableColumn("ID");
idCol.setMinWidth(20);
idCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("pcId"));
TableColumn pcNameCol = new TableColumn("机器名称");
pcNameCol.setMinWidth(50);
pcNameCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("pcName"));
//nameCol.setCellFactory(cellFactory);
TableColumn netIdCol = new TableColumn("机器IP");
netIdCol.setMinWidth(50);
netIdCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("pcIp"));
TableColumn macIdCol = new TableColumn("机器Mac");
macIdCol.setMinWidth(50);
macIdCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("macId"));
TableColumn pcStateCol = new TableColumn("机器状态");
pcStateCol.setMinWidth(30);
pcStateCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("pcState"));
TableColumn actionCol = new TableColumn("操作");
actionCol.setMinWidth(130);
actionCol.setCellValueFactory(new PropertyValueFactory<Billing, String>("pcAction"));
Button b1 =new Button("远程控制");
Button b2 =new Button("监控预览");
Button b3 =new Button("开机");
Button b4 =new Button("关机");
Button b5 =new Button("重启");
setOnAction(b1,1);
setOnAction(b2,2);
setOnAction(b3,3);
setOnAction(b4,4);
setOnAction(b5,5);
//TableColumn amount = new TableColumn("Amount");
//amount.getColumns().addAll(rateCol, priceCol);
table.getColumns().addAll( idCol, pcNameCol, netIdCol, pcStateCol, actionCol );
//table.setItems(data);
loadTableData();
getTableData();
table.setItems(tableData);
HBox hb = new HBox();
hb.getChildren().addAll(b1,b2,b3,b4,b5);
hb.setAlignment(Pos.BASELINE_LEFT);
hb.setSpacing(16);
final Label label2 = new Label();
label2.setAlignment(Pos.BASELINE_RIGHT);
VBox vbox = new VBox();
vbox.setSpacing(15);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(hb,table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
//对话框 Alert Alert.AlertType.CONFIRMATION:反问对话框
Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
//设置对话框标题
alert2.setTitle("提示");
//设置内容
alert2.setHeaderText("确定要关闭程序吗?");
//显示对话框
Optional<ButtonType> result = alert2.showAndWait();
//如果点击OK
if (result.get() == ButtonType.OK){
// ... user chose OK
stage.close();
System.exit(0);
//否则
} else {
event.consume();
}
}
});
stage.setTitle("控制台-机器数量["+pcData.size()+"](JDK:"+System.getProperty("java.version")+")");
/**
* 程序结束前启动刷新表格内容线程
*/
Thread thread = new Thread(() -> {
while(true) {
refresh(table.getSelectionModel().getFocusedIndex());
try {
Thread.sleep(3000);
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}
});
thread.start();
}
/**
* 载入本地缓存数据,初始化表格内容:即历史机器列表
*/
public void loadTableData(){
String str = Utils.readTxtFile("data.dat",";");
String[] strArr = str.split(";");
//if(strArr.length>0) return;
//System.out.println(strArr.length);
for(int i = 0; i< strArr.length; i++){
if(strArr[i].split("=")[0].equals("")) continue;
ToServer t = new ToServer();
t.name=strArr[i].replace(" ","").split("=")[0];
t.ip=strArr[i].replace(" ","").split("=")[1];
t.mac=strArr[i].replace(" ","").split("=")[2];
t.time=0;
pcData.put(t.mac,t);
connect.put(t.mac,false);
}
}
/**
* 装载表格实时数据/刷新表格实时数据
*/
public void getTableData(){
tableData.clear();
ToServer toServer;
int i=0;
for (String kk : pcData.keySet()) {
String pcState = "离线";
toServer = pcData.get(kk);//机器信息
//Api.pcStateDate = Api.getPcState().get(kk);//活动机器列表
if(toServer !=null){
if(System.currentTimeMillis()- toServer.time<5000) {
pcState = "在线";
}
i++;
tableData.add(new Billing(i+"",pcState,kk,toServer));
}
}
}
/**
* 刷新表格
* @param selRow 当前已选的行,以备刷新后不影响当前的选择
*/
public void refresh(int selRow){
getTableData();
//table.setItems(data);
table.getSelectionModel().select(selRow);
}
/**
* 提示窗口
* @param msg 提示信息
* @return 确定还是取消
*/
public static boolean alert(String msg){
//对话框 Alert Alert.AlertType.CONFIRMATION:反问对话框
Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
//设置对

zjc7098952
- 粉丝: 0
最新资源
- 基于MATLAB的PCM仿真.doc
- 云计算毕业论文.docx
- 大数据驱动的区域卫生平台建设方案PPT课件.ppt
- 自动化立体仓库毕业论文.doc
- 基于JAVA的购物网站(毕业论文).doc
- 计算机基础笔试试卷.doc
- 基于Web的在线实时通讯系统.doc
- 基于单片机的万年历实习报告.docx
- 基于无线传感网络的路灯采集系统.doc
- 多项目管理体系论文:地产公司大规模多项目开发条件下的产品保障体系.doc
- 自动化生产线安装与调试毕业论文.doc
- 实验三-信号卷积的MATLAB实现.doc
- c语言期末复习试卷.doc
- 信息系统安全自查报告.doc
- 基于matlab的伪随机序列生成及相关函数仿真实验.doc
- PLC和变频器综合实验报告.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页