问题列表 - 第318032页

当我创建 Robot 对象时,为什么 JavaFX 会引发 IllegalStateException?

我正在创建一个应用程序,我想要一个功能,当用户按下 时E,它将把鼠标指针设置到屏幕的中心(使用该moveMouse()方法)。然而,它只是给了我一个IllegalStateException.

我尝试过使用 aGlassRobot代替(来自这个问题并接受了答案),但这只给了我一个IllegalAccessException代替。我究竟做错了什么?我需要添加一些东西吗?

代码和错误

IllegalStateException

注:底部附近有一条评论

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082)
Caused by: java.lang.ExceptionInInitializerError
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    ... 5 more
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
    at javafx.graphics/com.sun.glass.ui.Application.checkEventThread(Application.java:447)
    at javafx.graphics/javafx.scene.robot.Robot.<init>(Robot.java:70)
    at com.galactify.tiles/com.galactify.tiles.HelloApplication.<clinit>(HelloApplication.java:37) // This is where …
Run Code Online (Sandbox Code Playgroud)

java mouse javafx mouseevent

3
推荐指数
1
解决办法
58
查看次数

分块列表后未找到子范围的 C++ 返回方法

我遇到了这种奇怪的行为,如果您创建一个列表的分块视图(由 2 个元素组成),然后尝试在范围 for 循环中打印正面和背面,则前面的方法将起作用,但后面的方法将不起作用t。它将产生一个编译器错误,指出实例化 .back() 方法需要双向迭代器。我错过了什么吗?

重现错误的代码

#include <iostream>
#include <list>
#include <ranges>
#include <vector>

int main() {
  std::list<int> nums_list = {0, 1, 2, 3, 4, 5};

  auto rng = nums_list | std::views::chunk(2);
  for (auto pair : rng) {
    std::cout << pair.front(); // Front will work normally
    std::cout << pair.back();  // No matching member function for 'back'.
  }
}

Run Code Online (Sandbox Code Playgroud)

我也用矢量尝试过,它按预期工作。

c++ std-ranges c++23

4
推荐指数
1
解决办法
159
查看次数

如何编写一个以一个闭包作为参数并以另一个闭包作为参数的函数?

我想编写一个将一个闭包作为参数的函数,该函数将另一个闭包作为参数,如下所示:

fn test<F: Fn(G), G: Fn()>(f: F) {
    let print = || {
        println!("hello");
    };
    f(print);
}

fn main() {
    test(|print| {
        print();
    })
}
Run Code Online (Sandbox Code Playgroud)

但这给出了这个错误:

error[E0308]: mismatched types
 --> src/main.rs:5:7
  |
1 | fn test<F: Fn(G), G: Fn()>(f: F) {
  |                   - expected this type parameter
2 |     let print = || {
  |                 -- the found closure
...
5 |     f(print);
  |     - ^^^^^ expected type parameter `G`, found closure
  |     |
  |     arguments to this function are …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
85
查看次数

PHP 空数组定义,以逗号开头或结尾

所以我在我的办公室的 PHP8 代码上遇到了这些(简化的):

[, $x] = $y;
Run Code Online (Sandbox Code Playgroud)

我也看到了另一种方式

[$x, ] = $y;
Run Code Online (Sandbox Code Playgroud)

这些会做什么呢?如果我在沙箱中执行此操作并事先为 $y 分配随机值,则 $x 不会得到任何结果。我知道这不是坏代码,因为 PHPStorm 验证了它。

我尝试过搜索它,但是包括 Stackoverflow 在内的每个搜索引擎都会忽略逗号或 [],所以我无法得到答案。

php arrays definition

3
推荐指数
1
解决办法
58
查看次数

低争用锁

Rust 中是否有针对低争用条件进行优化的锁?

每个实现都需要权衡,因此标准库可能对存在高争用的情况有所帮助。但我想要一个适合极快读取的锁,假设几乎没有争用。

rust

0
推荐指数
1
解决办法
100
查看次数

为什么 noexcept(true) 在 noexcept 工作的地方会失败?

我有以下代码:

struct S {
    // conditional noexcept specification which depends on noexceptness of foo()
    void bar() noexcept(noexcept(foo()));
    // conditional noexcept specification (true)
    void foo() noexcept(true);
};
Run Code Online (Sandbox Code Playgroud)

Clang 拒绝此代码,但 GCC 允许它(https://godbolt.org/z/nxqa5Tr1x):

<source>:2:34: error: exception specification is not available until end of class definition
    2 |     void bar() noexcept(noexcept(foo()));
      | 
Run Code Online (Sandbox Code Playgroud)

替换noexcept(true)为noexcept两个编译器都允许

问题

  1. 为什么会noexcept(true)失败但是noexcept成功?
  2. 哪种编译器符合该标准(如果有)?

注意:这个例子显然是最小的。我的实际用例是,我有一个clear()成员函数,仅在自定义容器中为noexceptif clear_impl()is 。noexcept

c++ clang language-lawyer compiler-bug noexcept

5
推荐指数
1
解决办法
124
查看次数

限制的正式定义未能考虑有效的情况

restirct根据C 标准中的正式定义,以下合理的程序似乎具有未定义的行为:

void positive_intcpy(int * restrict q, const int * restrict p, size_t n) {
  int *qBgn = q;
  const int *pEnd = p + n;
  // sequence point S
  while (p != pEnd && *p>0) *q++ = *p++;
  if (q != qBgn) fprintf(stderr,"Debug: %d.\n",*(q-1)); // undefined behavior!?
}
int main(void) {
  int a[6] = {4,3,2,1,0,-1};
  int b[3];
  positive_intcpy(b,a,3);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该函数将整数从一个数组复制到另一个数组,只要整数为正数。该fprintf调用显示最后复制的正整数(如果有)。p和之间永远不会有任何混叠q。

这真的是UB,还是我的推理错误?

这个问题涉及C99标准的6.7.3.1节。C23最新草案中相关文本没有变化。

q-1我们讨论的是上面标记的指针表达式。是否基于 指定的受限指针对象p? …

c language-lawyer restrict-qualifier

15
推荐指数
2
解决办法
358
查看次数

变异/过滤与另一个数据框同名的列

我想做这样的事情(一个愚蠢的代表):

iris %>%
    mutate(mtcars = Sepal.Length * 7) %>%
    filter(mtcars < max(mtcars$mpg))
Run Code Online (Sandbox Code Playgroud)

其中第一个mtcars引用 中的列iris,而第二个mtcars引用外部 data.frame。

我意识到我可以预先计算max(mtcars$mpg)或简单地重新标记 data.frame,但我想知道是否有一种方法可以在原位显式区分两者?例如:

iris %>%
    mutate(mtcars = Sepal.Length * 7) %>%
    filter(`mtcars` < max(EXTERNAL::mtcars$mpg))
#              ^                        ^
#            column               Not a column
Run Code Online (Sandbox Code Playgroud)

r calculated-columns tidyverse

3
推荐指数
3
解决办法
136
查看次数

如何定义 Parquet 和/或 Arrow 模式?

是否有一种与语言无关的方式以类似于 Avro 的方式表示 Parquet 或 Arrow 模式?例如,Avro 架构可能如下所示:

\n
{\n     "type": "record",\n     "namespace": "com.example",\n     "name": "FullName",\n     "fields": [\n       { "name": "first", "type": "string" },\n       { "name": "last", "type": "string" }\n     ]\n} \n
Run Code Online (Sandbox Code Playgroud)\n

Parquet 和/或 Arrow 有类似的东西吗?

\n

(我\xe2\x80\x99d将其写为2个单独的问题,但Stack Overflow将它们标记为骗局,抱歉)。

\n

parquet apache-arrow

6
推荐指数
1
解决办法
107
查看次数

如何在代码中在矩形前面渲染线条?

我是 javafx 新手,在制作 GridTiles16 网格时遇到问题。这就是我制作tiles16网格的方法:

private static void makeGrid(Group root, int gridSize, ArrayList<String> grid, GridTile16[][] gridTiles, Scene scene) {
    for (int i = 0; i < grid.size(); i++) {
        for (int j = 0; j < grid.get(i).length(); j++) {
            GridTile16 newGridTile = new GridTile16(i * gridSize, j * gridSize, gridSize, grid.get(i).charAt(j));
            root.getChildren().add(newGridTile);
            gridTiles[i][j] = newGridTile;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

GridTiles 类:

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class GridTile extends Group {

    protected int x;
    protected int y;
    protected int …
Run Code Online (Sandbox Code Playgroud)

java javafx

4
推荐指数
1
解决办法
113
查看次数