在平常swing开发中,keyevent和mouseevent是不可避免的,如input,move,click等,我想很多人都有这方面的经验,
希望大家能整理和总结一下已有的资源和代码,特别是组合事件的代码,如ctrl+鼠标左键,ctrl+shift+k,ctrl+shift+鼠标button
等等,我准备开10贴,共2000分,总结一下这方面的知识,与大家共享。
一个小小的发帖格式要求:
1,先简单说明代码用途
2,尽量是完整,并且拷贝,可运行的代码,如果有注释,更佳当然,这里我先抛砖引玉:
1,这个代码是CoreJAVA 7th Edithon中v1\v1ch8\MouseTest代码实例,对鼠标事件测试,进行了完整的举例。小而全,建议学习。/**
@version 1.31 2004-05-04
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame MousePanel panel = new MousePanel();
add(panel);
} public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}/**
A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList<Rectangle2D>();
current = null; addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; // draw all squares
for (Rectangle2D r : squares)
g2.draw(r);
} /**
Finds the first square containing a point.
@param p a point
@return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
} /**
Adds a square to the collection.
@param p the center of the square
*/
public void add(Point2D p)
{
double x = p.getX();
double y = p.getY(); current = new Rectangle2D.Double(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
} /**
Removes a square from the collection.
@param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
// the square containing the mouse cursor private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
// add a new square if the cursor isn't inside a square
current = find(event.getPoint());
if (current == null)
add(event.getPoint());
} public void mouseClicked(MouseEvent event)
{
// remove the current square if double clicked
current = find(event.getPoint());
if (current != null && event.getClickCount() >= 2)
remove(current);
}
} private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// set the mouse cursor to cross hairs if it is inside
// a rectangle if (find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
} public void mouseDragged(MouseEvent event)
{
if (current != null)
{
int x = event.getX();
int y = event.getY(); // drag the current rectangle to center it at (x, y)
current.setFrame(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
repaint();
}
}
}
}
希望大家能整理和总结一下已有的资源和代码,特别是组合事件的代码,如ctrl+鼠标左键,ctrl+shift+k,ctrl+shift+鼠标button
等等,我准备开10贴,共2000分,总结一下这方面的知识,与大家共享。
一个小小的发帖格式要求:
1,先简单说明代码用途
2,尽量是完整,并且拷贝,可运行的代码,如果有注释,更佳当然,这里我先抛砖引玉:
1,这个代码是CoreJAVA 7th Edithon中v1\v1ch8\MouseTest代码实例,对鼠标事件测试,进行了完整的举例。小而全,建议学习。/**
@version 1.31 2004-05-04
@author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame MousePanel panel = new MousePanel();
add(panel);
} public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}/**
A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList<Rectangle2D>();
current = null; addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; // draw all squares
for (Rectangle2D r : squares)
g2.draw(r);
} /**
Finds the first square containing a point.
@param p a point
@return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
} /**
Adds a square to the collection.
@param p the center of the square
*/
public void add(Point2D p)
{
double x = p.getX();
double y = p.getY(); current = new Rectangle2D.Double(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
} /**
Removes a square from the collection.
@param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
// the square containing the mouse cursor private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
// add a new square if the cursor isn't inside a square
current = find(event.getPoint());
if (current == null)
add(event.getPoint());
} public void mouseClicked(MouseEvent event)
{
// remove the current square if double clicked
current = find(event.getPoint());
if (current != null && event.getClickCount() >= 2)
remove(current);
}
} private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// set the mouse cursor to cross hairs if it is inside
// a rectangle if (find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
} public void mouseDragged(MouseEvent event)
{
if (current != null)
{
int x = event.getX();
int y = event.getY(); // drag the current rectangle to center it at (x, y)
current.setFrame(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
repaint();
}
}
}
}
如果有兴趣的话,可以去我的资源看看,这是个好书,包括源码,绝对好书.1,java事件简图:
java.awt.event
Class InputEvent
java.lang.Object
|
+-java.util.EventObject
|
+-java.awt.AWTEvent
|
+-java.awt.event.ComponentEvent
|
+-java.awt.event.InputEvent
|
+-java.awt.event.KeyEvent+MouseEvent
2,转载<Java资料:Swing中的事件处理详细资料>
监听器:ActionListener方 法:actionPerformed事 件:ActionEvent● String getActionCommand()actioncommand是控件内部的一个字符串,用于标识控件的状态。此函数可以取得控件的状态,从而决定到底该做什么。● int getModifiers()取得“产生此事件时,用户同时按下的组合键”● long getWhen()取得“产生此事件时的时间戳”事件产生原因:点击按钮,在列表框中选择内容,在文本框中点回车,计时器到期------------------------------------------------------------------监听器:AdjustmentListener方 法:adjustmentValueChanged事 件:AdjustmentEvent● Adjustable getAdjustable()adjustable是一个接口,各种滚动条都实现了这个接口。● int getAdjustmentType()取得滚动类型,共有UNIT_INCREMENT,UNIT_DECREMENT,BLOCK_INCREMENT,BLOCK_DECREMENT,TRACK共5种类型● int getValue()取得滚动滑块的当前值事件产生原因:滚动条的滚动------------------------------------------------------------------监听器:ItemListener方 法:itemStateChanged事 件:ItemEvent● Objedt getItem()取得被选中的元素。注意,返回值是Object,还应该进行强制类型转换。● ItemSelectable getItemSelectable()ItemSelectable是一个接口,代表那些包含了n个可供选择的子元素的对象。此方法返回产生此事件的对象。此方法的作用主要在于,如果一个列表框是允许多选的,那么上面的方法就不好用了,应该用此方法得到列表对象,再取得被选中的多个元素。● int getStateChange()取得选择的状态,是SELECTED还是DESELECTED事件产生原因:各种按钮的点击,列表框的选择-----------------------------------------------------------------监听器:FocusListener方 法:focusGained、focusLost事 件:FocusEvent● Component getOppositeComponent()得到“参与焦点变换的”另一个对象。如果是焦点取得事件,则返回之前拥有焦点的对象;如果是焦点失去事件,则返回得到焦点的对象。若焦点转移到另外一个应用程序,返回null● boolean isTemporary()焦点是临时性转移还是永久转移。临时性转移:用鼠标拖动窗口或者窗口变为非激活状态。其他为永久性转移。事件产生原因:各种组件的焦点变化------------------------------------------------------------------监听器:KeyListener方 法:keyPressed、keyReleased、keyTyped事 件:KeyEvent● char getKeyChar()此方法只在keytyped中有效,返回按下的字符。比如按下shift+a,返回'A'● int getKeyCode()取得键盘上按键的整数编码。请注意,是键盘上的按键,比如F1,Ctrl,Home键等等,都有相应编码。不支持组合键,也就是说“Shift+a”和“a”返回同样的内容,都是小写a的ASCII码。但是大键盘的数字键和小键盘的数字键返回内容不同。● static String getKeyModifiersText(int modifiers)将掩码转化为字符串,比如“Ctrl”,“Ctrl+Shift”等。注意它是static方法● static String getKeyText(int keyCode)返回keyCode代表的按钮的名字,比如"Home","F1"等。注意它是statci方法事件产生原因:在各种组件上敲击键盘----------------------------------------------------------------监听器:MouseListener方 法:mouseClicked、mousePressed、mouseReleasedmouseEntered、mouseExited事 件:MouseEvent● Point getLocationOnScreen()返回鼠标相对于显示屏幕的绝对坐标(java 1.6版提供)● int getXOnScreen()返回鼠标相对于显示屏幕的X的绝对坐标(java 1.6版提供)● int getYOnScreen()返回鼠标相对于显示屏幕的Y的绝对坐标(java 1.6版提供)● Point getPoint()返回鼠标相对于组件的绝对坐标● int getX()返回鼠标相对于组件的X的绝对坐标● int getY()返回鼠标相对于组件的Y的绝对坐标● void translatePoint(int x, int y)给鼠标的做用点加上一个偏移量。比如2个参数分别是10, 20,你要用鼠标画直线,当你鼠标指向100,100开始画,实际上的线是画在110,120处● int getClickCount()产生此事件时鼠标共点击了多少下(单击、双击、三击……)● int getButton()产生此事件时,鼠标的哪个键被点击了:NOBUTTON, BUTTON1, BUTTON2 或者 BUTTON3● boolean isPopupTrigger()是否能够触发一个弹出式菜单。由于各平台触发弹出式菜单的情况不一样,因此应该在mousePressed 和 mouseReleased中都检测一下事件产生原因:在任何组件上点击鼠标、将光标移动进来或出去---------------------------------------------------------------------监听器:MouseMotionListener方 法:mouseDragged、mouseMoved事 件:MouseEvent同上!事件产生原因:在任何组件上移动鼠标---------------------------------------------------------------------监听器:MouseWheelListener方 法:mouseWheelMoved事 件:MouseWheelEvent● int getScrollType()滚动的类型,是按块滚动WHEEL_BLOCK_SCROLL还是按单位滚动WHEEL_UNIT_SCROLL● int getScrollAmount()返回为相应此事件应该滚动的单位数。此方法有效的前提是按单位滚动。● int getWheelRotation()鼠标滚轮滚动过程中”咔嚓“声的数目。向上滚为负值,向下滚为正值事件产生原因:在任何组件上滚动鼠标中键--------------------------------------------------------------------监听器:WindowListener方 法:windowOpened、windowClosing、windowClosed、windowIconifiedwindowDeiconified、windowActivated、windowDeactivated事 件:WindowEvent● Window getWindow()返回发生此事件的window对象● Window getOppositeWindow()
若发生了焦点转移,返回另一个参与此事件的window对象,或者null● int getOldState()窗口变化前的状态:NORMAL、ICONIFIED、MAXIMIZED_BOTH● int getNewState()窗口变化后的状态事件产生原因:窗口发生变化----------------------------------------------------------------------监听器:WindowFocusListener方 法:windowGainedFocus、windowLostFocus事 件:WindowEvent同上!事件产生原因:窗口得到或失去焦点------------------------------------------------------------------------监听器:WindowStateListener方 法:WindowStateChanged事 件:WindowEvent同上!事件产生原因:窗口状态改变
java中对有“选择”事件的处理,比如说表格的行选择事件
当我在处理行双击的时候,我根本不希望处理行选择事件,结果我试了多种方法都没有避免,不知楼主可有好的解决方式?
import java.awt.event.*;
import java.util.*;public class TrackEvent extends JFrame
{
private HashMap<String, JTextField> h = new HashMap<String, JTextField>();
private String[] event = { "focusGained", "focusLost", "keyPressed", "keyReleased", "keyTyped",
"mouseClicked", "mouseEntered", "mouseExited", "mousePressed", "mouseReleased",
"mouseDragged", "mouseMoved" };
private MyButton b1 = new MyButton(Color.BLUE, "test1"), b2 = new MyButton(Color.RED, "test2"); class MyButton extends JButton
{
void report(String field, String msg)
{
h.get(field).setText(msg);
} FocusListener fl = new FocusListener() {
public void focusGained(FocusEvent e)
{
report("focusGained", e.paramString());
} public void focusLost(FocusEvent e)
{
report("focusLost", e.paramString());
}
};
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e)
{
report("keyPressed", e.paramString());
} public void keyReleased(KeyEvent e)
{
report("keyReleased", e.paramString());
} public void keyTyped(KeyEvent e)
{
report("keyTyped", e.paramString());
}
};
MouseListener ml = new MouseListener() {
public void mouseClicked(MouseEvent e)
{
report("mouseClicked", e.paramString());
} public void mouseEntered(MouseEvent e)
{
report("mouseEntered", e.paramString());
} public void mouseExited(MouseEvent e)
{
report("mouseExited", e.paramString());
} public void mousePressed(MouseEvent e)
{
report("mousePressed", e.paramString());
} public void mouseReleased(MouseEvent e)
{
report("mouseReleased", e.paramString());
}
};
MouseMotionListener mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e)
{
report("mouseDragged", e.paramString());
} public void mouseMoved(MouseEvent e)
{
report("mouseMoved", e.paramString());
}
}; public MyButton(Color color, String label)
{
super(label);
setBackground(color);
addFocusListener(fl);
addKeyListener(kl);
addMouseListener(ml);
addMouseMotionListener(mml);
}
} public TrackEvent()
{
setLayout(new GridLayout(event.length + 1, 2));
for (String evt : event)
{
JTextField t = new JTextField();
t.setEditable(false);
add(new JLabel(evt, JLabel.RIGHT));
add(t);
h.put(evt, t);
}
add(b1);
add(b2);
} public static void main(String[] args)
{
JFrame f = new TrackEvent();
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700, 500);
f.setVisible(true);
}
}