gpt4 book ai didi

JavaFX - 将节点添加到父级时的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 12:28:16 25 4
gpt4 key购买 nike

我创建了一个简单的应用程序来模拟使用 JAVAFX 的动态节点创建。该应用程序能够在用户需要时通过单击“新建”按钮来创建新窗口。用户可以通过单击“添加任务”按钮,然后单击对话框窗口上的“添加”按钮,向窗口添加一个名为 TitledPane 的新节点。

我想要修复一个意外的行为。该应用程序仅将新节点(在本例中为TitledPane)添加到最后创建的窗口。并且前一个窗口上的所有节点都将消失。

您可以观看以下视频以更好地理解我的意思。

视频

https://youtu.be/eaWmu3zuuhE

<小时/>

NETBEANS 项目

以防万一你想玩一下。

https://drive.google.com/file/d/0B4Sbb8Ym-lcZLUIyWHV5ZXRSZE0/view?usp=sharing

<小时/>

代码:

TasksList.java

package taskslist;


import javafx.application.Application;
import javafx.scene.Parent;
import javafx.stage.Stage;

public class TasksList extends Application {

DisplayWhich display = new DisplayWhich();
Stage primaryStage;
Parent startWindow;

@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
initStart();
}

private void initStart(){
display.showDialogWindow();
}

public static void main(String[] args) {
launch(args);
}

}

TheList.java

package taskslist.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import taskslist.DisplayWhich;

public class TheList extends BorderPane {
public static VBox listWrapper;
public static ScrollPane listScroller;
public ObservableList<TitledPane> tasks;
private List<String> titles = new ArrayList<>();

public TheList(){
tasks = FXCollections.observableArrayList();
listWrapper = new VBox(5);
listScroller = new ScrollPane(listWrapper);
}

public void setTitles(String... title){
titles = Arrays.asList(title);
}

public List<String> getTitles(){
return titles;
}

public void loadSavedList(){
for(int i=0; i<getTitles().size();i++){
String ttlString = getTitles().get(i);
this.createTask(ttlString);
}
// Display Tasks
listWrapper.getChildren().addAll(this.tasks);
}

// Dialong for adding a new task and also editing a task
private void addTaskDialog(){
GridPane container = new GridPane();
Scene scene = new Scene(container, 150, 50);
Stage addNewTask = new Stage();
addNewTask.initModality(Modality.APPLICATION_MODAL);
addNewTask.setTitle("Add Task");

TextField title = new TextField();

Button confirm = new Button("Add");

// Create Task
confirm.setOnAction((ev) -> {
String ttlString = title.getText();
this.createTask(ttlString);
listWrapper.getChildren().clear();
listWrapper.getChildren().addAll(this.tasks);
addNewTask.close();
});

container.add(title, 0, 1);
container.add(confirm, 0, 5);

addNewTask.setScene(scene);
addNewTask.showAndWait();
}


// Assemble all this.tasks list components
public void render(){
setCenter(listScroller);
loadSavedList();
Button newProject = new Button("New");
Button addTask = new Button("Add Task");

BorderPane listBottom = new BorderPane();
HBox bottomLeft = new HBox();
bottomLeft.getChildren().add(newProject);
listBottom.setLeft(bottomLeft);
HBox bottomRight = new HBox();
bottomRight.getChildren().add(addTask);
listBottom.setRight(bottomRight);

newProject.setOnAction((evt) -> {
DisplayWhich display = new DisplayWhich();
display.showDialogWindow();
});

addTask.setOnAction((e) -> {
addTaskDialog();
});

setBottom(listBottom);
}

// Cteate task from strings
private void createTask(String... strings){
String taskTitle = strings.length > 0 ? strings[0] : "";
TitledPane task = new TitledPane();
task.setPrefWidth(647);
task.setExpanded(false);
task.setText(taskTitle);
this.tasks.add(task);
}
}

NewDialog.java

package taskslist.view;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import taskslist.DisplayWhich;

public class NewDialog {
DisplayWhich display = new DisplayWhich();
Stage stage = new Stage();
Parent startWindow = new AnchorPane();

@FXML
private Button cancelNew;
@FXML
private Button confirmCreation;
/**
* Initializes the controller class.
*/
@FXML
private void initialize() {
}

@FXML
private void cancelNewCreation(ActionEvent event) {
((Stage)cancelNew.getScene().getWindow()).close();
}

@FXML
private void confirmCreateNew(ActionEvent event) {
((Stage)confirmCreation.getScene().getWindow()).close();
TheList wrap = new TheList();
TheWindow window = new TheWindow();
window.makeWindow(wrap);
wrap.setTitles("one", "two", "three", "four");
wrap.render();
}
}

DisplayWhich.java

package taskslist;

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import taskslist.view.TheList;

public class DisplayWhich {
Stage stage = new Stage();
Parent startWindow = new AnchorPane();

public DisplayWhich(){}

public Stage showDialogWindow(){
try {
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Create New Project");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/taskslist/view/newDialog.fxml"));
startWindow = loader.load();
Scene scene = new Scene(startWindow);
stage.setScene(scene);
stage.setOnCloseRequest((event) -> {
System.out.println("test");
});
stage.showAndWait();
} catch (IOException ex) {
ex.printStackTrace();
}
return stage;
}
}

TheWindow.java

package taskslist.view;

import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TheWindow {
public TheWindow(){}

public void makeWindow(BorderPane group) {
Stage mainWindow = new Stage();
Scene scene = new Scene(group, 650, 550);
mainWindow.setScene(scene);
mainWindow.setTitle("Task List");
mainWindow.centerOnScreen();
mainWindow.show();
}
}

为什么会发生这种奇怪的行为以及如何修复它,以便它只将新节点添加到单击的“添加任务”按钮所在的同一窗口?

最佳答案

这些字段不应是静态的:

public static VBox listWrapper;
public static ScrollPane listScroller;

关于JavaFX - 将节点添加到父级时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45389629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com