Main.java
Код: Выделить всё
package application;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
public class Main extends Application {
public final Counter counter = new Counter();
public final SimpleStringProperty labelText = new SimpleStringProperty();
public Label countLabel = new Label("default text");
public void initialize() {
labelText.addListener((observable, oldValue, newValue) -> {
System.out.println("Changed: " + oldValue + " -> " + newValue);
});
countLabel.textProperty().set("test");
counter.getProperty().addListener((observable, oldValue, newValue) -> {
labelText.set("Click! " + newValue + " total clicks!");
});
countLabel.textProperty().bind(labelText);
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("views/Main.fxml"));
StackPane root = loader.load();
root.getChildren().add(countLabel);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Обновление примерно через 12 часов:
Итак, несмотря на то, что этот вопрос закрыто, я воспользовался советами всех и везде, где только мог найти, сократил свой код до настолько простого и пустого, насколько я мог себе представить, и решил обновить это на случай, если кто-нибудь сможет точно указать, где я Я делаю что-то не так.
Я перенес все из основного класса в отдельный класс MainUI, хотя у меня такое ощущение, что я все еще делаю что-то не так.
Класс приложения
Код: Выделить всё
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("views/Main.fxml"));
StackPane root = loader.load();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Код: Выделить всё
Код: Выделить всё
package application;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.Label;
public class MainUI {
protected final Counter counter = new Counter();
public StringProperty labelText = new SimpleStringProperty();
@FXML
public Label countLabel;
public void initialize() {
countLabel = new Label();
labelText.set("Start clicking");
counter.getProperty().addListener((observable, oldValue, newValue) -> {
labelText.set("Click! " + newValue + " total clicks!");
System.out.println(labelText.get());
});
}
}
Код: Выделить всё
Код: Выделить всё
package application;
public class Click extends MainUI {
public void test() {
System.out.println("Testing, 1, 2, 3...");
}
public void click() {
counter.addToCounter(1)
}
}
Код: Выделить всё
Код: Выделить всё
package application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
public class Counter {
private final IntegerProperty count = new SimpleIntegerProperty(0);
public IntegerProperty getProperty() {
return count;
}
public void addToCounter(int addNum) {
count.set(count.getValue() + addNum);
}
public void setCounter(int setNum) {
this.count.set(setNum);
}
}
Код: Выделить всё
text="${controller.labelText.value}"
Подробнее здесь: https://stackoverflow.com/questions/789 ... -duplicate