Document事件

这个事件有点特别,需要用getDocument()返回到自己所维护的文档,然后就可以添加监视器

(textArea1.getDocument).addDocumentListener(DocumentListener listen)

DocumentListener接口有三个方法

public void changedUpdate(DocumentEvent e)
public void removeUpdate(DocumentEvent e)
public void insertUpdate(DocumentEvent e)

单词排序代码、

public class test{

    public static void main(String args[]){
Component window1=new Component();
window1.setBounds(40,40,300,200);
}
} class Component extends JFrame{
JTextArea textArea1,textArea2;
JMenu menu;
JMenuBar menuBar;
JMenuItem copyItem,cutItem,pasteItem;
Component(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
textArea1=new JTextArea(6,10);
textArea2=new JTextArea(6,10);
// textArea1.setLineWrap(true);//文本自动换行 w
// textArea1.setWrapStyleWord(true);//以单词为界自动换行
// textArea2.setLineWrap(true);//文本自动换行
// textArea2.setWrapStyleWord(true);//以单词为界自动换行
setLayout(new FlowLayout());
copyItem=new JMenuItem("复制(c)");
cutItem=new JMenuItem("剪贴(t)");
pasteItem=new JMenuItem("粘贴(v)");
menuBar=new JMenuBar();
menu=new JMenu("编辑");
menuBar.add(menu);
setJMenuBar(menuBar);//这样写才会变一条菜单
menu.add(copyItem);
menu.add(cutItem);
menu.add(pasteItem);
copyItem.setAccelerator(KeyStroke.getKeyStroke('c'));//设置快捷键
cutItem.setAccelerator(KeyStroke.getKeyStroke('t'));
pasteItem.setAccelerator(KeyStroke.getKeyStroke('v'));
copyItem.setActionCommand("copy");
copyItem.setActionCommand("cut");
copyItem.setActionCommand("paste");
add(new JScrollPane(textArea1));
add(new JScrollPane(textArea2));
DListener dListener1=new DListener(textArea1,textArea2);
(textArea1.getDocument()).addDocumentListener(dListener1);
AListener aListener1=new AListener(textArea1,textArea2);
copyItem.addActionListener(aListener1);
cutItem.addActionListener(aListener1);
pasteItem.addActionListener(aListener1);
}
} class DListener implements DocumentListener{
JTextArea textArea1,textArea2;
DListener(JTextArea a,JTextArea b){
textArea1=a;
textArea2=b;
}
public void changedUpdate(DocumentEvent e){
String regex="[\\s\\d\\p{Punct}]+";
String[] word=(textArea1.getText()).split(regex);
Arrays.sort(word);
textArea2.setText(null);
for(String a:word){
textArea2.append(a+",");
}
}
public void removeUpdate(DocumentEvent e){//这个多了两个抽象方法,现在把他们设置为和上面一样
changedUpdate(e);
}
public void insertUpdate(DocumentEvent e){
changedUpdate(e);
}
} class AListener implements ActionListener{//快捷键功能并没有debug到
JTextArea textArea1,textArea2;
AListener(JTextArea a,JTextArea b){
textArea1=a;
textArea2=b;
}
public void actionPerformed(ActionEvent e){
String a=e.getActionCommand();
if(a.equals("copy"))
textArea2.copy();
else if(a.equals("cut"))
textArea2.cut();
else if(a.equals("paste"))
textArea1.paste();
}
}

MouseEvent事件

MouseEvent有五个方法

getX()//获得x坐标
getY()//获得y坐标
getModiifiers()//获得鼠标的左键或者右键,左右键分别用InputEvent类中的BUTTON_MASK和BUTTON3_MASK
getClickCount()//获得点击次数
getSource()//获得发生鼠标事件的事件源

他的监视器MouseLIstener接口有五个方法

mousePressed(MouseEvent e)//按下鼠标发生鼠标事件
mouseReleased(MouseEvent e)//释放鼠标发生鼠标事件
mouseEntered(MouseEvent e)//进入组件发生鼠标事件
mouseExited(MouseEvent e)//离开组件发生鼠标事件
mouseClicked(MouseEvent e)//单击主键发生鼠标事件

一个鼠标位置的例子

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class test{ public static void main(String args[]){
Component window1=new Component();
window1.setBounds(40,40,300,200);
}
} class Component extends JFrame{
JTextField text1;
JTextArea textArea1;
JButton button1;
Component(){
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
button1=new JButton("click");
textArea1=new JTextArea(5,20);
text1=new JTextField(8);
add(button1);
add(text1);
add(new JScrollPane(textArea1));
MousePolice mouseListener1=new MousePolice(textArea1);
button1.addMouseListener(mouseListener1);
text1.addMouseListener(mouseListener1);
addMouseListener(mouseListener1);
}
} class MousePolice implements MouseListener{
JTextArea textArea1;
MousePolice(JTextArea a){
textArea1=a;
}
public void mousePressed(MouseEvent e){
textArea1.append("按下鼠标位置:"+e.getX()+","+e.getY()+"\n");
}
public void mouseReleased(MouseEvent e){
textArea1.append("放开鼠标位置:"+e.getX()+","+e.getY()+"\n");
}
public void mouseEntered(MouseEvent e){
if(e.getSource() instanceof JButton)
textArea1.append("进入按钮,位置:"+e.getX()+","+e.getY()+"\n");
if(e.getSource() instanceof JTextField)
textArea1.append("进入文本框,位置:"+e.getX()+","+e.getY()+"\n");
if(e.getSource() instanceof JFrame)
textArea1.append("进入窗口,位置:"+e.getX()+","+e.getY()+"\n");
}
public void mouseExited(MouseEvent e){
textArea1.append("退出位置: "+e.getX()+","+e.getY()+"\n");
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()>=2){
textArea1.setText("鼠标连击:"+e.getX()+","+e.getY()+"\n");
}
}
}

java事件处理2的更多相关文章

  1. 从零开始理解JAVA事件处理机制(1)

    “事件”这个词已经被滥用了.正因为“事件”的被滥用,很多人在用到事件的时候不求甚解,依样画葫芦,导致学习工作了很多年,还是不清楚什么是事件处理器.什么是事件持有者.所以,如果你对于Event这个词还是 ...

  2. 从零开始理解JAVA事件处理机制(2)

    第一节中的示例过于简单<从零开始理解JAVA事件处理机制(1)>,简单到让大家觉得这样的代码简直毫无用处.但是没办法,我们要继续写这毫无用处的代码,然后引出下一阶段真正有益的代码. 一:事 ...

  3. 从零开始理解JAVA事件处理机制(3)

    我们连续写了两小节的教师-学生的例子,必然觉得无聊死了,这样的例子我们就是玩上100遍,还是不知道该怎么写真实的代码.那从本节开始,我们开始往真实代码上面去靠拢. 事件最容易理解的例子是鼠标事件:我们 ...

  4. java 事件处理

    Java事件处理机制:EventObject类作为描述事件信息的事件信息类的基类,由EventListener接口派生新的接口或类来作为事件接收方的类,再定义事件源类. 事件信息类的构造方法必须含有事 ...

  5. java事件处理机制

    java中的事件机制的参与者有3种角色:   1.event object:就是事件产生时具体的"事件",用于listener的相应的方法之中,作为参数,一般存在与listerne ...

  6. [转]Java事件处理机制- 事件监听器的四种实现方式

    原文来自http://stefan321.iteye.com/blog/345221 自身类作为事件监听器 外部类作为事件监听器 匿名内部类作为事件监听器 内部类作为事件监听器 自身类作为事件监听器: ...

  7. Java事件处理机制(深入理解)

    本文是关于Java事件处理机制的梳理,以及有重点的介绍一些注意点,至于基础的概念啥的不多赘述. 一.Java事件处理机制初步介绍(看图理解) 根据下图,结合生活实际,可以得知监护人可以有多个,坏人对小 ...

  8. Java事件处理机制- 事件监听器的四种实现方式

    自身类作为事件监听器 外部类作为事件监听器 匿名内部类作为事件监听器 内部类作为事件监听器 自身类作为事件监听器: import javax.swing.*; import java.awt.*; i ...

  9. java 事件处理机制:按下上下左右键控制小球的运动

    /** * 加深对事件处理机制的理解 * 通过上下左右键来控制一个小球的位置 */package com.test3;import java.awt.*;import javax.swing.*;im ...

  10. java事件处理机制(自定义事件)

    java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...

随机推荐

  1. 终端编写c程序

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:JackAlan链接:http://www.zhihu.com/question/21483375/answer/322672 ...

  2. Solr4.8.0源码分析(4)之Eclipse Solr调试环境搭建

    Solr4.8.0源码分析(4)之Eclipse Solr调试环境搭建 由于公司里的Solr调试都是用远程jpda进行的,但是家里只有一台电脑所以不能jpda进行调试,这是因为jpda的端口冲突.所以 ...

  3. EventSystem

    Unity5.0 EventSystem事件系统的详细说明 一.EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动创建了对象EventSys ...

  4. Linux定时运行与开机运行任务

    http://os.51cto.com/art/200805/75144.htm at命令与crontab命令 http://os.51cto.com/art/201007/211874.htm ht ...

  5. 【HDOJ】5131 Song Jiang's rank list

    STL的使用. /* 5131 */ #include <iostream> #include <map> #include <cstdio> #include & ...

  6. cf590B Chip 'n Dale Rescue Rangers

    B. Chip 'n Dale Rescue Rangers time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. How to make onActivityResult get called on Nested Fragment

    One of the common problem we always meet in the world of Fragment is: although we could callstartAct ...

  8. 解读sample2

    说明 理解被测试代码 理解测试代码 组织关系 检查字符串比较结果的断言 说明 被测试代码文件 sample2.h.sample2.cc 测试代码文件 sample2_unittest.cc 官网上如是 ...

  9. Oracle sqlplus 语法

    目录: 0. FREFACE 1. 执行一个SQL脚本文件 2. 对当前的输入进行编辑 3. 重新运行上一次运行的sql语句 4. 将显示的内容输出到指定文件 5. 关闭spool输出 6.显示一个表 ...

  10. Android Dialog AlertDialog

    1.普通的对话框 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...