各种事件汇聚
1 按钮单击事件
View Code
jbutton=new JButton("改变"); jbutton.addActionListener(new Jbe()); class Jbe implements ActionListener{ public void actionPerformed(ActionEvent e){ content.setBackground(Color.red); } }
2 鼠标事件
include : mouseClickde(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) class MouseE extends JFrame{ Container content; public Container(){ content=getContentPane(); content.addMouseListener(new Mo()); content.add(jtf); } class Mo implements MouseListener{ public void mousePressed(MouseEvent e) { jtf.setText("shubiaozaijiemianzhongbeianxia"); } public void mousePressed(MouseEvent e) { jtf.setText("shubiaozaijiemianzhongbeishifang"); } public void mouseEntered(MouseEvent e) {jtf.setText("shubiaojinrudaojiemianzhong"); public void mouseExited(MouseEvent e) { jtf.setText("shubiaotuichudangqianjiemiaochuangkou"); } public void mouseClick(MouseEvent e) { jtf.setText("shubiaojinxingdanjile,qiweizhi X zuobiaowei"+e.getX()+Y zuobiaowei"+e.getY()); }
3 鼠标高级事件
content.addMouseMotionListener(new MO()); class Mo implements MouseMotionListener{ x=(int)e.getX(); y=(int)e.getY(); if(X!=-1&&Y=-1) Graphics g=getGraphics(); g.drawLine(x,y,x,y); } public void mouseMoved(MouseEvent e){}
4 选项事件
public ItenE(){ content=getContentPane(); String s[]={"C#","C++","Java","jsp"}; combox=new JComboBox(s); combo.addItemListener(new Mo()); content.add(combo); } class Mo implements ItemListener{ public void itemStateChanged(ItemEvent e){ if(combo.getSelectedIndex()==0) j1.setIcon(new ImageIcon("1.jpg")); if(combo.getSelectedIndex()==1) j1.setIcon(new ImageIcon("2.jpg")); if(combo.getSelectedIndex()==2) j1.setIcon(new ImageIcon("3.jpg")) ; if(combo.getSelectedIndex()==3) j1.setIcon(new ImageIcon("4.jpg"));
5 窗口事件
public WindowE() { addwindowListener(new Mo()); } class Mo implements WindowListener{ public void windowClosing(WindowEvent e){ jtf.append("\n 窗口正在关闭"); } public void windowOpened(WindowEvent e){ jtf.append("\n窗口打开"); } public void windowIconified(WindowEvent e){ jtf.append("\n窗口最小化"); } public void windowDeiconified(WindowEvent e){ jtf.append("\n撤销图标化"); }