Text 'Extent' property doesn't contain the correct size

前端 未结 1 677
离开以前
离开以前 2021-01-18 09:33

I want to place some text in a GUI, and I want to know the exact size the uicontrol of type \'text\' needs to be!

I\'ve found several threa

1条回答
  •  日久生厌
    2021-01-18 10:19

    Observing the Java side of things, Swing components have several methods of interest:

    • getVisibleRect
    • getSize (which, from my tests, gives comparable results to getVisibleRect)
    • getPreferredSize

    The thing is, that the "preferred size" seems to be the correct size (which you seek), whereas the size returned by get(...,'Extent'); is the visible size, which has the following meaning:

    getVisibleRect()

    Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()), and all of its ancestors' visible rectangles.

    To clarify the above: theme- and platform-specific decorations of the figure window may decrease the available space of the component, and therefore its visible size (as mentioned here).

    As a numeric example, when running with default settings and repmat('A',14), I get (on Win7, MATLAB 2015a):

    • get(u,'Extent') - [0,0,116,214]
    • jHandle.getVisibleRect - java.awt.Rectangle[x=0,y=0,width=116,height=214]
    • jHandle.getSize - java.awt.Dimension[width=116,height=214]
    • jHandle.getPreferredSize - java.awt.Dimension[width=116,height=221]

    Now the question is how to get PreferredSize (or jHandle from which it may be retreived) conveniently...

    One option, which I used, is the findjobj utility, whose usage is as simple as jHandle = findjobj(u).

    To summarize:

    1. Place findjobj in your working folder.
    2. Replace the two lines where you find textsize by this:

      v = findjobj(u); textsize = [v.getPreferredSize.getWidth v.getPreferredSize.getHeight];

    3. PROFIT.

    P.S.

    My reasoning may be flawed and understanding of Swing incorrect, however this explanation makes sense to me and more importantly - it works.

    0 讨论(0)
提交回复
热议问题