导航:首页 > 编程语言 > 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相关的资料

热点内容
java计算12 浏览:249
大金空调摆动式压缩机 浏览:453
新的云服务器如何设置首页 浏览:687
javastring字符位置 浏览:196
银河麒麟字体库存在哪个文件夹 浏览:956
魔兽加丁服务器的航空叫什么 浏览:152
花冠改装案例哪个app多 浏览:515
成绩单app哪个好用 浏览:140
北美程序员vs国内程序员 浏览:181
php解析xml文档 浏览:121
石墨文档APP怎么横屏 浏览:185
墙主钢筋加密和非加密怎么看 浏览:144
金山区文件夹封套定制 浏览:708
soho程序员 浏览:672
java字节截取 浏览:526
php提交作业 浏览:815
房产还没解压可以办理赠予吗 浏览:224
java毫秒转分钟 浏览:753
模式识别中文pdf 浏览:774
c语言平均数字编译错误 浏览:171