当鼠标在MATLAB中传递静态文本时更改鼠标光标

时间:2014-08-10 12:59:31

标签: java matlab user-interface text matlab-guide

当鼠标在static text上时,我希望鼠标光标变化(不点击它,只改变静态文本区域)。我在undocumented-matlab中找到了这些java鳕鱼:

jb = javax.swing.JButton;
jb.setCursor(java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

我将这些代码复制到CreareFcn的{​​{1}}和ButtonDownFcn中,但没有任何更改,一切都是默认的。我该怎么办呢?我应该把这些代码放在static text

感谢。

1 个答案:

答案 0 :(得分:3)

您可以使用鼠标移动侦听器实现此目的,如下所示:

function init()
  %// Initialize the figure with a listener:
  h = figure('WindowButtonMotionFcn',@windowMotion,'Pos',[400,400,200,200]);
  %// Add a "static" text label:
  col = get(h,'color');
  lbl = uicontrol('Style','text', 'Pos',[10,160,120,20], ...
                  'Background',col, 'HorizontalAlignment','left');
  drawnow;
  setptr(gcf, 'fleur'); %// Optional, set default pointer.

function windowMotion(varargin)
    cursor_pos = get(h,'CurrentPoint');
    set(lbl,'String',sprintf('Mouse position: %d, %d',cursor_pos));
    drawnow;

    pos = get(lbl,'position'); %// This doesn't need to happen every time, 
                               %// it's here for the sake of demonstration.
    if (cursor_pos(1)>pos(1) && cursor_pos(1)<pos(1)+pos(3)) && ...
       (cursor_pos(2)>pos(2) && cursor_pos(2)<pos(2)+pos(4)) 
       setptr(gcf, 'hand'); %// Change to this cursor if pointer is inside
                            %// the element.
    else
       setptr(gcf, 'fleur'); %//otherwise (re)change to default
    end

  end

end

请注意,这不是if,而是switch-case类型的选择(如果您希望光标针对不同的UI元素进行不同的更改)。

此代码基于this post on UndocumentedMatlab。您可以在MATLAB here中找到有关鼠标指针修改的更多信息。

修改

要在GUIDE中自动创建此回调,请参阅下面的图片。请注意,您需要将lbl更改为handles.tag_of_statictxth更改为当前数字的句柄(通常由gcf或{{1}返回})。 How to create the callback automatically in GUIDE