简体   繁体   English

如何检查事件是由模拟操作还是手动执行的操作触发的

[英]how to check an event is fired by simulated actions or manually performed actions

I want to know is it possible to check an event is generated by another event or manually generated event.In brief suppose we click any button, inside action performed if we write code to generate another event on some other component, how can i get to know which is generated by which.我想知道是否可以检查一个事件是由另一个事件生成的还是手动生成的事件。简而言之,假设我们单击任何按钮,如果我们编写代码在其他组件上生成另一个事件,则在内部执行的操作,我怎样才能到达知道哪个是由哪个生成的。 How can differentiate whether manual or simulated based on event generated????如何根据生成的事件区分是手动还是模拟????

public void actionPerformed(ActionEvent ae) { JComponent source =(JComponent) ae.getSource(); public void actionPerformed(ActionEvent ae) { JComponent source =(JComponent) ae.getSource(); //this is action for manually clicked event. //这是手动点击事件的动作。 if(source == button1) { //this is to generate simulated event. if(source == button1) { //这是生成模拟事件。 exitButton.doClick(); exitButton.doClick(); } //this is action for simulated event. } //这是模拟事件的动作。 else if (source == exitButton) { System.exit(0); else if (source == exitButton) { System.exit(0); } } } }

You could distinguish between your custom events and other events by checking their class type.您可以通过检查它们的类类型来区分自定义事件和其他事件。

if (catchedEvent instanceof MyCustomEvent){}

If you have several custom events, you could set eventCode in MyCustomEvent class to indicate the event meaning.如果您有多个自定义事件,您可以在 MyCustomEvent 类中设置 eventCode 来指示事件含义。

this link may help you too. 此链接也可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM