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

时间:2018-08-18 06:21:31

标签: java javafx

JavaFX的新手,对控制器的工作方式缺乏一点了解,但是事情就到这里了。

我的问题很容易。我需要在运行时更新屏幕上的Label

此问题已在以下网站上得到解决:

Java FX change Label text

Java FX change Label text 2

Passing Parameters

这些链接是否描述了相同的内容,但执行的方式不同?

但是我的程序有些不同。

程序流程如下:

主舞台有几个Objects,它们在Pane的内部扩展为Label。可以右键单击这些Objects,以打开上下文菜单。上下文菜单中的一个选项将使用RadioButtons打开一个新窗口。

这个想法是选择RadioButtons中的一个,并使用该字符串将Label改写回主舞台。

但是我的代码第一次只能运行一次。屏幕上未显示所有后续更改。我什至可以输出已更改为Label的{​​{1}},它显示正确的值,但是从不更新Console上的Label

在屏幕上显示标签的类:

Stage

调用菜单的上下文菜单类:

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(); } } }); } } }); } } 值的类设置为:

Label

我研究了依赖项注入,尝试了绑定和传递参数,但是得到了相同的结果。我知道这很简单,我们将不胜感激!谢谢。

1 个答案:

答案 0 :(得分:0)

这里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;
    }
}

请随时根据需要进行澄清。