我正在尝试使用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!!!");
}
});在控制器中以我计划的方式工作很好,尽管我非常喜欢nIcE cOw的方式。出于好奇,有没有一种标准的或被广泛接受的在Swing应用上实现MVC的方式?
发布于 2012-04-20 21:11:40
据我所知,你是这样做的。更好的是,我更喜欢匿名类,它们尊重封装的概念。在这里,你可以尝试一下这段代码,看看你能掌握什么:
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();
}
});
}
}https://stackoverflow.com/questions/10246511
复制相似问题