本文介绍了如何定义一个对象以与类GUI中的类Controleur匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,我无法为Controleur类中的按钮设置事件处理程序,而该类必须由vue和modele的Controleur的构造函数来操作.

I am new to JavaFX and I cannot set the eventhandler for the buttons in the class Controleur that has to manipulate with the Contructor of Controleur of vue and modele.

问题是如何在GUI类中创建对象以与Controleur类中的Controleur的构造方法匹配?

The question how can i create the object in class GUI to match with the Constructor of Controleur in class Controleur?

任何帮助将不胜感激.

这是我的课程 GUI :

public class GUI extends Application implements IView{

     TextField textfield1, textfield2, textfield3, textfield4, textfield5;

     Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;

     Button button_add,button_sub,button_div,button_mult,button_virgule,button_neg,button_push,button_clear,button_backspace;



        // TODO Auto-generated method stub
        @Override
        public void start(Stage primaryStage) throws Exception {
            // TODO Auto-generated method stub

            AnchorPane anchorPane= new AnchorPane();
            //error place as i have no idea to create object controleur with parameter vue and modele
            Controleur controleur=new Controleur();
            Scene sc = new Scene(anchorPane, 400.0, 375.0);
            primaryStage.setTitle("Ma calculette");
            primaryStage.setScene(sc);
            primaryStage.show(); 


            textfield1= new TextField();
            textfield1.setPrefHeight(27.0);
            textfield1.setPrefWidth(400.0);
            textfield1.setLayoutY(109.0);
            textfield1.setEditable(false);
            textfield1.setAlignment(Pos.CENTER_RIGHT);
            textfield1.setId("textfield1");
            anchorPane.getChildren().add(textfield1);

            textfield2= new TextField();
            textfield2.setPrefHeight(27.0);
            textfield2.setPrefWidth(400.0);
            textfield2.setLayoutY(82.0);
            textfield2.setEditable(false);
            textfield2.setAlignment(Pos.CENTER_RIGHT);
            textfield2.setId("textfield2");
            anchorPane.getChildren().add(textfield2);

            button7 = new Button("7");
            button7.setPrefHeight(34.0);
            button7.setPrefWidth(82.0);
            button7.setLayoutY(207.0);
            button7.setLayoutX(21.0);
            button7.setMnemonicParsing(false);
            button7.setId("seven");
            //button7.setOnAction(handle);
            anchorPane.getChildren().add(button7);

            button4 = new Button("4");
            button4.setPrefHeight(34.0);
            button4.setPrefWidth(82.0);
            button4.setLayoutY(241.0);
            button4.setLayoutX(21.0);
            button4.setMnemonicParsing(false);
            //button4.setOnAction(handle);
            button4.setId("four");
}

这是我的课程 Controleur :

public class Controleur implements ActionListener{

  GUI vue;
  Accumulateur modele;

  public Controleur(GUI vue, Accumulateur modele) {
      this.vue=vue;
      this.modele=modele;

  }


    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        if(vue.textfield1.getText().isEmpty() || modele.pile.empty()) {
            if (event.getSource() == vue.button1) {
                vue.textfield1.setText(vue.textfield1.getText()+ "1");
           }else if (event.getSource() == vue.button2) {
                vue.textfield1.setText(vue.textfield1.getText() + "2");
           }else if (event.getSource() == vue.button3) {
                vue.textfield1.setText(vue.textfield1.getText() + "3");
           }else if (event.getSource() == vue.button4) {
                vue.textfield1.setText(vue.textfield1.getText() + "4");
           }else if (event.getSource() == vue.button5) {
                vue.textfield1.setText(vue.textfield1.getText() + "5");
           }else if (event.getSource() == vue.button6) {
                vue.textfield1.setText(vue.textfield1.getText() + "6");
           }else if (event.getSource() == vue.button7) {
                vue.textfield1.setText(vue.textfield1.getText() + "7");
           }else if (event.getSource() == vue.button8) {
                vue.textfield1.setText(vue.textfield1.getText() + "8");
           }else if (event.getSource() == vue.button9) {
                vue.textfield1.setText(vue.textfield1.getText() + "9");
           }else if (event.getSource() == vue.button0) {
                vue.textfield1.setText(vue.textfield1.getText() + "0");   
           }else if (event.getSource() == vue.button_virgule) {
               if(!vue.textfield1.getText().contains(".")) {
                  vue.textfield1.setText(vue.textfield1.getText() + ".");   
              }else {
                   }
           }else if (event.getSource() == vue.button_push) {
               modele.pile.push(Double.parseDouble(vue.textfield1.getText()));
           }else if (event.getSource() == vue.button_backspace) {
               if(!vue.textfield1.getText().isEmpty()) {
                  vue.textfield1.setText(""+vue.textfield1.getText().substring(0, vue.textfield1.getText().length() - 1));
               }else {                     
               }
           }
    }

推荐答案

JavaFX包含EventHandler,因此您无需使用awt库.另外,您声明了Controleur的实例,但没有提及任何内容作为参数,因此会出现编译错误.下一步在Gui类中:

JavaFX includes EventHandler , so you don’t need to use awt libraries.Also, you declared instance of Controleur but didn’t mentioned anything as argument, so there will be compiling error. Do nextIn Gui class :

Controleur controleur = new Controleur(this);
button.setOnAction(controleur);

在控制人员课程中:

Gui parent;
Accumulator accumulator;
public Controleur(Gui parent){
 this.parent = parent;
 accumulator = new Accumulator ();
}
public void handle(Event event){
 Object ob = event.getSource();
  //here we communicate through reference parent
  if(ob == parent.button){
   String data = parent.textField.getText();
  // here we add value to list in Accumulator class through accumulator reference 
   accumulator.list.add(data);
 }
}

在Accumulator类(它是模型)中:

In Accumulator class (which is model) :

List list;
Accumulator (){
 list = new ArrayList();
}

当您按下按钮时,通过Controleur类父级的引用,它询问控制器已按下了哪个按钮?然后,通过参考累加器,取决于所按下的按钮,控制器将值通过累加器传递到Accimulator类(为model)中的List,以将数据保存在list中.这样便可以将值从GUI传递到Controller到Model.

When you press button ,through the reference of Controleur class parent it asks Controller what button has been pressed? Then , through the reference accumulator , depends on what button has been pressed, Controler pass value through accumulator to List in Accimulator class(which is model ) to save data in list. That’s how you can pass value from GUI to Controller to Model.

这篇关于如何定义一个对象以与类GUI中的类Controleur匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:37