所以我有一个 BorderPane
,它有一个设置在中心的 TextArea
。它显示有关当年销售额的信息:
public static BorderPane addTransactionPanel() {
BorderPane pane = new BorderPane();
pane.setPrefSize(500, 320);
final String[] monthName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
final TextArea middle = new TextArea();
middle.setPrefRowCount(1000);
middle.setEditable(false);
pane.setBottom(addBox);
pane.setCenter(middle);
for (int i = 2; i < li.get(index).getIncomeBook().getExpense().size(); i++) {
if (li.get(index).getIncomeBook().getExpense().get(i).getDate().getYear() == year) {
middle.appendText("New expense of: $" + moneyDisplay(li.get(index).getIncomeBook().getExpense().get(i).getValue()) + " on " + monthName[li.get(index).getIncomeBook().getExpense().get(i).getDate().getMonth() - 1] + "\n");
}
}
return pane;
}
这会比较 IncomeBook 支出的内容(及其花费的年份),如果与当前年份匹配,则会将文本附加到 TextArea
上。但现在,当我更改年份(这是一个公共(public)变量)时,我希望它更新。我正在尝试创建另一种方法:
public void updateTransactionPanel(BorderPane transactionPanel){
// I want to clear transactionPanel.middle and write in it again.
// Can I access the "middle" Node from another method?
}
最佳答案
从另一个方法访问transactionPanel的“中间”节点,清除它并重新写入。
public void updateTransactionPanel(BorderPane transactionPanel){
TextArea middle = (TextArea) transactionPanel.getCenter();
middle.setText("Updated transaction panel text");
}
关于JavaFX:我可以访问它包含的节点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393216/