>  기사  >  Java  >  Java에서 이벤트 수신을 추가하는 네 가지 방법의 코드 예

Java에서 이벤트 수신을 추가하는 네 가지 방법의 코드 예

高洛峰
高洛峰원래의
2017-01-22 16:52:161984검색

Java에서 이벤트를 추가하는 여러 가지 방법(약간 변경된 codebrother의 기사에서 재인쇄됨):

/**
 * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
 *
 * @author codebrother
 */
class EventListener1 extends JFrame implements ActionListener {
  private JButton btBlue, btDialog;
 
  public EventListener1() {
    setTitle("Java GUI 事件监听处理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("蓝色");   
    btDialog = new JButton("弹窗");
     
    // 将按钮添加事件监听器
    btBlue.addActionListener(this);
    btDialog.addActionListener(this);
 
    add(btBlue);
    add(btDialog);
 
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // ***************************事件处理***************************
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == btBlue) {
      Container c = getContentPane();
      c.setBackground(Color.BLUE);
    }
    else if (e.getSource() == btDialog) {
      JDialog dialog = new JDialog();
      dialog.setBounds(300, 200, 400, 300);
      dialog.setVisible(true);
    }
  }
 
}
 
/**
 * Java事件监听处理——内部类处理
 *
 * @author codebrother
 */
 
class EventListener3 extends JFrame {
  private JButton btBlue, btDialog;
 
  // 构造方法
  public EventListener3() {
    setTitle("Java GUI 事件监听处理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("蓝色");
    btDialog = new JButton("弹窗");
    // 添加事件监听器对象(面向对象思想)
    btBlue.addActionListener(new ColorEventListener());
    btDialog.addActionListener(new DialogEventListener());
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // 内部类ColorEventListener,实现ActionListener接口
  class ColorEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      Container c = getContentPane();
      c.setBackground(Color.BLUE);
    }
  }
  // 内部类DialogEventListener,实现ActionListener接口
  class DialogEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      JDialog dialog = new JDialog();
      dialog.setBounds(300, 200, 400, 300);
      dialog.setVisible(true);
    }
  }
 
}
 
/**
 * Java事件监听处理——匿名内部类处理
 *
 * @author codebrother
 */
class EventListener2 extends JFrame {
  private JButton btBlue, btDialog;
 
  public EventListener2() {
    setTitle("Java GUI 事件监听处理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
 
    btBlue = new JButton("蓝色");
    btDialog = new JButton("弹窗");
     
    // 添加事件监听器(此处即为匿名类)
    btBlue.addActionListener(new ActionListener() {
      // 事件处理
      @Override
      public void actionPerformed(ActionEvent e) {
        Container c = getContentPane();
        c.setBackground(Color.BLUE);
      }
    });
     
    // 并添加事件监听器 
    btDialog.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JDialog dialog = new JDialog();
        dialog.setBounds(300, 200, 400, 300);
        dialog.setVisible(true);
      }
    });
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 
}
 
/**
 * Java事件监听处理——外部类处理
 *
 * @author codebrother
 */
class EventListener4 extends JFrame {
  private JButton btBlue, btDialog;
 
  public EventListener4() {
    setTitle("Java GUI 事件监听处理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("蓝色");
    btDialog = new JButton("弹窗");
    // 将按钮添加事件监听器
    btBlue.addActionListener(new ColorEventListener(this));
    btDialog.addActionListener(new DialogEventListener());
 
    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
 
}
// 外部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
  private EventListener4 el;
  ColorEventListener(EventListener4 el) {
    this.el = el;
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    Container c = el.getContentPane();
    c.setBackground(Color.BLUE);
  }
}
// 外部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
    JDialog dialog = new JDialog();
    dialog.setBounds(300, 200, 400, 300);
    dialog.setVisible(true);
  }
}
 
public class ActionListenerTest
{
  public static void main(String args[])
  {
    new EventListener2();
  }
}

Java에서 이벤트 수신을 추가하는 네 가지 방법에 대한 더 많은 코드 예제를 보려면 PHP 중국어 웹사이트에 주의하세요. 관련 기사를 보려면!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.