利用JavaFx实现三种页面置换算法的动态表格输出
package pageRA;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class GUI extends Application {
Button bt = new Button("刷新数据");
TableView<Data1> tbv1 = new TableView<>();// 表一,用于显示页访问序列和页码
static TableView<Data2> tbv2 = new TableView<>();// 表2,用于页框数和各算法的命中率
static HBox hbox = new HBox();
VBox vbox = new VBox();
VBox vbox1 = new VBox();
@SuppressWarnings("unchecked")
@Override
public void start(Stage primaryStage) throws Exception {
// 设置button,实现刷新数据
bt.setOnAction((ActionEvent) -> {
// 创建数据对象并添加到表格视图中
// 分别用int[],和String[]来进行接收
// 用ObservableList<Data1> 类型的 dataList 变量来存储数据。
Address ad = new Address();
PageAccess pa = new PageAccess(ad);
//存放表1数据
int addressColum[] = ad.address;
String pageAddressColumn[] = pa.pageAccessStr;
ObservableList<Data1> dataList1 = FXCollections.observableArrayList();
for (int i = 0; i < ad.address.length; i++) {
Data1 d1 = new Data1(i, addressColum[i], pageAddressColumn[i]);
dataList1.add(d1);
}
tbv1.setItems(dataList1);
//存放表2数据
ObservableList<Data2> dataList2 = FXCollections.observableArrayList();
for(int i=4;i<=40;i++) {
double hit[] = new double[3];//存放三种算法的命中率
PageReAlgor pra = new PageReAlgor(i, pa);
hit[0] = pra.opt(i, pa);
hit[1] = pra.fifo(i, pa);
hit[2] = pra.lru(i, pa);
Data2 data2 = new Data2(i, hit[0], hit[1], hit[2]);
dataList2.add(data2);
}
tbv2.setItems(dataList2);
});
// 设置表格1
tbv1.setEditable(false);// 不可编辑
TableColumn<Data1, Integer> id = new TableColumn<>("ID");
id.setMinWidth(100);
id.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<Data1, Integer> address = new TableColumn<>("Address");
address.setMinWidth(100);
address.setCellValueFactory(new PropertyValueFactory<>("address"));
TableColumn<Data1, String> pageAddress = new TableColumn<>("PageAddress");
pageAddress.setMinWidth(150);
pageAddress.setCellValueFactory(new PropertyValueFactory<>("pageAddress"));
// 将表格列添加到表格视图中
tbv1.getColumns().addAll(id, address, pageAddress);
// 设置表格2
setData2();
// 将组件放入VBox
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(bt, tbv1);
vbox1.setSpacing(5);
vbox1.setPadding(new Insets(38, 0, 0, 10));
vbox1.getChildren().addAll(tbv2);
hbox.setSpacing(10);
hbox.setPadding(new Insets(10, 5, 5, 10));
hbox.getChildren().addAll(vbox,vbox1);
Scene scene = new Scene(hbox);
primaryStage.setTitle("GUI Sample");
primaryStage.setWidth(900);
primaryStage.setHeight(500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Data1 {// 两列
private final int address;
private final String pageAddress;
private final int id;
public Data1(int id, int address, String pageAddress) {
this.id = id;
this.address = address;
this.pageAddress = pageAddress;
}
// 必须要有返回值表中才会有数据
public int getAddress() {
return address;
}
public String getPageAddress() {
return pageAddress;
}
public int getId() {
return id;
}
}
public static class Data2 {
private int pageNum;// 页框数
private double optR;// 命中率
private double fifoR;
private double lruR;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public double getOptR() {
return optR;
}
public void setOptR(double optR) {
this.optR = optR;
}
public double getFifoR() {
return fifoR;
}
public void setFifoR(double fifoR) {
this.fifoR = fifoR;
}
public double getLruR() {
return lruR;
}
public void setLruR(double lruR) {
this.lruR = lruR;
}
public Data2(int pageNum, double optR, double fifoR, double lruR) {
super();
this.pageNum = pageNum;
this.optR = optR;
this.fifoR = fifoR;
this.lruR = lruR;
}
}
@SuppressWarnings("unchecked")
public static void setData2() {
// 设置表头
tbv2.setEditable(false);// 不可编辑
TableColumn<Data2, Integer> pageNumCol = new TableColumn<>("页框数");
pageNumCol.setMinWidth(100);
pageNumCol.setCellValueFactory(new PropertyValueFactory<>("pageNum"));// 要与Data2所设的变量名对应
TableColumn<Data2, Double> optC = new TableColumn<>("OPT");
optC.setMinWidth(100);
optC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getOptR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));
return new ReadOnlyObjectWrapper<>(formattedValue);
});
//optC.setCellValueFactory(new PropertyValueFactory<>("optR"));
TableColumn<Data2, Double> fifoC = new TableColumn<>("FIFO");
fifoC.setMinWidth(150);
fifoC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getFifoR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));//将表格设置为3位小数
return new ReadOnlyObjectWrapper<>(formattedValue);
});
// fifoC.setCellValueFactory(new PropertyValueFactory<>("fifoR"));
TableColumn<Data2, Double> lruC = new TableColumn<>("LRU");
lruC.setMinWidth(150);
lruC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getLruR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));
return new ReadOnlyObjectWrapper<>(formattedValue);
});
// lruC.setCellValueFactory(new PropertyValueFactory<>("lruR"));
// 将表格列添加到表格视图中
tbv2.getColumns().addAll(pageNumCol, optC, fifoC, lruC);
}
}
总体而言,设计一个JavaFx表格需要以下几个步骤:
- 分析GUI需要哪些组件,面板要怎么排放。
- 设计所要输出的表格TableView中的表头TableColumn以及类型(Data)
- 将组件放入面板(Pane),再将面板添加到(Stage)
- 设计事件处理setOnAction,利用ObservableList将数据以Data的形式一行行进行保存,再由TableView.setItems()方法输入到表格里
效果如图: