gpt4 book ai didi

java - Java Swing FocusListener的MVC实现

转载 作者:行者123 更新时间:2023-11-30 06:30:44 25 4
gpt4 key购买 nike

我正在尝试使用 MVC 架构模式构建一个简单的 Java Swing 应用程序。我所做的是在我的 View 中创建用户界面组件(作为私有(private)的),并具有返回组件的公共(public)方法。然后 Controller 调用这些方法,通过它们我可以为事件/ Action 监听器编写方法。下面是一个示例:

查看:

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 的问题。当我在 View 中创建 FocusListener 时,会在 View 中创建 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

}
};

我希望所有事件处理程序都在我的 Controller 中。我的问题是......有没有一种方法可以从我的 Controller 调用/声明 focusLost 和 focusGained 方法?我试图将 FocusListener 定义为公共(public)的,以便我可以在我的 Controller 中定义它:

查看:

public FocusListener l;
public someComponentType someComponent;

Controller :

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

}

};

但这不起作用。

是否可以从 Controller 处理 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!!!");
}
});

在 Controller 中可以按照我计划的方式正常工作,尽管我非常喜欢 nIcE cOw 的处理方式。出于好奇,是否存在在 Swing 应用程序上实现 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();
}
});
}
}

关于java - Java Swing FocusListener的MVC实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com