所以也许我没有使用它打算如何使用的方法,而是我在 youtube 用户 thenewboston 观看的视频中完全使用它,并且效果很好。帮助将不胜感激
package checkers;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import javafx.scene.Scene ;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.application.*;
import javafx.stage.*;
public class Checkers extends Application {
Stage window;
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Title");
HBox layout = new HBox();
Button startButton = new Button("Start");
Button quitButton = new Button("Quit");
layout.getChildren().addAll(startButton, quitButton);
Scene startScene = new Scene(layout, 400, 300);
window.setScene(startScene);
window.show();
}
public static void main(String[] args) {
launch(args);
} …Run Code Online (Sandbox Code Playgroud) 我在课堂上有一个项目,我需要用简单的三个圆圈来显示交通灯。我从黄色的开始,然后尝试在其他一些随机的地方添加一个红色的,只是为了看看我是否可以做到,但是黄色的是唯一显示的。我不知道红色的是否在黄色的下面,但无论如何,为什么没有显示红色圆圈对我来说没有多大意义。
package tryingGraphicsStuff;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.scene.control.*;
public class TryingGraphicsStuff extends Application{
@Override
public void start(Stage stage) throws Exception {
// create circle
Circle circle = new Circle();
circle.setCenterX(150);
circle.setCenterY(150);
circle.setRadius(50);
circle.setFill(Color.RED);
// place on pane
StackPane p = new StackPane();
p.getChildren().add(circle);
// ensure it stays centered if window resized
//circle.centerXProperty().bind(p.widthProperty().divide(2));
//circle.centerYProperty().bind(p.heightProperty().divide(2));
Circle circleTwo = new Circle();
circleTwo.setCenterX(400);
circleTwo.setCenterY(400);
circleTwo.setRadius(50);
circleTwo.setFill(Color.YELLOW);
// place on pane
p.getChildren().add(circleTwo);
// create …Run Code Online (Sandbox Code Playgroud) 我用 JAVAFx 制作了一个图形界面,在 NETBEANS 7.3.1 上,我遇到了问题;堆栈跟踪是
Executing com.javafx.main.Main from C:\Users\Guillaume\Documents\NetBeansProjects4\JavaFXApplication1\dist\run1976733325\JavaFXApplication1.jar using platform C:\Program Files\Java\jdk1.7.0_80/bin/java
Page language not specified.
file:/C:/Users/Guillaume/Documents/NetBeansProjects4/JavaFXApplication1/dist/run1976733325/JavaFXApplication1.jar!/javafxapplication1/Sample.fxml:30
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:510)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:594)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2472)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2177)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
at javafxapplication1.JavaFXApplication1.start(JavaFXApplication1.java:21)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:745)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) …Run Code Online (Sandbox Code Playgroud) 是否可以创建可访问的 JavaFX 对象数组。
目前我有:
@FXML
private CheckBox pc1,pc2,pc4,pc8,pc16,pc32,pc64,pc128,pc256,pc512,pc1024,pc2048;
private final CheckBox[] pcController = {pc1,pc2,pc4,pc8,pc16,pc32,pc64,pc128,
pc256,pc512,pc1024,pc2048};
Run Code Online (Sandbox Code Playgroud)
我正在尝试访问这些对象并根据不同数组中的值翻转复选框,如下所示:
boolean[] bits = getBits();
for(int i =0; i<pcController.length;i++){
pcController[i].setSelected(bits[i]);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Cannot invoke "javafx.scene.control.CheckBox.setSelected(boolean)" because "this.pcController[i]" is null ...
Run Code Online (Sandbox Code Playgroud)
编辑:所以如果我在方法内实例化数组,我可以让系统半工作。
代码:
package learning;
import javafx.application.Application;
import javafx.stage.Stage;
public class One extends Application {
public static void main (String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in Application constructor 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:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application
instance: class learning.One
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:833) Caused by:
java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class learning.One …Run Code Online (Sandbox Code Playgroud) 我有一个单一场景的JavaFX应用程序,它运行一个动画,动画需要在两种状态之间来回切换。
基于“Java 如何编程,11/e,早期对象”中的示例
,我编写了一个控制器,该控制器在初始化方法中创建类似的设置,
以及一个具有布尔值的任务,该任务用布尔值向动画计时器发出何时通过翻转切换状态的信号其值然后睡眠。无论
我做什么,我都会不断收到从该方法抛出的“java.lang.IllegalStateException:任务只能从 FX 应用程序线程使用” 。call()
这是控制器的简化版本:
public class AnimationController {
@FXML public AnimationTimer myAnimationTimerExtention;
private ExecutorService executorService;
public void initialize() {
myAnimationTimerExtention.setState(false);
TimeingTask task = new TimeingTask (Duration.ofSeconds(5));
task .valueProperty().addListener((observableValue, oldValue, newValue) -> {
myAnimationTimerExtention.setState(false);
});
executorService = Executors.newFixedThreadPool(1);
executorService.execute(timeTrafficLightTask);
executorService.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
这是我的任务:
public class TimeingTask extends Task<Boolean> {
private final Duration duration;
public TimeingTask(Duration duration) {
this.duration = duration;
updateValue(true);
}
@Override
protected Boolean call() throws Exception {
while (!isCancelled()) {
updateValue(!getValue()); …Run Code Online (Sandbox Code Playgroud) 请有人帮我澄清这些概念来自c ++,并尝试学习java一段时间,并且当我尝试尝试一些摇摆应用程序时,我只是达到这些条件(前两个)...
如何制作一个触发的自定义事件Stage.setScene()?
在我的代码中,按钮切换场景,效果很好。但是,我想扩展舞台以具有一个额外的事件,当按钮或可能的任何其他元素触发 setScene 时会触发该事件。
例子:
package sample;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
Group g1 = new Group();
Button b1 = new Button("2");
g1.getChildren().setAll(b1);
Scene scene1 = new Scene(g1, 50, 50);
Group g2 = new Group();
Button b2 = new Button("1");
g2.getChildren().setAll(b2);
Scene scene2 = new Scene(g2, 50, 50);
stage.setScene(scene1);
stage.setTitle("JavaFX Application Life Cycle"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写这个多线程程序,但出了问题。我有一个 HelloApplication 类,我在那里创建了两个新线程。
run 方法位于另一个名为 ThreadLocalSession 的类中。在那里,我需要存储两个不同的值,每个线程使用 ThreadLocal 变量存储一个值。这种方法失败了,因为第二个线程覆盖了第一个线程写入的值。
由于这个原因,我尝试打印线程 id,我注意到相同的线程 ID 和线程名称被打印了两次......这怎么可能?
这是 HelloApplication 类:
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
ThreadLocalSession firstUser = new ThreadLocalSession(1);
ThreadLocalSession secondUser = new ThreadLocalSession(2);
new Thread(firstUser).start();
new Thread(secondUser).start();
}
public static void main(String[] args) {
launch();
}
}
Run Code Online (Sandbox Code Playgroud)
这是 ThreadLocal 会话类:
public class ThreadLocalSession implements Runnable{
private static final ThreadLocal<Session> usersession= new ThreadLocal<Session>();
private Integer tr_id;
public ThreadLocalSession(int i) {
this.tr_id =i;
}
@Override …Run Code Online (Sandbox Code Playgroud) 我想要一个Java GUI库,它允许这样的事情:
Window window1 = new Window();
window1.show();
Window window2 = new Window();
window2.show();
...
Run Code Online (Sandbox Code Playgroud)
但是JavaFX窗口需要在其自己的扩展Application的类中启动,并且无法多次实例化。
为什么不能像其他类一样实例化JavaFX窗口?为什么称为应用程序而不是窗口或框架?JavaFX为什么会打破OOP?为什么我们必须使用反射混合的单例?我不需要魔术,我希望在屏幕上出现一个具有OOP行为的窗口对象。
我的&&逻辑运算符将无法工作,并始终返回false并返回第二个条件为true.但它对||非常有效 运营商.我做错了什么?
我尝试使用|| 运算符,它工作正常,但我想使用&&运算符
@FXML
private void loginFunction() throws IOException {
String username = this.username.getText();
String password = this.username.getText();
if (username.equals("username") && password.equals("password")) {
// This Code is used to alert the user of any information in case the username
// and password is all correct
Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
alert2.setHeaderText("Login Successful");
alert2.setTitle("Welcome");
alert2.setContentText("Pleae Wait. You shall be redirected soon.");
alert2.show();
} else {
Alert alert3 = new Alert(Alert.AlertType.WARNING);
alert3.setTitle("Intruder Found");
alert3.setContentText("Either your username or password is incorrect. Please …Run Code Online (Sandbox Code Playgroud) package tic.tac.toe.menu;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TicTacToeMenu extends Application {
@Override
public void start(Stage primaryStage) {
Button start = new Button();
start.setText("How to Play?");
start.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("\n"+"The goal of tic-tac-toe is to get 3 of your pieces in a row vertically, horizontally, or diagonally ");
System.out.println("To play this game click inside a square to put down your piece, you choose to …Run Code Online (Sandbox Code Playgroud)