简体   繁体   English

无法使鼠标回调与Java swing一起使用

[英]Can't get mouse callback to work with java swing

I would like to get eventData when the mouse is clicked or dragged along a slider. 当鼠标单击或沿滑块拖动时,我想获取eventData。 I have tried suggestions across blog posts ( http://undocumentedmatlab.com/blog/uicontrol-callbacks ) and the corresponding book. 我已经尝试过博客文章( http://undocumentedmatlab.com/blog/uicontrol-callbacks )和相应书籍中的建议。 It seems to me that I am missing something. 在我看来,我缺少了一些东西。 Here is the code: 这是代码:

function testMouse
hFig = figure('Position',[450 100 700 850]);

jSlider = javax.swing.JSlider;
[jSlider,~] = javacomponent(jSlider,[100,20,500,50]);
jbh = handle(jSlider,'CallbackProperties');

set (jbh, 'MouseDraggedCallbackData', @myCallbackFcn)

guidata(hFig, handles);

function myCallbackFcn
get (jbh, 'MouseDraggedCallbackData')
guidata(hFig, handles);

Here is the error I get when I run the code in MATLAB: 这是我在MATLAB中运行代码时遇到的错误:

Error using javahandle_withcallbacks.javax.swing.JSlider/set Changing the 'MouseDraggedCallbackData' property of javahandle_withcallbacks.javax.swing.JSlider is not allowed. 使用javahandle_withcallbacks.javax.swing.JSlider / set时发生错误不允许更改javahandle_withcallbacks.javax.swing.JSlider的'MouseDraggedCallbackData'属性。

Error in testMouse (line 8) set (jbh, 'MouseDraggedCallbackData', @myCallbackFcn) testMouse(第8行)设置错误(jbh,'MouseDraggedCallbackData',@myCallbackFcn)

I'd appreciate any suggestions. 我将不胜感激任何建议。

Thanks to my colleague for sharing a simple way to get the final value from the slider. 感谢我的同事分享了一种从滑块获取最终价值的简单方法。 Here is the code: 这是代码:

function jSlider = testMouse2

hFig = figure('Position',[450 100 700 850]);

jSlider = javax.swing.JSlider;
jSlider.setMinimum(0);
jSlider.setMaximum(20);
jSlider.setMajorTickSpacing(5);
jSlider.setMinorTickSpacing(1);
jSlider.setPaintTicks(true);
jSlider.setPaintLabels(true);
jSlider.setSnapToTicks(true);
javacomponent(jSlider,[10,70,600,45]);

end

You can call the function, and then simply use a java call to get the final value of the slider this way: 您可以调用该函数,然后简单地使用java调用以这种方式获取滑块的最终值:

slider = testMouse2;

slider.getValue

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

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