国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java添加事件監聽的四種方法代碼實例

2019-11-26 15:24:56
字體:
來源:轉載
供稿:網友

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();  }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望谟县| 平阳县| 资阳市| 乡宁县| 延边| 新晃| 江陵县| 广安市| 松原市| 庆云县| 泰州市| 绥化市| 阳春市| 岳西县| 临泉县| 三明市| 金平| 金坛市| 新和县| 县级市| 雅安市| 卓尼县| 建平县| 阜新市| 文昌市| 宿松县| 泰顺县| 岢岚县| 邻水| 北宁市| 郯城县| 湾仔区| 宜州市| 清远市| 德兴市| 武夷山市| 嘉善县| 临澧县| 茶陵县| 平度市| 东明县|