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()+" "); } public void mouseReleased(MouseEvent e){ textArea1.append("放开鼠标位置:"+e.getX()+","+e.getY()+" "); } public void mouseEntered(MouseEvent e){ if(e.getSource() instanceof JButton) textArea1.append("进入按钮,位置:"+e.getX()+","+e.getY()+" "); if(e.getSource() instanceof JTextField) textArea1.append("进入文本框,位置:"+e.getX()+","+e.getY()+" "); if(e.getSource() instanceof JFrame) textArea1.append("进入窗口,位置:"+e.getX()+","+e.getY()+" "); } public void mouseExited(MouseEvent e){ textArea1.append("退出位置: "+e.getX()+","+e.getY()+" "); } public void mouseClicked(MouseEvent e){ if(e.getClickCount()>=2){ textArea1.setText("鼠标连击:"+e.getX()+","+e.getY()+" "); } } }