MVC实现的Java Swing的FocusListener的(MVC implementation

2019-09-16 15:17发布

我试图建立一个使用MVC架构模式一个简单的Java Swing应用程序。 我所做的是在我的意见创建用户界面组件(私人),并使其返回组件的公共方法。 这些方法然后由控制器,通过它,我可以写事件/动作侦听器的方法调用。 下面是一个示例实施例:

视图:

private JButton btnAdd;

    public JButton getBtnAdd(){
        return btnAdd;
    }

控制:

myGuiFrame gui = new myGuiFrame();


        //on add button clicked
    gui.getBtnAdd().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //calls to model
        }
    });

这是实现正确的吗?

如果是这样,那么我在与FocusListeners问题。 当我在视图中创建一个的FocusListener的focusLost和focusGained方法是在视图中创建。

private FocusListener l;

someComponent.addFocusListener(l);

        l = new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
            }

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub

            }
        };

我希望所有的事件处理程序是在我的控制器。 我的问题是...是有办法,我可以调用/声明focusLost和focusGained从我的控制器的方法呢? 我试图定义的FocusListener作为公众,这样我可以在我的控制器定义它:

视图:

public FocusListener l;
public someComponentType someComponent;

控制器:

gui.l = new FocusListener() {
    @Override
    public void focusLost(FocusEvent e) {
        // TODO Auto-generated method stub

}

@Override
public void focusGained(FocusEvent e) {
    // TODO Auto-generated method stub
    gui.someComponent.addFocusListener(gui.l);

    }   

};

然而,这是行不通的。

是否可以处理来自控制器FocusEvents?

编辑:

天哪,我的坏。 不太明白什么罗宾是所有有关。 我是有明确规定的地方的的FocusListener太迷恋。 一个简单的:

    gui.getTextFieldEmployeeCode().addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void focusGained(FocusEvent e) {
            // TODO Auto-generated method stub
            System.out.println("YES!!!");
        }
    });

在控制器将工作在我打算做的方式就好了,虽然我很喜欢有多好牛的走了。 只是出于好奇,有没有Swing的应用程序实现MVC的标准或广泛接受的方式?

Answer 1:

据我了解,你做的方式是这样的。 更妙的是,我更喜欢匿名类,他们尊重封装的概念。 这里尝试你对这个代码的手,看看有什么你可以用这个把握:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class View
{
    private JButton focusButton;
    private JButton spareButton;

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("VIEW");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        focusButton = new JButton("GAINED/LOST");
        focusButton.addFocusListener(new ButtonController(this));

        spareButton = new JButton("SPARE");
        spareButton.setOpaque(true);
        spareButton.addActionListener(new ButtonController(this));
        spareButton.addFocusListener(new ButtonController(this));

        contentPane.add(focusButton);
        contentPane.add(spareButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public JButton getFocusButton()
    {
        return focusButton;
    }

    public JButton getSpareButton() 
    {
        return spareButton;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new View().createAndDisplayGUI();
            }
        });
    }
}

class ButtonController implements FocusListener, ActionListener
{
    private View view;
    private JButton focusButton;
    private JButton spareButton;

    public ButtonController(View v)
    {
        view = v;
        focusButton = view.getFocusButton();
        spareButton = view.getSpareButton();
    }

    public void actionPerformed(ActionEvent ae)
    {
        JButton button = (JButton) ae.getSource();

        if (button == spareButton)
        {
            spareButton.setBackground(Color.BLUE);
            focusButton.setEnabled(true);
        }
    }

    public void focusGained(FocusEvent fe)
    {
        JButton button = (JButton) fe.getSource();

        if (button == focusButton)
        {
            focusButton.setEnabled(true);
        }
        else if (button == spareButton)
        {
            spareButton.setBackground(Color.WHITE);
        }
    }

    public void focusLost(FocusEvent fe)
    {
        JButton button = (JButton) fe.getSource();

        if (button == focusButton)
        {
            focusButton.setEnabled(false);
        }
        else if (button == spareButton)
        {
            spareButton.setBackground(Color.DARK_GRAY.darker());
        }
    }
}

另一种方法,如果你觉得被激怒getters/setters和各地发送的参考,是定义一个内部类如下(在前面的例子简单的解决方法):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class View
{
    private JButton focusButton;
    private JButton spareButton;

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("VIEW");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        focusButton = new JButton("GAINED/LOST");
        focusButton.addFocusListener(new ButtonController());

        spareButton = new JButton("SPARE");
        spareButton.setOpaque(true);
        spareButton.addActionListener(new ButtonController());
        spareButton.addFocusListener(new ButtonController());

        contentPane.add(focusButton);
        contentPane.add(spareButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private class ButtonController implements FocusListener, ActionListener
    {

        public void actionPerformed(ActionEvent ae)
        {
            JButton button = (JButton) ae.getSource();

            if (button == spareButton)
            {
                spareButton.setBackground(Color.BLUE);
                focusButton.setEnabled(true);
            }
        }

        public void focusGained(FocusEvent fe)
        {
            JButton button = (JButton) fe.getSource();

            if (button == focusButton)
                focusButton.setEnabled(true);
            else if (button == spareButton)
                spareButton.setBackground(Color.WHITE);
        }

        public void focusLost(FocusEvent fe)
        {
            JButton button = (JButton) fe.getSource();

            if (button == focusButton)
                focusButton.setEnabled(false);
            else if (button == spareButton)
                spareButton.setBackground(Color.DARK_GRAY.darker());
        }
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new View().createAndDisplayGUI();
            }
        });
    }
}


文章来源: MVC implementation of Java Swing FocusListener