我正在使用a GridPane
来存储关于城市的信息(在游戏中),但有时我想删除一些行.这是我用于我的课程GridPane
:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.franckyi.kingsim.city.City;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
public class CityGrid extends GridPane {
private List<City> cities;
public CityGrid() {
super();
cities = new ArrayList<City>();
}
public List<City> getCities() {
return cities;
}
public void deleteCity(int row) {
cities.remove(row - 1);
removeNodes(getNodesFromRow(row));
int i = row;
while (!getNodesFromRow(i + 1).isEmpty()) {
moveNodes(i + 1, getNodesFromRow(i + 1));
removeNodes(getNodesFromRow(i + 1));
i++;
}
}
public void …
Run Code Online (Sandbox Code Playgroud) 好吧,标题可能很难理解.我找不到正确的东西.所以,基本上我使用Java 8函数来创建可重试的API.我想要一个简单的接口实现,所以我在of(...)
Retryable接口的每个实现中创建了一个方法,我们可以使用lambda表达式,而不是手动创建一个匿名类.
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public interface Retryable<T, R> extends Function<T, R>{
void retrying(Exception e);
void skipping(Exception e);
int trials();
@Override
default R apply(T t) {
int trial = 0;
while (true) {
trial++;
try {
return action(t);
} catch (Exception e) {
if (trial < trials()) {
retrying(e);
} else {
skipping(e);
return null;
}
}
}
}
R action(T input) throws Exception;
interface RunnableRetryable extends Retryable<Void, Void> {
static RunnableRetryable of(Consumer<Exception> retrying, Consumer<Exception> …
Run Code Online (Sandbox Code Playgroud) updateProgress(workDone, max)
我的程序使用 JavaFX 中的任务来下载和解压缩文件,并通过使用方法和方法在屏幕上显示进度progressProperty().bind(observable)
。它适用于下载:
package com.franckyi.lan.installer;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import javafx.concurrent.Task;
public class DownloadTask extends Task<Void> {
private String file;
private String url;
public DownloadTask(String dir, String fileurl) {
file = dir;
url = fileurl;
}
@Override
protected Void call() throws Exception {
URLConnection connection = new URL(url).openConnection();
long fileLength = connection.getContentLengthLong();
try (InputStream is = connection.getInputStream();
OutputStream os = Files.newOutputStream(Paths.get(file))) {
long nread = 0L;
byte[] buf …
Run Code Online (Sandbox Code Playgroud)