【问题标题】:How do you change the label text in java using eclipse and scene builder?如何使用 Eclipse 和场景生成器更改 Java 中的标签文本?
【发布时间】:2015-02-14 11:00:03
【问题描述】:

我正在尝试使用 Eclipse 和场景构建器制作“Hello World”。

package lj.HelloWorld;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;

public class HelloWorld extends Application {
    private Stage primaryStage;
    private BorderPane rootLayout;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("AddressApp");

    initRootLayout();
}

/**
 * Initializes the root layout.
 */
public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(HelloWorld.class.getResource("rootHelloWorld.fxml"));
        rootLayout = (BorderPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
// my attempt to refer to my label object in scenebuilder.
@FXML
private Label lblOutput;

@FXML
private void handleClick(ActionEvent event) {
    lblOutput.setText("This");
}
}

这是我的 fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane prefHeight="200.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="btnClick" layoutX="98.0" layoutY="14.0" mnemonicParsing="false" text="Click Me" />
            <Label fx:id="lblOutput" layoutX="167.0" layoutY="18.0" text="1231231" />
         </children>
      </Pane>
   </center>
</BorderPane>

我只想知道如何引用标签并将其文本更改为“Hello World”。 PS:我来自vb.net学习java。我曾尝试在 google 上寻找 Hello World 示例,但有些网站已关闭,而其他网站对我来说太复杂了。就像 ch.makery 教程一样。

【问题讨论】:

    标签: java eclipse javafx scenebuilder


    【解决方案1】:

    你需要一个控制器类

    package lj.HelloWorld;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.text.Text;
     public class FXMLDocumentController {
    
    @FXML private Label lblOutput;
    
    @FXML protected void handleButtonAction(ActionEvent e){
    lblOutput.setText("Hello World");
    }
    }
    

    并告诉你的 XML 控制器类

     <BorderPane  fx:controller="lj.HelloWorld.FXMLDocumentController"
      <Button fx:id="btnClick" layoutX="98.0" layoutY="14.0" mnemonicParsing="false"    text="Click Me"
    onAction="#handleButtonAction"/>
    

    有关详细信息和教程,请阅读:http://docs.oracle.com/javafx/2/get_started/hello_world.htm

    【讨论】:

    • 你的速度更快了:)。 @YinYangKim,您可以创建控制器类并将它们与场景构建器链接到场景。在那里,您可以转到按钮和标签的代码窗格,您可以将 ID 链接到组件,并且可以将处理程序(如 handleButtonAction)链接到您的按钮。您实际上并不需要 ActionEvent。您可以创建任何由点击执行的方法。
    • 我收到 javafx.fxml.LoadException: /C:/Users/Kim/Documents/Workspace/Learning%20Java/bin/lj/HelloWorld/rootHelloWorld.fxml:8 错误。如果我没看错,则错误指向&lt;BorderPane prefHeight="200.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lj.HelloWorld.FXMLDocumentController"&gt; 我还在错误日志Caused by: java.lang.ClassNotFoundException: lj.HelloWorld.FXMLDocumentController 的底部找到了这个
    • 他找不到控制器类。您是否在包 lj.HelloWorld 中创建了一个名为 FXMLDocumentController 的新类?
    • 该死,错过了“创建一个新的类部分”抱歉。我只是尝试覆盖我的 HelloWorld 类。现在可以了。谢谢,对于这个和教程。不过有一个问题,这是什么import javafx.scene.text.Text;?它在我的项目中被下划线标记为“未使用”。
    • javafx.scene.text.Text 只有在不使用文本字段的情况下才需要文本字段,您可以将其删除。
    【解决方案2】:

    您可以尝试从标签的实例中引用,例如: lbl.setText("Hello World");

    【讨论】:

    • 他没有使用 Swing,他使用的是 JavaFX。没有 JLabel 之类的东西
    • 我认为它很好!
    • 并非如此。正如您在代码中看到的那样,他已经完成了 lbl.setText ... 但问题是如何将点击链接到处理程序。所以解决方案是控制器类而不是 setText 调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多