JAVA 表单检测变化

JAVA form detect change

我想在模型视图控制器设计模式的上下文中单独处理事件。 所以,假设我有一个 class extends JFrame.

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;


public class Example2 extends JFrame{
  private JSpinner spinner1;
  private JSpinner spinner2;
  private JSpinner spinner3;
  private JSpinner spinner4;

  private JLabel  lbl1;
  private JLabel  lbl2;
  private JLabel  lbl3;
  private JLabel  lbl4;

  private JButton button;

    public Example2() {
        setLayout(new GridBagLayout());
        initComponents();
        buildFrame();
        pack();
    }



private void initComponents(){
   lbl1 = new JLabel("Number 1:");
    lbl2 = new JLabel("Number 2:");
     lbl3 = new JLabel("Number 3:"); 
     lbl4 = new JLabel("Number 4:");

     button = new JButton("O.K");
     spinner1 = new JSpinner(new SpinnerNumberModel(0,Integer.MIN_VALUE, Integer.MAX_VALUE, 1));
     spinner2 = new JSpinner(new SpinnerNumberModel(0,Integer.MIN_VALUE, Integer.MAX_VALUE, 1));
     spinner3 = new JSpinner(new SpinnerNumberModel(0,Integer.MIN_VALUE, Integer.MAX_VALUE, 1)); 
     spinner4 = new JSpinner(new SpinnerNumberModel(0,Integer.MIN_VALUE, Integer.MAX_VALUE, 1));

     button = new JButton("O.K");

}

private void buildFrame(){
    GridBagConstraints gc = new GridBagConstraints(); 
    gc.gridx = 0;
    // First row
    gc.gridy = 0 ;
    getContentPane().add(lbl1,gc);
    // Next row
    gc.gridy++ ;
    getContentPane().add(lbl2,gc);
    // Next row
    gc.gridy++ ;
    getContentPane().add(lbl3,gc);

    // Next row
    gc.gridy++ ;
    getContentPane().add(lbl4,gc);

    gc.gridy = 0;
    gc.gridx = 1;

    getContentPane().add(spinner1,gc);

    // Next row
    gc.gridy++ ; 
     getContentPane().add(spinner2,gc);
     // Next row
    gc.gridy++ ; 
     getContentPane().add(spinner3,gc);

     // Next row
    gc.gridy++ ; 
     getContentPane().add(spinner4,gc);


    // Next row 
     gc.gridy++;
     gc.gridx = 0 ; 

    getContentPane().add(button,gc);


}

public static void main(String[] args){
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Example2().setVisible(true);
         }
    });
}



}

有没有办法单独处理事件。在 Example2 class.Some 中制作一个 class 女巫处理事件。

public class Example2EventsManager {
}

而不是在 Example2 中处理事件 class:

button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("processing");
         }
    });
..........
spinner1.addChangeListener(new ChangeListener() {

    @Override
    public void stateChanged(ChangeEvent ce) {
      }
});
etc.

创建一个实现 ActionListener 接口的class

public class MyActionListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
         System.out.println(...);
    }
}

然后使用class:

button.addActionListener( new MyActionListener() );

如果你愿意,你可以实现多个接口

public class MyListener implements ActionListener, ChangeListener
...