简体   繁体   English

为什么在使用 .getSource() 方法时将 ChangeEvent 对象转换为 JSlider?

[英]Why cast a ChangeEvent object to JSlider when using .getSource() Method?

The book I'm learning from is going through event listeners and I'm confused about casting a ChangeEvent object to a JSlider object when using the event.getSource() method.我正在学习的书是通过事件侦听器学习的,我对在使用 event.getSource() 方法时将 ChangeEvent 对象转换为 JSlider 对象感到困惑。 Why is this necessary?为什么这是必要的?

Secondarily, does casting or method calling take precedence?其次,强制转换或方法调用优先吗? It seems like the method call occurs first if the .getSource() method is a ChangeEvent method.如果 .getSource() 方法是 ChangeEvent 方法,则方法调用似乎首先发生。

I apologize for the single line of code but that's all the book has to offer outside of simple explicit directions to cast the ChangeEvent to a JSlider:我为单行代码表示歉意,但这就是这本书在简单明确的指示之外提供的将 ChangeEvent 转换为 JSlider 的全部内容:

"This method is called with a ChangeEvent object that can identify the slider component that changed in cale. Call the object's getSource() method and cast the object to a JSlider, as in the following statement:" (followed by the code below with no explanation why to cast it). “此方法使用 ChangeEvent 对象调用,该对象可以识别在 cal 中更改的滑块组件。调用对象的 getSource() 方法并将对象强制转换为 JSlider,如下面的语句所示:”(后面是下面的代码,没有解释为什么要投它)。

JSlider changedSlider = (JSlider) event.getSource();

That doesn't cast the event to JSlider .这不会将事件转换为JSlider It casts the source of the event to JSlider .它将事件转换为JSlider

The source of the event is the slider which has fired the event.事件源触发事件的滑块。

And the cast is probably necessary because the rest of the code needs to access the JSlider properties/methods.并且转换可能是必要的,因为其余的代码需要访问JSlider属性/方法。

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

相关问题 为什么ChangeListener的getSource()不返回用于生成事件的类型的预制对象? - Why doesn't ChangeListener's getSource() return a pre-cast object of the type used to generate an event? 内部和外部ChangeEvent(带有更改侦听器的JSlider) - Internal and External ChangeEvent (JSlider w/Change Listener) ActionEvent.getSource:如何正确转换源对象 - ActionEvent.getSource: how to cast properly the source Object 如何将getSource()转换为JTextField? - How to cast getSource() to be a JTextField? 如何在同一个类中区分stateChanged(ChangeEvent e)和多个JSlider - How to differentiate stateChanged(ChangeEvent e) with multiple JSlider in the same class 为什么在使用委托方法时没有未选中的Cast警告? - Why no Unchecked Cast warning when using delegate method? 为什么我不能调用e.getSource()的返回方法? - Why I can't call method of returns of e.getSource()? 为什么我从 MouseAdapter 的 getSource 方法中收到此错误? - Why am i getting this error from getSource method in MouseAdapter? 为什么传递给XPathExpression.evalutate方法时不需要强制转换Document对象? - Why does Document object not need to be cast when passing to XPathExpression.evalutate method? 按钮上的getSource方法遇到麻烦 - Having trouble with the getSource method on buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM