gpt4 book ai didi

java - 从 JavaFX GridPane 中的 TextArea 获取文本

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

我有一个项目,其中我询问用户他们使用什么类型的电子设备,以告诉他们需要多少太阳能电池板和电池。

为了收集信息,我使用具有三列的 gridPane,其中有 TextArea 和可变数量的行。我需要收集 gridPane 的第二行和第三行中的信息。

我使用了我在此处提出的另一个问题中找到的以下方法。

private void calculer(ActionEvent event)
{
double ampApp = 0;
double heureApp = 0;
double wattTotal = 0;
double nbJourAuto;
double heureSoleil;
int nbPanneau;
int nbBatterie;
String stringTemp;

for(int i =1; i < rangee; i++)
{
stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).toString();
ampApp += parseDouble(stringTemp);
}

for(int i =1; i < rangee; i++)
{
stringTemp = getNodeByRowColumnIndex(i, 2, gridApp).toString();
heureApp += parseDouble(stringTemp);
}

(这只是该方法的第一部分,我知道缺少“}”)

这是 getNodeByRowColumnIndex 代码:

public Node getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane) {
Node result = null;
ObservableList<Node> childrens = gridPane.getChildren();
for(Node node : childrens)
{
if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column)
{
result = node;
break;
}
}
return result;
}

我尝试了这个方法,但不断收到 java.lang.NullPointerException

有人知道为什么以及如何解决这个问题吗?

编辑:

根据要求,这是堆栈跟踪

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at systemesolaire.FXMLDocumentController.getNodeByRowColumnIndex(FXMLDocumentController.java:111)
at systemesolaire.FXMLDocumentController.calculer(FXMLDocumentController.java:81)
... 58 more

这是我初始化我的 child 的方法(我使用了 SceneBuilder,所以这里是 FXML 文件)

<content>
<GridPane fx:id="gridApp" prefHeight="141.0" prefWidth="452.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="226.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="146.0" minWidth="10.0" prefWidth="123.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="113.0" minWidth="10.0" prefWidth="103.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
<RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" />
</rowConstraints>
<children>
<Label text=" Nom de l'appareil">
<font>
<Font name="Anke" size="18.0" />
</font>
</Label>
<Label text="Ampérage" GridPane.columnIndex="1">
<font>
<Font name="Anke" size="14.0" />
</font>
</Label>
<Label text="Heures utilisation" GridPane.columnIndex="2">
<font>
<Font name="Anke" size="13.0" />
</font>
</Label>
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="2" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
</GridPane>
</content>

这是我如何向 gridPane 添加行

 @FXML
private void ajouteRangee(ActionEvent event)
{

TextArea nom = new TextArea();
TextArea amp = new TextArea();
TextArea heure = new TextArea();



nom.setPromptText("Nom");
amp.setPromptText("Amp");
heure.setPromptText("heure");


gridApp.addRow(rangee, nom, amp, heure);
gridApp.layout();
rangee++;

}

最佳答案

我在老师的帮助下得到了答案,如果有人有同样的问题,这就是我的解决方法。

NullPointerException 是针对 getColumnIndex 出现的,该解决方案绕过了该问题,但我不知道为什么我首先会遇到此异常。

由于行数和列数已知,因此无需在每个节点中搜索正确的节点。此外,由于每行的子项数量相同,因此有一种逻辑方法可以获取特定的子项。方法如下:

 public TextArea getNodeByRowColumnIndex( final int row, final int column, GridPane gridPane) 
{

ObservableList<Node> childrens = gridPane.getChildren();
TextArea result = (TextArea) childrens.get((row*3)+column);

return result;
}

ObservableList 从左到右逐行获取子项,因此您可以使用以下公式获取列表中的特定对象 (((rowIndex + 1) * 每行的对象数量) + 您所在的列对象)

然后你可以通过以下方式获取 TextArea 文本:

 stringTemp = getNodeByRowColumnIndex( i, 1, gridApp).getText();

关于java - 从 JavaFX GridPane 中的 TextArea 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39671692/

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