【问题标题】:mvc pattern gui actionlistener classmvc 模式 gui actionlistener 类
【发布时间】:2016-04-13 06:46:55
【问题描述】:

我正在使用 gui 开发一个基本的 mvc 模式。此代码包括 JComboBox。当点击 jcombobox 并从这里选择一个字符串项时,空标签将填充一个特定的字符串。我应该如何做我的控制器类?

这是我的 Gui 课

public class SchoolView extends JPanel {
    private JTextField selectedStandName;
    private JComboBox<String> stands;
    private JLabel standLabel;
    String[] items = { "Rainbow Pots", "BookWorm's Place","Caffein Charger","Jedi's Place"};

    public SchoolView() {
        stands = new JComboBox<String>(items);
        add(stands);
        addStandLabel();
        addStandInfoLabel();
        addStandInfoField();
    }

    private void addStandLabel() {
        standLabel = new JLabel("Stands");
        add(standLabel);
    }

    private void addStandInfoLabel() {
        JLabel selectedStandInfo = new JLabel("Stand Info");
        add(selectedStandInfo);
    }
    private void addStandInfoField() {
        SelectedStandName = new JTextField();
        add(selectedStandName);
        selectedStandName.setColumns(20);
    }
}

这是我的控制器类

public class SchoolController {
    public SchoolController(){}
}

【问题讨论】:

    标签: java user-interface model-view-controller


    【解决方案1】:

    鉴于MVC relationship:
    视图对模型更新做出反应(观察者模式)
    控制器对视图做出反应(观察者模式)
    控制器更新模型

    然后您的控制器将自己订阅 SchoolView 并在事件中更新第二个视图组件。 见下面的伪代码:

    public class SchoolController implements OnSelectListener {
        JTextField viewComponent;
        public SchoolController(schoolView, viewComponent){
            schoolView.onSelect(this)
            this.viewComponent= viewComponent;
        }
    
        @Override
        public void onSelect(selectedItem) {
            viewComponent.set(selectedItem);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 2014-08-16
      • 2014-01-04
      • 1970-01-01
      • 2012-11-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      相关资源
      最近更新 更多