Java新增事件的幾種方式(轉載了codebrother的文章,做了稍微的改變):
/** * Java事件監聽處理――自身類別實作ActionListener接口,作為事件監聽器* * @author codebrother */class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { private JButton btBlue, btDialog; public EventListener1() { 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(new FlowLayout()); btBlue = new JButton("色"); btDialog = new JButton("彈跳窗"); //新增事件監聽器物件(物件導向思想) btBlue.addActionListener(new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.CloseOperITation. ); } // 內部類別ColorEventListener,實作ActionListener介面class ColorEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } } // 內部類別DialogEventListener,實作ActionList 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.EEXIT); }}/** * Java事件監聽處理――外部類別處理* * @author codebrother */class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle("Java GUI 事件監聽處理"); setBounds(100, 100, 500, 350); ; btBlue = new JButton("藍色"); btDialog = new JButton("彈跳窗"); // 將按鈕新增事件監聽器btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogListener()); add( btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}// 外部類別ColorEventListener,實作ActionListener介面class ColorEventListener implements ActionListener { private EventListener4 el; ColorEventListener(PerListener4 el) { Event.el = action eoid. Container c = el.getContentPane(); c.setBackground(Color.BLUE); }}// 外部類別DialogEventListener,實作ActionListener介面class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog log; .setBounds(300, 200, 400, 300); dialog.setVisible(true); }}public class ActionListenerTest{ public static void main(String args[]) { new EventListener2(); }}