我正在使用JavaFX SceneBuilder做日历。
我在我的Main类上声明了我的标签:
public class Main extends Application {
//Calendar used
public Calendar cal;
//variables for get the number of week+current year
public static int weeks, year;
@FXML
//Text areas for the 4 weeks column
public TextArea week1,week2,week3,week4;
@FXML
//4 control Buttons
public Button prev,next,next2,prev2, start;
@FXML
//Labels
public Label lab1;
我试图设置文本的方法是这样的:
public void ClickStart (ActionEvent event){
lab1=new Label();
lab1.setText("hola");
cal=Calendar.getInstance();
year = cal.get(Calendar.YEAR);
weeks= cal.get(Calendar.WEEK_OF_YEAR);
week1.setText("Week " + Integer.toString(weeks) + " " + Integer.toString(year));
cal.add(Calendar.WEEK_OF_YEAR, 1);
weeks= cal.get(Calendar.WEEK_OF_YEAR);
year = cal.get(Calendar.YEAR);
week2.setText("Week " + Integer.toString(weeks) + " " + Integer.toString(year));
cal.add(Calendar.WEEK_OF_YEAR, 1);
weeks= cal.get(Calendar.WEEK_OF_YEAR);
year = cal.get(Calendar.YEAR);
week3.setText("Week " + Integer.toString(weeks) + " " + Integer.toString(year));
cal.add(Calendar.WEEK_OF_YEAR, 1);
weeks= cal.get(Calendar.WEEK_OF_YEAR);
year = cal.get(Calendar.YEAR);
week4.setText("Week " + Integer.toString(weeks) + " " + Integer.toString(year));
cal.add(Calendar.WEEK_OF_YEAR, -3);
}
如有必要,我还会包含我的.fmxl文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.Main">
<children>
<GridPane>
<children>
<GridPane>
<children>
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="0" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="9" />
<TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="9" />
<Button id="prev" fx:id="prev2" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="8" GridPane.rowIndex="0" />
<Button fx:id="next" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="29.999900000002526" prefWidth="70.00009999999747" text="next" GridPane.columnIndex="0" GridPane.rowIndex="9" />
<Button fx:id="next2" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="30.0" prefWidth="70.0" text="next" GridPane.columnIndex="8" GridPane.rowIndex="9" />
<Button fx:id="prev" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<TextArea fx:id="week1" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextArea fx:id="week2" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="4" />
<TextArea fx:id="week4" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="8" />
<Label id= "lab1" fx:id="lab1" prefHeight="40.000099999997474" prefWidth="150.0" text="" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="1" />
<Label minHeight="14.0" prefHeight="14.0" prefWidth="150.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="5" />
<Label prefHeight="533.0" prefWidth="70.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="1" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="3" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="7" />
<Label prefHeight="44.0" prefWidth="87.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="5" />
<Label prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="3" />
<TextArea fx:id="week3" prefHeight="100.00009999999747" prefWidth="70.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="6" />
<Button fx:id="start" mnemonicParsing="false" onAction="#ClickStart" prefHeight="30.0" prefWidth="70.0" text="Start" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
问题在于,当我运行应用程序并按下“开始”按钮时,标签不会更改为给定的文本(默认情况下为“空”&#39;)。
答案 0 :(得分:1)
由于您已在评论中找到了解决方案,因此我们只会简要说明删除原因
lab1 = new Label();
解决了这个问题。
当您在fxml文件中声明lab1
时,您告诉JavaFX FXMLLoader
为您实例化Label
。所以基本上它为你做了new Label()
号召。然后,它会将对其创建的Label
的引用传递给您的类lab1
的变量Main
。当您更改lab1
以引用lab1 = new Label();
中新创建的标签时,您覆盖了对UI中标签的引用。该新标签尚未添加到任何Scene
并且未显示,因此当您更改其文字时,更改将不可见。