導航:首頁 > 編程語言 > actioneventjava

actioneventjava

發布時間:2022-04-27 18:12:42

java中的actionlistener是什麼

actionlistener字面上理解就是動作監聽器。
它是一個介面,在實現此介面的類中,你可以給需要關注其動作的組件(如Button)添加監聽器(addActionListener(this);),之後在事件處理方法(public void actionPerformed(ActionEvent event){})中,對每個事件進行不同處理。
給你個例子吧,是我自己寫的一個記事本:
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JFrame implements ActionListener{
int width = 500,height = 400;
JPanel panel;
JMenuBar bar;
JMenu fileMenu,editMenu,helpMenu;
JMenuItem 打開O,新建N,保存S,另存A,剪切T,復制C,粘貼P,關於A;
JTextArea textArea = null;
File tempFile = null;
public MainClass(){ //構造方法
setTitle("TextEdit");
setSize(width,height);
panel = (JPanel)getContentPane();
bar = new JMenuBar();
fileMenu = new JMenu("文件F");
fileMenu.setMnemonic('F');
editMenu = new JMenu("編輯E");
editMenu.setMnemonic('E');
helpMenu = new JMenu("幫助H");
helpMenu.setMnemonic('H');
打開O = new JMenuItem("打開O");
打開O.setMnemonic('O');
新建N = new JMenuItem("新建N");
新建N.setMnemonic('N');
保存S = new JMenuItem("保存S");
保存S.setMnemonic('S');
另存A = new JMenuItem("另存A");
另存A.setMnemonic('A');
剪切T = new JMenuItem("剪切C");
剪切T.setMnemonic('t');
復制C = new JMenuItem("復制C");
復制C.setMnemonic('C');
粘貼P = new JMenuItem("粘貼P");
粘貼P.setMnemonic('P');
關於A = new JMenuItem("關於A");
關於A.setMnemonic('A');
fileMenu.add(打開O);
fileMenu.add(新建N);
fileMenu.add(保存S);
fileMenu.add(另存A);
bar.add(fileMenu);
editMenu.add(剪切T);
editMenu.add(復制C);
editMenu.add(粘貼P);
bar.add(editMenu);
helpMenu.add(關於A);
bar.add(helpMenu);
textArea = new JTextArea();
panel.add("North",bar);
panel.add("Center", textArea);
打開O.addActionListener(this);
新建N.addActionListener(this);
保存S.addActionListener(this);
另存A.addActionListener(this);
剪切T.addActionListener(this);
復制C.addActionListener(this);
粘貼P.addActionListener(this);
關於A.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event){ //處理事件
if(event.getSource() == 打開O){ //處理打開
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(panel);
tempFile = jfc.getSelectedFile();
FileReader fis;
try {
fis = new FileReader(tempFile);
textArea.read(fis,null);
textArea.setEditable(true);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 新建N){ //處理新建
textArea.setEditable(true);
textArea.setText(null);
}
if(event.getSource() == 保存S){ //處理保存
if(tempFile == null){
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
else{
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
}
if(event.getSource() == 另存A){ //處理另存
JFileChooser jfc = new JFileChooser();
jfc.showSaveDialog(panel);
tempFile = jfc.getSelectedFile();
try{
FileWriter fos = new FileWriter(tempFile);
textArea.write(fos);
}catch(Exception ex){ex.printStackTrace();}
}
if(event.getSource() == 剪切T){ //處理剪切
textArea.cut();
}
if(event.getSource() == 復制C){ //處理復制
textArea.();
}
if(event.getSource() == 粘貼P){ //處理粘貼
textArea.paste();
}
if(event.getSource() == 關於A){ //處理關於
textArea.setText("Manifest-Version: 1.0\n" +
"Created-By: Libra_JL\n" +
"QQ: 254791521\n");
textArea.setEditable(false);
panel.validate();
validate();
}
}
public static void main(String []args){ //主函數
new MainClass();
}
}

❷ java中actionevent都包括什麼事件

詳細查閱JAVA的,不知道你看懂么...

Overview Package Class Use Tree Deprecated Index Help
JavaTM 2 Platform
Std. Ed. v1.4.2
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

--------------------------------------------------------------------------------

java.awt
Class AWTEvent
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ActionEvent, AdjustmentEvent, AncestorEvent, ComponentEvent, HierarchyEvent, InputMethodEvent, InternalFrameEvent, InvocationEvent, ItemEvent, TextEvent

--------------------------------------------------------------------------------

public abstract class AWTEvent
extends EventObject
The root event class for all AWT events. This class and its subclasses supercede the original java.awt.Event class. Subclasses of this root AWTEvent class defined outside of the java.awt.event package should define event ID values greater than the value defined by RESERVED_ID_MAX.

The event masks defined in this class are needed by Component subclasses which are using Component.enableEvents() to select for event types not selected by registered listeners. If a listener is registered on a component, the appropriate event mask is already set internally by the component.

The masks are also used to specify to which types of events an AWTEventListener should listen. The masks are bitwise-ORed together and passed to Toolkit.addAWTEventListener.

Since:
1.1
See Also:
Component.enableEvents(long), Toolkit.addAWTEventListener(java.awt.event.AWTEventListener, long), ActionEvent, AdjustmentEvent, ComponentEvent, ContainerEvent, FocusEvent, InputMethodEvent, InvocationEvent, ItemEvent, HierarchyEvent, KeyEvent, MouseEvent, MouseWheelEvent, PaintEvent, TextEvent, WindowEvent, Serialized Form

--------------------------------------------------------------------------------

Field Summary
static long ACTION_EVENT_MASK
The event mask for selecting action events.
static long ADJUSTMENT_EVENT_MASK
The event mask for selecting adjustment events.
static long COMPONENT_EVENT_MASK
The event mask for selecting component events.
protected boolean consumed
Controls whether or not the event is sent back down to the peer once the source has processed it - false means it's sent to the peer; true means it's not.
static long CONTAINER_EVENT_MASK
The event mask for selecting container events.
static long FOCUS_EVENT_MASK
The event mask for selecting focus events.
static long HIERARCHY_BOUNDS_EVENT_MASK
The event mask for selecting hierarchy bounds events.
static long HIERARCHY_EVENT_MASK
The event mask for selecting hierarchy events.
protected int id
The event's id.
static long INPUT_METHOD_EVENT_MASK
The event mask for selecting input method events.
static long INVOCATION_EVENT_MASK
The event mask for selecting invocation events.
static long ITEM_EVENT_MASK
The event mask for selecting item events.
static long KEY_EVENT_MASK
The event mask for selecting key events.
static long MOUSE_EVENT_MASK
The event mask for selecting mouse events.
static long MOUSE_MOTION_EVENT_MASK
The event mask for selecting mouse motion events.
static long MOUSE_WHEEL_EVENT_MASK
The event mask for selecting mouse wheel events.
static long PAINT_EVENT_MASK
The event mask for selecting paint events.
static int RESERVED_ID_MAX
The maximum value for reserved AWT event IDs.
static long TEXT_EVENT_MASK
The event mask for selecting text events.
static long WINDOW_EVENT_MASK
The event mask for selecting window events.
static long WINDOW_FOCUS_EVENT_MASK
The event mask for selecting window focus events.
static long WINDOW_STATE_EVENT_MASK
The event mask for selecting window state events.
Fields inherited from class java.util.EventObject
source
Constructor Summary
AWTEvent(Event event)
Constructs an AWTEvent object from the parameters of a 1.0-style event.
AWTEvent(Object source, int id)
Constructs an AWTEvent object with the specified source object and type.
Method Summary
protected void consume()
Consumes this event, if this event can be consumed.
int getID()
Returns the event type.
protected boolean isConsumed()
Returns whether this event has been consumed.
String paramString()
Returns a string representing the state of this Event.
void setSource(Object newSource)
Retargets an event to a new source.
String toString()
Returns a String representation of this object.
Methods inherited from class java.util.EventObject
getSource
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail

id
protected int idThe event's id.

See Also:
getID(), AWTEvent(java.lang.Object, int)

--------------------------------------------------------------------------------

consumed
protected boolean consumedControls whether or not the event is sent back down to the peer once the source has processed it - false means it's sent to the peer; true means it's not. Semantic events always have a 'true' value since they were generated by the peer in response to a low-level event.

See Also:
consume(), isConsumed()

--------------------------------------------------------------------------------

COMPONENT_EVENT_MASK
public static final long COMPONENT_EVENT_MASKThe event mask for selecting component events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

CONTAINER_EVENT_MASK
public static final long CONTAINER_EVENT_MASKThe event mask for selecting container events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

FOCUS_EVENT_MASK
public static final long FOCUS_EVENT_MASKThe event mask for selecting focus events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

KEY_EVENT_MASK
public static final long KEY_EVENT_MASKThe event mask for selecting key events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

MOUSE_EVENT_MASK
public static final long MOUSE_EVENT_MASKThe event mask for selecting mouse events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

MOUSE_MOTION_EVENT_MASK
public static final long MOUSE_MOTION_EVENT_MASKThe event mask for selecting mouse motion events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

WINDOW_EVENT_MASK
public static final long WINDOW_EVENT_MASKThe event mask for selecting window events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

ACTION_EVENT_MASK
public static final long ACTION_EVENT_MASKThe event mask for selecting action events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

ADJUSTMENT_EVENT_MASK
public static final long ADJUSTMENT_EVENT_MASKThe event mask for selecting adjustment events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

ITEM_EVENT_MASK
public static final long ITEM_EVENT_MASKThe event mask for selecting item events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

TEXT_EVENT_MASK
public static final long TEXT_EVENT_MASKThe event mask for selecting text events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

INPUT_METHOD_EVENT_MASK
public static final long INPUT_METHOD_EVENT_MASKThe event mask for selecting input method events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

PAINT_EVENT_MASK
public static final long PAINT_EVENT_MASKThe event mask for selecting paint events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

INVOCATION_EVENT_MASK
public static final long INVOCATION_EVENT_MASKThe event mask for selecting invocation events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

HIERARCHY_EVENT_MASK
public static final long HIERARCHY_EVENT_MASKThe event mask for selecting hierarchy events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

HIERARCHY_BOUNDS_EVENT_MASK
public static final long HIERARCHY_BOUNDS_EVENT_MASKThe event mask for selecting hierarchy bounds events.

See Also:
Constant Field Values

--------------------------------------------------------------------------------

MOUSE_WHEEL_EVENT_MASK
public static final long MOUSE_WHEEL_EVENT_MASKThe event mask for selecting mouse wheel events.

Since:
1.4
See Also:
Constant Field Values

--------------------------------------------------------------------------------

WINDOW_STATE_EVENT_MASK
public static final long WINDOW_STATE_EVENT_MASKThe event mask for selecting window state events.

Since:
1.4
See Also:
Constant Field Values

--------------------------------------------------------------------------------

WINDOW_FOCUS_EVENT_MASK
public static final long WINDOW_FOCUS_EVENT_MASKThe event mask for selecting window focus events.

Since:
1.4
See Also:
Constant Field Values

--------------------------------------------------------------------------------

RESERVED_ID_MAX
public static final int RESERVED_ID_MAXThe maximum value for reserved AWT event IDs. Programs defining their own event IDs should use IDs greater than this value.

See Also:
Constant Field Values
Constructor Detail

AWTEvent
public AWTEvent(Event event)Constructs an AWTEvent object from the parameters of a 1.0-style event.

Parameters:
event - the old-style event

--------------------------------------------------------------------------------

AWTEvent
public AWTEvent(Object source,
int id)Constructs an AWTEvent object with the specified source object and type.

Parameters:
source - the object where the event originated
Method Detail

setSource
public void setSource(Object newSource)Retargets an event to a new source. This method is typically used to retarget an event to a lightweight child Component of the original heavyweight source.
This method is intended to be used only by event targeting subsystems, such as client-defined KeyboardFocusManagers. It is not for general client use.

Parameters:
newSource - the new Object to which the event should be dispatched

--------------------------------------------------------------------------------

getID
public int getID()Returns the event type.

--------------------------------------------------------------------------------

toString
public String toString()Returns a String representation of this object.

Overrides:
toString in class EventObject
Returns:
A a String representation of this EventObject.

--------------------------------------------------------------------------------

paramString
public String paramString()Returns a string representing the state of this Event. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns:
a string representation of this event

--------------------------------------------------------------------------------

consume
protected void consume()Consumes this event, if this event can be consumed. Only low-level, system events can be consumed

--------------------------------------------------------------------------------

isConsumed
protected boolean isConsumed()Returns whether this event has been consumed.

--------------------------------------------------------------------------------
Overview Package Class Use Tree Deprecated Index Help
JavaTM 2 Platform
Std. Ed. v1.4.2
PREV CLASS NEXT CLASS FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

--------------------------------------------------------------------------------
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

❸ java中的ActionEvent都到底用了干什麼用的

兄弟,可以去sun那裡下一點 example 看看。ActionEvent貌似是個abstract class, 可以相應一些awt控制項的事件,比如 button的click,drop down的select等,繼承它後 並且重寫 他的回調方法可以去實現你要的效果。

❹ 在java里actionPerformed(ActionEvent e)是做什麼用的有什麼功能。

這是一個事件監聽器,可以處理類似單擊滑鼠時觸發的事件
ActionEvent就是一個事件類,傳入的e就是該事件的對象

❺ java中的ActionEvent都到底是什麼意思

兄弟,可以去sun那裡下一點
example
看看。actionevent貌似是個abstract
class,
可以相應一些awt控制項的事件,比如
button的click,drop
down的select等,繼承它後
並且重寫
他的回調方法可以去實現你要的效果。

❻ 如何用java實現「點擊一個按鈕,出現另一個按鈕」的功能

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;@SuppressWarnings("serial")
public class ButtonFrame extends JFrame {
public ButtonFrame() {
buttonPanel = new JPanel();
setSize(500, 500);
makeButton(); add(buttonPanel);
}

public void makeButton() {
JButton buttonFather = new JButton("I'm father");
buttonFather.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
addButton(event);
}
});
buttonPanel.add(buttonFather);
}

private void addButton(ActionEvent event) {
JButton buttonSon = new JButton("I'm son");
buttonPanel.add(buttonSon);
this.validate();
}
private JPanel buttonPanel; public static void main(String[] args) {
new ButtonFrame().setVisible(true);
}
}

❼ java 有關actionevent

static修飾方法表示靜態方法,屬性歸類所有。非靜態成員歸每個對象,也就是說static修飾的只有一個副本,而非static修飾的每個對象都有一個

Java規定靜態方法只能訪問靜態變數,非靜態方法可以訪問所有變數

main方法是static修飾,故在其中使用的變數,要麼是局部變數,要麼就必須是靜態變數

❽ 在java中發生事件後ActionEvent創建的什麼對象,該對象和事件源有什麼區別

創建ActionEvent對象啊,這個對象可以獲取到事件源,事件源是一個控制項,比如按鈕,這只是一個事件對象

❾ java程序:這個程序為什麼函數的參數actionPerformed(ActionEvent ev

可以,這是一個匿名內部類裡面定義的一個方法,方法裡面的參數有些時候可以作為區分不同方法的作用,這個算是方法重構。至於參數是否用到,對程序而已並無影響,一般不這樣。這個有可能是繼承那個類的實現方法的格式,那這樣的話這個方法就算是方法覆蓋(重寫)。

❿ java的事件處理是什麼

打個比方:如果用戶用滑鼠單擊了按鈕對象button,則該按鈕button就是事件源,而java運行時系統會生成ActionEvent類的對象actionE,該對象中描述了該單擊事件發生時的一些信息,然後,事件處理者對象將接收由java運行時系統傳遞過來的事件對象actionE並進行相應的處理。
由於同一個事件源上可能發生多種事件,因此java採取了授權處理機制,事件源可以把在其自身所有可能發生的事件分別授權給不同的事件處理者來處理。比如在Canvas對象上既可能發生滑鼠事件,也可能發生鍵盤事件,該Canvas對象就可以授權給事件處理者一來處理滑鼠事件,同時授權給事件處理者二來處理鍵盤事件。有時也將事件處理者稱為監聽器,主要原因也在於監聽器時刻監聽著事件源上所有發生的事件類型,一旦該事件類型與自己所負責處理的事件類型一致,就馬上進行處理。授權模型把事件的處理委託給外部的處理實體進行處理,實現了將事件源和監聽器分開的機制。事件處理者(監聽器)通常是一個類,該類如果要能夠處理某種類型的事件,就必須實現與該事件類型相對的介面。例如例5.9中類ButtonHandler之所以能夠處理ActionEvent事件,原因在於它實現了與ActionEvent事件對應的介面ActionListener。每個事件類都有一個與之相對應的介面。將事件源對象和事件處理器(事件監聽器)分開。
使用授權處理模型進行事件處理的一般方法歸納如下:
1.對於某種類型的事件XXXEvent, 要想接收並處理這類事件,必須定義相應的事件監聽器類,該類需要實現與該事件相對應的介面XXXListener;
2.事件源實例化以後,必須進行授權,注冊該類事件的監聽器,使用addXXXListener(XXXListener ) 方法來注冊監聽器。
這是我的一點個人見解,希望對你有幫助,加油。

閱讀全文

與actioneventjava相關的資料

熱點內容
壓縮機異音影響製冷嗎 瀏覽:711
德斯蘭壓縮機 瀏覽:490
程序員太極拳視頻 瀏覽:531
網上購買加密鎖 瀏覽:825
安卓為什麼軟體要隱私 瀏覽:83
虛擬主機管理源碼 瀏覽:811
java圖形圖像 瀏覽:230
單片機輸出口電平 瀏覽:486
java配置資料庫連接 瀏覽:479
java多態的體現 瀏覽:554
java的split分隔符 瀏覽:128
跪著敲代碼的程序員 瀏覽:238
web和php有什麼區別 瀏覽:120
加密的電梯卡怎麼復制蘋果手機 瀏覽:218
warez壓縮 瀏覽:137
黑馬程序員培訓機構官網天津 瀏覽:904
mainjavasrc 瀏覽:58
如何買伺服器挖礦 瀏覽:292
php批量上傳文件夾 瀏覽:560
安卓固件怎麼更新 瀏覽:169