Java分配帮助(与MVC作斗争)



我有一个Java任务,我们必须使用MVC设计模式创建一个足够简单的世界时钟应用程序。

该应用程序允许用户同时在屏幕上查看多个时间(或时钟)。他们从一系列城市创建的Jlist中选择这些时钟(这些城市的相关时差用逗号分隔,例如"巴塞罗那,+1.0"。然后按添加按钮显示这些城市的时钟。还有一个删除城市按钮,其工作方式与添加按钮相同,只是明显地删除了显示的时钟。时钟和城市显示在两个并排的J滚动窗格中。

我正在为一些事情而挣扎。

  • 我不知道如何通过MVC将按钮连接到动作监听器,然后连接到选择监听器
  • 我一直在尝试如何将选定的城市存储在数组列表中,然后在该列表中迭代以显示/隐藏相关时钟。这是最好的方法吗?我忍不住觉得会有一个更优雅的解决方案
  • 我也不确定创建多个时钟的最佳方式是什么?我尝试了循环,但一直遇到问题。这是我用来创建一个时钟的代码。

    myClock = new JTextField(8);
    myClock.setFont(font);
    myClock.setEditable(false);
    myClock.setFocusable(false);
    myClock.setHorizontalAlignment(JTextField.CENTER);
    myClock.setBorder(BorderFactory.createTitledBorder(lineBorder, "City"));
    clockPanel.add(myClock);
    

然后使用此方法更新时钟时间。

    public void update() {
            myClock.setText(secondsAsTimeText(model.currentTime()));
        } 

如果有任何帮助,我将不胜感激。我已经为此工作了三天,但一直没有得到任何帮助。如果我还不清楚,请让我详细说明。我也是新来的堆栈溢出,所以我希望我的问题中没有犯任何新手错误。不要害怕指责我,这样我就可以从错误中吸取教训:)

我的建议:

  • 将diff时间存储在Map(城市名称,diffthecurrenttime)中,例如,如果您居住在伦敦,需要纽约市的当前时间,则您(NewYourk,-5)在模型初始化时通过加载属性文件放入所有键及其值,并且在添加或删除城市时不必重写模型类

  • 当视图开始时,获取所有城市并放入Jlist(联系人这样做)

您不必迭代。。使用城市名称作为关键字来查找正确的时间,如您在下面的示例代码中所见。

属性加载部分:

制作带有.properties的文件,例如configuration.properties,内容应该是这样的:

纽约=-5东京=8

并将属性文件放在src文件夹中。

制作一个类文件,该文件将从属性文件加载信息:

public class PropertiesLoader{
       public Map<String,Integer> loadCities(){
             Properties file=getProperties("configuration.properties");
             return convertPropertiesToMap(file); //it will be return empty Map if       unable to load properties
       }
       private Properties getProperties(String fileName){
              Properties prop = new Properties();
              InputStream input = null;
              try {
                   input = getClass().getClassLoader().getResourceAsStream(fileName);
                   if (input == null) {
                    //Unable to find file
                                return null;
                   }
                   prop.load(input);
              } catch (IOException ex) {
                           ex.printStackTrace();
              } finally {
                           if (input != null) {
                                  try {
                                        input.close();
                                  } catch (IOException e) {
                                        e.printStackTrace();
                                  }
                           }
          }
             return prop;
       }
       private Map<String,Integer> convertPropertiesToMap(Properties file){
              Map<String,Integer> result=new HashMap<String,Integer>();
                        if(file != null){
                               Enumeration<?> e = file.propertyNames();
                               while (e.hasMoreElements()) {
                                      String key = (String) e.nextElement();
                                      Integer value = Integer.parseInt(file.getProperty(key));
                                      result.put(key,value);
                               }
                    }     
              return result;
       }

}

您必须在模型类中创建PropertiesLoader类,并通过调用loadCities()方法将信息存储在模型中。

MVC部分:您的控制器将连接它们:

这里是一个示例代码:

   public class Controller {
       private final View view;
       private final Model model;
       public Controller(View aview, Model aModel){
              this.view=aview;
              this.model=amodel;
              registerListeners();
       }
      public void registerListener(){
             ActionListener actionListener= new ActionListener(){
                       public void actionPerformed(ActionEvent e) {    
                              String city=view.getSelectedCity();
                              view.swithClockToCity(model.getCityTime(city));                                 
                       }
             }; 
         view.addListenerToOkButton(actionListener);
      }

   }

View类中的某个位置应该是这个

public class View{
       private JList cityList;
       private JButton selectCityButton;
       //other stuff
       public String getSelectedCity(){
              cityList.getSelectedItem().toString();
       }
       public void addListenerToOkButton(ActionListener alistener){
               selectCityButton.addActionListener(alistener);
       }
       public void swithClockToCity(Long time){
         //change the clock panel or something...
         //I put here your code:
          myClock.setText(secondsAsTimeText(time));
       }

}

您必须以类似的方式编写删除按钮及其操作。我希望我能帮助你!如果你有问题,请毫不犹豫地问!

最新更新