加入GUI(Swing)和业务逻辑

时间:2015-04-12 02:52:01

标签: java swing observer-pattern business-logic

免责声明: - 以下示例是我在现实中尝试做的高度缩放的版本。

概述:

应用程序名称:随机数生成器。

所需的应用程序流程:RandomNumberLogic类每10秒生成一个随机数。生成的数字应显示在RandomNumberPanel上。

应用程序设计目标:使GUI和业务逻辑尽可能分开。 (低耦合是我要找的词,我猜)。

代码:

BasicRandomApplication.java

package SideActivities;

import javax.swing.SwingUtilities;

public class BasicRandomApplication
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
           public void run()
           {
                new MainFrame();
                RandomNumberLogic x = new RandomNumberLogic();
                x.generateNumbers();
            }
        });
    }
}

MainFrame.java

package SideActivities;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;

public class MainFrame extends JFrame
{
    RandomNumberPanel randomNumberPanel;

    MainFrame()
    { 
        randomNumberPanel = new RandomNumberPanel();

        this.setLayout(new BorderLayout());

        this.add(randomNumberPanel, BorderLayout.CENTER);
        this.setSize(new Dimension(400, 500));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }
}

RandomNumberLogic.java

package SideActivities;

import java.util.Random;

public class RandomNumberLogic
{
    private ModelListener listener;

    public void generateNumbers()
    {
        long firstTime = System.currentTimeMillis();
        int refreshRate = 10 * 1000;
        long delta = 0;
        while (true)
        {
            long secondTime = System.currentTimeMillis();
            delta += (secondTime - firstTime) / refreshRate;
            if (delta >= 1)
            {
                Random random = new Random();
                ModelEvent e = new ModelEvent();
                e.setNumber(random.nextInt());
                listener.refreshData(e);
                delta--;
           }
       }
   }

   public void setListener(ModelListener listener)
   {
        this.listener = listener;
   }
}

RandomNumberPanel.java

package SideActivities;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class RandomNumberPanel extends JPanel implements ModelListener
{
    private JLabel number;

    RandomNumberPanel()
    {
        number = new JLabel();

        this.setLayout(new BorderLayout());

        this.add(number, BorderLayout.CENTER);
        this.setBorder(BorderFactory.createLineBorder(Color.RED, 3));
    }

    @Override
    public void refreshData(ModelEvent e)
    {
        number.setText(e.getNumber() + "");
    }

}

ModelEvent.java

package SideActivities;

public class ModelEvent
{
    private int number;

    public void setNumber(int number)
    {
        this.number = number;
    }

    public int getNumber()
    {
        return this.number;
    }
}

ModelListener.java

 package SideActivities;

 public interface ModelListener
 {
     public void refreshData(ModelEvent e);
 }

问题1)为什么我的GUI应用程序会永远开始显示某些内容?

问题2)为什么我会得到以下异常?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SideActivities.RandomNumberLogic.generateNumbers(RandomNumberLogic.java:23)
    at SideActivities.BasicRandomApplication$1.run(BasicRandomApplication.java:15)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Q2的解决方案)从未调用过setListener方法。

我做了以下更改,但GUI仍需要永远更新。可能是什么原因?我正在构建应用程序吗?

BasicRandomApplication.java

package SideActivities;

import javax.swing.SwingUtilities;

public class BasicRandomApplication
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                MainFrame y = new MainFrame();
                RandomNumberLogic x = new RandomNumberLogic();
                x.setListener(y.randomNumberPanel);
                x.generateNumbers();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

此处listener为空。调用类setListener()

的方法RandomNumberLogic