我一直在教自己JavaFX,我正在开发一个简单的文件传输计算器。它可以计算文件大小,传输速度和传输时间。
我希望能够将当前选项从文件(例如,兆字节,千兆字节等)加载到几个JavaFX ChoiceBox。我需要:
我需要了解它们如何转换,因为用户可能想知道通过42 KB / s连接传输1 TB数据需要多少时间。
我想过使用一个文本文件,但是以一种容易被文件读取的方式格式化会很麻烦,并且很难自动化写入过程。我想过使用XML来做这件事,但我不知道如何将它用于此目的,或者它是否是一个好主意。 那么,加载选项和每个选项的最佳方法是什么?以及如何使用它?
package application;
import java.io.ObjectInputStream.GetField;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
public class MainInterfaceController implements Initializable {
/*
* Declarations of the interface interactive bits FS - File Size TS -
* Transfer Speed TT - Transfer Time
*/
@FXML
private TextField FSTextField;
@FXML
private ChoiceBox<String> FSChoiceBox;
@FXML
private RadioButton FSRadioButton;
@FXML
private TextField TSTextField;
@FXML
private ChoiceBox<String> TSChoiceBox;
@FXML
private RadioButton TSRadioButton;
@FXML
private TextField TTTextField;
@FXML
private ChoiceBox<String> TTChoiceBox;
@FXML
private RadioButton TTRadioButton;
@FXML
private Button AboutButton;
@FXML
private Button CalculateButton;
// Variables & Data Sets
//These should be initialized in the init method so they can be populated with info from a file(?)
private ObservableList<String> fileSizes = FXCollections.observableArrayList("KB", "MB", "GB", "TB", "PB");
private ObservableList<String> transferSpeeds = FXCollections.observableArrayList("KB/s", "MB/s", "GB/s", "TB/s", "PB/s");
private ObservableList<String> timeUnits = FXCollections.observableArrayList("Seconds", "Minutes", "Hours");
@Override
public void initialize(URL location, ResourceBundle resources) {
populateChoiceBoxes();
setRadioButtonsActions();
CalculateButton.setOnAction(e -> calculateValues());
}
private void populateChoiceBoxes() {
FSChoiceBox.setItems(fileSizes);
TSChoiceBox.setItems(transferSpeeds);
TTChoiceBox.setItems(timeUnits);
}
private void setRadioButtonsActions() {
FSRadioButton.setOnAction(e -> clearRadioButtons(e));
TSRadioButton.setOnAction(e -> clearRadioButtons(e));
TTRadioButton.setOnAction(e -> clearRadioButtons(e));
}
private void clearRadioButtons(ActionEvent e) {
if (e.getSource() == FSRadioButton) {
TSRadioButton.setSelected(false);
TTRadioButton.setSelected(false);
}
if (e.getSource() == TSRadioButton) {
FSRadioButton.setSelected(false);
TTRadioButton.setSelected(false);
}
if (e.getSource() == TTRadioButton) {
FSRadioButton.setSelected(false);
TSRadioButton.setSelected(false);
}
}
private void calculateValues() {
if (FSRadioButton.isSelected()) {
try {
String TSText = TSTextField.getText();
double transferSpeed = Double.parseDouble(TSText);
String TTText = TTTextField.getText();
double transferTime = Double.parseDouble(TTText);
} catch (NumberFormatException e) {
Alert alert = new Alert(AlertType.ERROR, "The transfer speed/transfer time must be a number!");
alert.showAndWait();
}
}
}
private void convertSpeed(double transferSpeed, ChoiceBox<String> speedUnit) {
}
}
到目前为止,我有上面的代码。正如您在开始时所看到的,我声明了ObservableLists。这就是我想要自动化的东西以及最终的转换方法。这将使程序更灵活,更容易更新。
答案 0 :(得分:3)
在Java中执行此操作的标准方法是使用.properties
文件,该文件与类java.util.Properties
有一种标准格式:
key=value
<entry key="key">value</entry>
Properties
类具有标准方法,用于加载属性并从Properties
对象访问它们。
引用J2SE Javadoc示例可能是:
Properties properties = new Properties();
try(InputStream is = this.getClass().getResourceAsStream("my.properties")) {
properties.load(is);
}
String value = properties.getProperty("key");
答案 1 :(得分:0)