我正在尝试使用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);

    }


};

但是,这不起作用。

是否可以从控制器处理FocusEvent?

编辑:

天哪,我的坏。不太了解罗宾是怎么回事。我太着迷于在某个地方显式定义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!!!");
        }
    });


尽管我非常喜欢nCCE的工作方式,但控制器中的控制器仍可以按我计划的方式正常工作。出于好奇,是否有在Swing Apps上实现MVC的标准或广泛接受的方式?

最佳答案

据我了解,您的方式就是这样。更好的是,我更喜欢匿名类,他们尊重封装的概念。在这里尝试一下此代码,看看您可以从中学到什么:

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();
            }
        });
    }
}

10-06 13:58