溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么理解Java事件響應

發(fā)布時間:2021-11-03 16:31:54 來源:億速云 閱讀:130 作者:iii 欄目:編程語言

這篇文章主要介紹“怎么理解Java事件響應”,在日常操作中,相信很多人在怎么理解Java事件響應問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么理解Java事件響應”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

在 GUI中,我們看到了如何用圖形樹來組織一個圖形界面。然而,這樣的圖形界面是靜態(tài)的。我們無法互動的對該界面進行操作。GUI的圖形元素需要增加事件響應(event handling),才能得到一個動態(tài)的圖形化界面。

元素, 事件, 監(jiān)聽器

我們在 GUI一文中提到了許多圖形元素。有一些事件(Event)可能發(fā)生在這些圖形元素上,比如:

  • 點擊按鈕

  • 拖動滾動條

  • 選擇菜單

Java中的事件使用對象表示,比如ActionEvent。每個事件有作用的圖形對象,比如按鈕,滾動條,菜單。

所謂互動的GUI,是指當上面事件發(fā)生時,會有相應的動作產(chǎn)生,比如:

  • 改變顏色

  • 改變窗口內(nèi)容

  • 彈出菜單

每個動作都針對一個事件。我們將動作放在一個監(jiān)聽器(ActionListener)中,然后讓監(jiān)聽器監(jiān)視(某個圖形對象)的事件。當事件發(fā)生時,監(jiān)聽器中的動作隨之發(fā)生。

怎么理解Java事件響應 

因此,一個響應式的GUI是圖形對象、事件對象、監(jiān)聽對象三者互動的結(jié)果。我們已經(jīng)知道了如何創(chuàng)建圖形對象。我們需要給圖形對象增加監(jiān)聽器,并讓監(jiān)聽器捕捉事件。

按鈕響應

下面實現(xiàn)一個響應式的按鈕。在點擊按鈕之后,面板的顏色會改變,如下圖:

怎么理解Java事件響應

import javax.swing.*;import java.awt.event.*;import java.awt.*;public class HelloWorldSwing {    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorld");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        // Pane's layout
        Container cp = frame.getContentPane();
        cp.setLayout(new FlowLayout());        // add interactive panel to Content Pane
        cp.add(new ButtonPanel());        // show the window        frame.pack();
        frame.setVisible(true);
    }    public static void main(String[] args) {
        Runnable tr = new Runnable() {            public void run() {
                createAndShowGUI();
            }
        };
        javax.swing.SwingUtilities.invokeLater(tr);
    }
}/**
 * JPanel with Event Handling */class ButtonPanel extends JPanel
{    public ButtonPanel()
    {
        JButton yellowButton = new JButton("Yellow");
        JButton redButton = new JButton("Red");        
        this.add(yellowButton);        this.add(redButton);        
        /**
         * register ActionListeners         */
        ColorAction yellowAction = new ColorAction(Color.yellow);
        ColorAction redAction    = new ColorAction(Color.red);
        
        yellowButton.addActionListener(yellowAction);
        redButton.addActionListener(redAction);
    }    
    /**
     * ActionListener as an inner class     */
    private class ColorAction implements ActionListener
    {        public ColorAction(Color c)
        { 
            backgroundColor = c;
    }   
    
        /**
         * Actions         */
        public void actionPerformed(ActionEvent event)
        {
            setBackground(backgroundColor); // outer object, JPanel method            repaint();
        }    
        private Color backgroundColor;
    }
}

上面,我們用一個內(nèi)部類ColorAction來實施ActionListener接口。這樣做是為了讓監(jiān)聽器能更方便的調(diào)用圖形對象的成員,比如setBackground()方法。

ActionListener的actionPerformed()方法必須被覆蓋。該方法包含了事件的對應動作。該方法的參數(shù)為事件對象,即監(jiān)聽ActionEvent類型的事件。ActionEvent是一個高層的類,Java會找到圖形對象(按鈕)會發(fā)生的典型事件(點擊)作為事件。

ColorAction生成的對象即為監(jiān)聽器對象。

我們?yōu)閮蓚€按鈕JButton添加了相應的監(jiān)聽器對象。當有事件發(fā)生時,對應動作將隨之產(chǎn)生。

到此,關(guān)于“怎么理解Java事件響應”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI