1 Debug提示
import java.awt.*;
import javax.swing.*;
/**
* @version 1.14 2015-08-20
* @author Cay Horstmann
*/
public class EventTracerTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new EventTracerFrame();
frame.setTitle("EventTracerTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class EventTracerFrame extends JFrame
{
public EventTracerFrame()
{
// add a slider and a button
add(new JSlider(), BorderLayout.NORTH);
add(new JButton("Test"), BorderLayout.SOUTH);
// trap all events of components inside the frame
EventTracer tracer = new EventTracer();
tracer.add(this);
pack();
}
}
import java.awt.*;
import java.beans.*;
import java.lang.reflect.*;
/**
* @version 1.31 2004-05-10
* @author Cay Horstmann
*/
public class EventTracer
{
private InvocationHandler handler;
public EventTracer()
{
// the handler for all event proxies
handler = new InvocationHandler()
{
public Object invoke(Object proxy, Method method, Object[] args)
{
System.out.println(method + ":" + args[0]);
return null;
}
};
}
/**
* Adds event tracers for all events to which this component and its children can listen
* @param c a component
*/
public void add(Component c)
{
try
{
// get all events to which this component can listen
BeanInfo info = Introspector.getBeanInfo(c.getClass());
EventSetDescriptor[] eventSets = info.getEventSetDescriptors();
for (EventSetDescriptor eventSet : eventSets)
addListener(c, eventSet);
}
catch (IntrospectionException e)
{
}
// ok not to add listeners if exception is thrown
if (c instanceof Container)
{
// get all children and call add recursively
for (Component comp : ((Container) c).getComponents())
add(comp);
}
}
/**
* Add a listener to the given event set
* @param c a component
* @param eventSet a descriptor of a listener interface
*/
public void addListener(Component c, EventSetDescriptor eventSet)
{
// make proxy object for this listener type and route all calls to the handler
Object proxy = Proxy.newProxyInstance(null, new Class[] { eventSet.getListenerType() },
handler);
// add the proxy as a listener to the component
Method addListenerMethod = eventSet.getAddListenerMethod();
try
{
addListenerMethod.invoke(c, proxy);
}
catch (ReflectiveOperationException e)
{
}
// ok not to add listener if exception is thrown
}
}
2 让AWT Robot做事
Robot类可以模拟键盘和鼠标事件。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
/**
* @version 1.05 2015-08-20
* @author Cay Horstmann
*/
public class RobotTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
// make frame with a button panel
ButtonFrame frame = new ButtonFrame();
frame.setTitle("ButtonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
// attach a robot to the screen device
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = environment.getDefaultScreenDevice();
try
{
final Robot robot = new Robot(screen);
robot.waitForIdle();
new Thread()
{
public void run()
{
runTest(robot);
};
}.start();
}
catch (AWTException e)
{
e.printStackTrace();
}
}
/**
* Runs a sample test procedure
* @param robot the robot attached to the screen device
*/
public static void runTest(Robot robot)
{
// simulate a space bar press
robot.keyPress(' ');
robot.keyRelease(' ');
// simulate a tab key followed by a space
robot.delay(2000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(' ');
robot.keyRelease(' ');
// simulate a mouse click over the rightmost button
robot.delay(2000);
robot.mouseMove(220, 40);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// capture the screen and show the resulting image
robot.delay(2000);
BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));
ImageFrame frame = new ImageFrame(image);
frame.setVisible(true);
}
}
/**
* A frame to display a captured image
*/
class ImageFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
/**
* @param image the image to display
*/
public ImageFrame(Image image)
{
setTitle("Capture");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JLabel label = new JLabel(new ImageIcon(image));
add(label);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A frame with a button panel
*/
public class ButtonFrame extends JFrame
{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
public ButtonFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// create buttons
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
buttonPanel = new JPanel();
// add buttons to panel
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
// add panel to frame
add(buttonPanel);
// create button actions
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
// associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
/**
* An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
}
}
本文介绍了一个Java程序中实现事件监听的方法,并展示了如何使用AWT Robot类来模拟键盘和鼠标事件。通过创建自定义事件监听器,可以捕获并打印组件上的所有事件,同时利用Robot类实现自动化操作。
4万+

被折叠的 条评论
为什么被折叠?



