题
如何在Java 8中创建合适的后台加载程序?条件:
目的是例如使重新加载请求被接受,但数据库不被请求所淹没。
MCVE
这是MCVE。它由一个后台任务组成,该任务通过简单地调用Thread.sleep 2秒钟来模拟加载。每秒安排一次任务,这自然会导致后台加载任务重叠,因此应避免。
public class LoadInBackgroundExample {
/**
* A simple background task which should perform the data loading operation. In this minimal example it simply invokes Thread.sleep
*/
public static class BackgroundTask implements Runnable {
private int id;
public BackgroundTask(int id) {
this.id = id;
}
/**
* Sleep for a given amount of time to simulate loading.
*/
@Override
public void run() {
try {
System.out.println("Start #" + id + …Run Code Online (Sandbox Code Playgroud) 我想为类是超类的子类创建一个注册表.这些类存储在充当注册表的映射中.根据键,从注册表中选择一个类,并通过反射创建该类的实例.
我想根据超类的构造函数(带有1个参数)来实例化一个类.它只有在我在子类中声明构造函数时才有效.
有没有办法使用超类的构造函数实例化类?有没有办法使代码类型安全?
示例代码:
public class ReflectionTest {
/**
* Base class with no-args constructor and another constructor with 1 parameter
*/
public static class BaseClass {
Object object;
public BaseClass() {
System.out.println("Constructor with no args");
}
public BaseClass( Object object) {
this.object = object;
System.out.println("Constructor with parameter= " + object);
}
public String toString() {
return "Object = " + object;
}
}
/**
* Subclass with 1 parameter constructor
*/
public static class SubClass1 extends BaseClass {
public …Run Code Online (Sandbox Code Playgroud) 有没有办法在类似于ScrollPane的TableView中更改ScrollBar的策略?我只看到TableView的VirtualFlow计算可见性,但不存在手动干扰的可能性.
我需要垂直滚动条始终可见,水平从不.更改条形的可见状态不起作用.
例:
import java.time.LocalDate;
import java.time.Month;
import java.util.Set;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import com.sun.javafx.scene.control.skin.VirtualFlow;
public class ScrollBarInTableViewDemo extends Application {
private TableView<Data> table1 = new TableView<>(); // table with scrollbars
private TableView<Data> table2 = new TableView<>(); // table without scrollbars
private final ObservableList<Data> data =
FXCollections.observableArrayList(
new Data( LocalDate.of(2015, Month.JANUARY, 10), …Run Code Online (Sandbox Code Playgroud) 我在JavaFX中创建一个图形,它应该通过有向边连接.最好的是双三次曲线.有谁知道如何添加箭头?
当然,箭头应该根据曲线的末端旋转.
这是一个没有箭头的简单示例:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class BasicConnection extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
// bending curve
Rectangle srcRect1 = new Rectangle(100,100,50,50);
Rectangle dstRect1 = new Rectangle(300,300,50,50);
CubicCurve curve1 = new CubicCurve( 125, 150, 125, 200, 325, 200, 325, 300);
curve1.setStroke(Color.BLACK);
curve1.setStrokeWidth(1);
curve1.setFill( null);
root.getChildren().addAll( srcRect1, dstRect1, curve1);
// steep curve
Rectangle …Run Code Online (Sandbox Code Playgroud) 我想创建一个具有以下功能的表:
下面是实现这些功能的代码.值应该在焦点丢失时提交.问题:他们没有承诺.触发焦点更改事件,根据控制台输出值将是正确的,但最后表格单元格中的值是旧值.
有谁知道如何防止这种情况,你如何获得当前的EditingCell对象,以便我可以手动调用提交?毕竟,应该调用某种验证器,如果值不正确,则会阻止更改焦点.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewInlineEditDemo extends Application {
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用zoomable/pannable画布创建一个应用程序.
特点:
只要您开始按比例缩放,缩放点就会起作用.将鼠标放在网格点上并滚动鼠标滚轮.枢轴点将保持开始缩放的位置.
问题:
放大时,然后将鼠标移动到另一个点并再次缩放,然后移动枢轴点并在初始鼠标位置不再进行缩放.
示例:
这是代码:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
/**
* The canvas which holds all of the nodes of the application.
*/
class PannableCanvas extends Pane {
Scale scaleTransform;
public PannableCanvas() {
setPrefSize(600, 600);
setStyle("-fx-background-color: lightgrey; -fx-border-color: blue;");
// add scale transform
scaleTransform …Run Code Online (Sandbox Code Playgroud) 题
有没有办法禁用谷歌地图瓷砖的褪色?或者有没有办法检测地图是否完全渲染?
问题
我想在地图完全加载(和渲染)时获取一个事件并截取屏幕截图.我按照几个答案的建议尝试了这些事件
google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
// screenshot
});
google.maps.event.addListener(map, 'idle', function(){
// screenshot
});
window.onload = function(e){
// screenshot
};
Run Code Online (Sandbox Code Playgroud)
但即使在加载并且上述事件被触发后,瓷砖仍在褪色.
它看起来像这样:左边是谷歌地图,右边是在事件发生后拍摄的自动截图:
码
代码在html和JavaFX中
demo.html
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#mapcanvas {
/* height: 4000px; we need the value from java */
width: 100%
}
</style>
</head>
<body>
<div id="mapcanvas"></div>
<script type="text/javascript">
console.log("Loading map tiles");
// set map canvas height
document.getElementById('mapcanvas').style.height = window.mapCanvasHeight;
document.map = new …Run Code Online (Sandbox Code Playgroud) 问题
我尝试使用JavaFX创建一个3d图表,但它似乎比人们期望的更难.
我目前的做法是创建一个TriangleMesh,但这是相当间接的.我想要做的就是为List<Point3D>图表提供一个图表,然后图表应该呈现为一个表面.
然而,即使是一个包含5个数据点的简单金字塔也相当复杂:
float h = 200; // Height
float s = 200; // Side
TriangleMesh pyramidMesh = new TriangleMesh();
pyramidMesh.getTexCoords().addAll(0,0);
pyramidMesh.getPoints().addAll(
0, 0, 0, // Point 0 - Top
0, h, -s/2, // Point 1 - Front
-s/2, h, 0, // Point 2 - Left
s/2, h, 0, // Point 3 - Back
0, h, s/2 // Point 4 - Right
);
pyramidMesh.getFaces().addAll(
0,0, 2,0, 1,0, // Front left face
0,0, 1,0, 3,0, // Front right …Run Code Online (Sandbox Code Playgroud) 我想在网页上嵌入一个或多个JavaFX应用程序.你是怎样做的?
Oracle网站上有一些零碎的内容,但没有完整的例子.
有浏览器中的部署教程,包装基础知识教程等.有人提到Ant任务,有些没有.
所以在我阅读之后仍然有很多问题.我喜欢Ant吗?我需要创建一个applet吗?等等
所有我想看到的是一个最小和完整的"Hello World"示例,以便了解它是如何工作的.即使在StackOverflow这里也只是对同一个问题的答案的点点滴滴,所以这并没有真正帮助.
我昨天提出了这个问题,但删除了它,并认为我会尝试自己.事实证明,当你知道陷阱时,这很容易.因为这已经在这里被问到了,我想我会在答案中分享我的最小和完整的例子.
使用JavaFX示例只需几分钟即可为此html页面创建代码:

问题
您可以向检测鼠标在其上移动的节点添加事件侦听器.如果在移动节点之前按下鼠标按钮,则不起作用.
题
有人知道如何在按下按钮时检测鼠标移动吗?到目前为止,我只是通过使用MOUSE_DRAGGED事件找到了解决方案,然后使用getPickResult()而不是使用getSource()并评估PickResult数据.
这是代码,包括Uluk的解决方案.旧的和新的解决方案可通过useNewVersion(Uluk的版本)boolean切换:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
boolean useNewVersion= true;
int rows = 10;
int columns = 20;
double width = 1024;
double height = 768;
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
// create grid
Grid grid = new Grid( columns, rows, width, …Run Code Online (Sandbox Code Playgroud)