当前位置: 首页 > 知识库问答 >
问题:

从java fx中的其他类更新标签

卞博简
2023-03-14

JavaFX来说非常陌生,对控制器的工作方式缺乏一点了解,但现在就开始了。

我的问题很简单。我需要在运行时更新屏幕上的标签

这个问题以前在这个网站上已经解决过:

Java FX更改标签文本

Java FX更改标签文本2

传递参数

还有,这些链接描述的是同样的事情,但做的不同吗?

import javafx.scene.control.Label;
import javafx.scene.layout.Pane;

public class CoursePane extends Pane {

    private Label courseID;

    public CoursePane(Label courseID) {

        this.courseID = courseID;
    }

    public String getCourseID() {

        return courseID.getText();
    }

    public Label getCourseLabel() {

        return courseID;
    }

    public void setCourseID(String ID) {

        courseID.setText(ID);   
    }
}
public class CourseContext {


    static String fxmlfile;
    private static Object paneSrc; //the CoursePane that was clicked on

    public static void start(CoursePane pane, String courseSrc) {

        //Context Menu
        ContextMenu contextMenu = new ContextMenu();

        //MenuItems
        MenuItem item4 = new MenuItem("option");

        //add items to context menu
        contextMenu.getItems().addAll(item4);

        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.isSecondaryButtonDown()) {

                    //the coursePane that was right clicked on
                    paneSrc = event.getSource().toString();

                    contextMenu.show(pane, event.getScreenX(), event.getScreenY());

                    item4.setOnAction(new EventHandler<ActionEvent>() {

                        @Override
                        public void handle(ActionEvent event) {

                            try {

                                FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("my fxml file for the radio Buttons"));

                                Parent root= loader.load();

                                ElectiveController electiveController = loader.getController();

                                electiveController.start( "pass the coursePane that was right clicked on" );

                                Scene scene = new Scene(root);

                                Stage stage = new Stage();

                                stage.setScene(scene);

                                stage.setTitle("Set Elective");

                                stage.show();

                            }
                            catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });
    }
}

最后,具有标签应设置为的值的类:

public class ElectiveController {

    @FXML
    private Button setButton;

    private RadioButton chk;

    //the pane that was right clicked on
    private static String courseSource;

    public void start(Course courseSrc) { //courseSrc: the Pane you right clicked on

        courseSource = courseSrc.getCoursenamenumber().getValue();

    }//end start

    //sets the course pane with the selected elective radio button
    @FXML
    private void setElective() {

        chk = (RadioButton)humElectiveGroup.getSelectedToggle();

        //This is supposed to set the value for the coursePane Object to show on the screen!
        MainStage.getCoursePanes().get(courseSource).setCourseID(chk.getText());

        Stage stage = (Stage) setButton.getScene().getWindow();

        stage.close();
    }
}

我研究了依赖注入,尝试绑定和传递参数,但得到了相同的结果。我知道这是直截了当的,任何帮助都很感激!谢了。

共有1个答案

鲁羽
2023-03-14

以下是如何将不同部分连接起来的mcve。
-可以将其复制粘贴到单个文件中并调用。
-请注意,它并不是用来表示或模拟应用程序的。它旨在演示一个(非常基本和简单的)问题解决方案

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

//main class
public class UpdateViewByMenu extends Application {

    private Controller controller;

    @Override
    public void start(Stage stage) throws Exception {

        BorderPane root = new BorderPane();
        controller = new Controller();
        root.setTop(controller.getMenu());
        root.setBottom(controller.getView());
        Scene scene = new Scene(root, 350,200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args);}
}

//controller which "wires" view to model
class Controller {

    private Model model;
    private View view;
    private TopMenu menu;

    public Controller() {
        model = new Model();
        view = new View();
        menu = new TopMenu();
        //wire up menu to model : menu changes update model
        menu.getMenuTextProperty().addListener(
                e-> model.setCourseID(menu.getMenuTextProperty().get()));
        //wire model to view: change in model update view
        view. geLabelTextProerty().bind(model.getCourseIDProperty());
        //set initial value to show
        menu.getMenuTextProperty().set("Not set");
    }

    Model     getModel() {return model;}
    Pane      getView()  { return view;}
    MenuBar  getMenu()  { return menu; }
}

//model which represent the data, in this case label info
class Model{

    SimpleStringProperty courseIdProperty;
    Model(){
        courseIdProperty = new SimpleStringProperty();
    }

    StringProperty getCourseIDProperty() {

        return courseIdProperty;
    }

    void setCourseID(String id) {

        courseIdProperty.set(id);
    }
}

//represents main view, in this case a container for a label
class View extends HBox {

    private Label courseID;

    View() {
        courseID = new Label();
        getChildren().add(courseID);
    }

    StringProperty geLabelTextProerty() {

        return courseID.textProperty();
    }
}

//menu
class TopMenu extends MenuBar{

    SimpleStringProperty menuTextProperty;

    TopMenu() {
        menuTextProperty = new SimpleStringProperty();
        Menu menu = new Menu("Select id");
        MenuItem item1 =  getMenuItem("10021");
        MenuItem item2 =  getMenuItem("10022");
        MenuItem item3 =  getMenuItem("10023");
        MenuItem item4 =  getMenuItem("10024");
        menu.getItems().addAll(item1, item2, item3, item4);
        getMenus().add(menu);
    }

    MenuItem getMenuItem(String text) {

        MenuItem item =  new MenuItem(text);
        item.setOnAction(e -> menuTextProperty.set(item.textProperty().get()));
        return item;
    }

    StringProperty getMenuTextProperty() {

        return menuTextProperty;
    }
}

如有需要,请毫不犹豫地要求澄清。

 类似资料:
  • 我有个问题。我想从另一个控制器类更改窗格的颜色。我正在使用以下代码: JavaFXApplication16 FXML

  • 我是的新手,我有一个和多个场景,我可以使用在它们之间切换,所有工作都很好,直到我尝试在单击菜单项时做一些事情而不是显示场景。 我试图通过使用方法类中调用,在那里我有菜单项action methods,我调用在单击菜单项时将文本写入方法中的文本字段,但在加载该场景的控制器后,什么都没有发生。这是我的代码: 类 家庭控制器类 我知道我可能忽略了一些东西,但我不知道它是什么,帮助将不胜感激。

  • 控制器类别:包装样品;

  • 我想从我用FXMLoader加载的场景中获取控制器。用例是: > 我的 JSON 管理器收到一个 JSON 对象 我启动的任务显示一个新的场景 在那之后,我有空旷的场景。 现在我这样做来填充组件 我的问题是,有没有更简单的方法来做到这一点?我有一个在 FXML 中指定的控制器 我想获得具有绑定值的实例(在本例中称为name的TextField) 提前谢谢

  • > System.out正确显示标签上的id; 第二个控制器与嵌入在第一个控制器中的FXML文件绑定。如代码所示,第二个FXML文件/控制器是动态加载的:。 当从第二个控制器中的initialize方法设置id时,id会按预期显示在标签中: public void initialize(){this.setid(“12”);} 编辑: 此窗格声明为属性 第二个:

  • 我有一个带有标签和按钮的窗口,还有一个带有文本字段和按钮的窗口。在主窗口中,我想使用按钮打开另一个窗口,在新窗口的文本字段中输入一些内容,单击新窗口上的按钮后,我想关闭该窗口,并用输入的文本更新主窗口标签。另外,我希望新窗口是模态的。 主窗口 新窗口 我知道它的设置一点都不正确,但希望它能解释我正在尝试做什么。