首页 文章

JavaFX Label不设置文本

提问于
浏览
0

我正在用JavaFX SceneBuilder做一个日历 .

我在我的Main类上声明了我的Label:

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>

问题是,当我运行应用程序并按下“开始”按钮时,标签不会更改为给定的文本(默认情况下为“空”) .

1 回答

  • 1

    由于您已经在评论中找到了解决方案,因此我将简要介绍一下删除的原因

    lab1 = new Label();
    

    解决了这个问题 .

    当您在fxml文件中声明 lab1 时,您告诉JavaFX FXMLLoader 为您实例化 Label . 所以基本上它为你做了 new Label() 电话 . 然后它将对它创建的 Label 的引用传递给类 Main 的变量 lab1 . 当您更改 lab1 以在 lab1 = new Label(); 中引用新创建的标签时,您覆盖了对UI中标签的引用 . 该新标签尚未添加到任何 Scene 并且未显示,因此当您更改其文本时,更改将不可见 .

相关问题