文字“范围"属性未包含正确的大小 [英] Text 'Extent' property doesn't contain the correct size

查看:98
本文介绍了文字“范围"属性未包含正确的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在GUI中放置一些文本,我想知道类型'text'uicontrol的确切大小!

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!

我发现了几个线程来说明可以使用包含相同文本的'text'对象的'Extent'属性来完成此操作,请参见示例:

I've found several threads explaining that this can be done using the 'Extent' property of a 'text' object containing the same text, see example:

function form = tempfunc(txt,font,fontsize)
    if nargin <3
        fontsize = 10;
    end
    if nargin < 2
        font = 'courier';
    end
    f = figure('Visible','off','Units','pixels');
    u = uicontrol(f,'Style','text','Units','pixels','String',txt,'FontName',font,'FontSize',fontsize);
    textsize = get(u,'Extent');
    textsize = textsize(3:4);
    close(f);

    form = figure('Units','pixels');
    uicontrol(form,'Style','text','Units','pixels','String',txt,'FontName',font,'FontSize',fontsize,'Position',[5,5,textsize]);
end

我的问题是现在这不起作用. 当我用tempfunc(repmat('A',14));运行以上命令时,将得到以下图形窗口:

My problem is now that this doesn't work. When I run the above with tempfunc(repmat('A',14)); I get the following figure window:

从图像中可以看到,在textsize变量中提取的文本的高度太小了!

As you can see from the image the height of the text extracted in the textsize variable is too small!

请注意,这是在运行Matlab R2014a的Windows计算机上运行程序时得到的结果.后来我在运行Matlab R2013b的Linux机器上运行了完全相同的代码,在那台机器上我得到了想要的结果.

Note that this is the result I got when I ran the program on my Windows computer running Matlab R2014a. I later ran the exact same code on a Linux machine running Matlab R2013b, and on that machine I got the result I wanted.

我正在制作的GUI应该(希望)能够在任何计算机上运行,​​但是现在我真的迷茫了如何继续制作可在任何计算机上使用的版本,所以请帮帮我!

The GUI I am making should (hopefully) be able to run on any computer, but right now I am really at a loss on how I should proceed to make a version that works on any machine, so please help me!

我试图在另一台运行Matlab R2011b(而不是R2014a)的Windows 7计算机(这次是Ultimate版,而不是Enterprise版)上运行相同的代码,但是它仍然产生错误的文本框高度-但是这次是文本框太高-查看图片:

I tried to run the same code on another Windows 7 machine (this time Ultimate edition instead of my Enterprise edition) running Matlab R2011b (instead of my R2014a), it still produced the wrong height of the text box - but this time the text box was too high - see image:

我终于安装了R2014b,但遗憾的是它没有帮助! 我有一张类似的图片:

I finally got R2014b installed, but sadly it did not help! I got a similar looking picture:

我还试图查看屏幕分辨率的不同选择是否有所不同-它们没有改变.

I also tried to see if different choices of resolution of my screen made any difference - they did not.

我注意到,不同的字体会产生不同的高度错误,例如默认字体(MS Sans Serif)产生的文本框太高(随着添加更多行,此高度错误也会增加)-在Linux上,对于我尝试过的所有字体,我都得到了正确的结果.

I've noticed that different fonts yield different errors in the height, e.g. the default font (MS Sans Serif) yields a text box that is too high (and this error in height also grows as more lines get added) - On Linux however I got the correct result for all the fonts I tried.

但实际上,我最感兴趣的情况是使用Courier字体的情况,因为我需要一种等宽字体.

But really the case I am most interested in is the case using the courier font, since I need a monospaced font for my purpose.

推荐答案

观察事物的Java方面,

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

问题是,首选大小"似乎是正确的大小(您要查找的),而get(...,'Extent');返回的大小是可见大小,其含义如下: :

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()

getVisibleRect()

返回组件的可见矩形"-该组件的可见矩形,新Rectangle(0,0,getWidth(),getHeight())和其祖先所有可见矩形的交集

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).

作为一个数值示例,使用默认设置和repmat('A',14)运行时,我得到了(在Win7上,MATLAB 2015a):

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]
  • 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]

现在的问题是如何方便地获取PreferredSize(或jHandle,可以从中获取它)...

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

我使用的一个选项是 findjobj 实用程序,其用法与jHandle = findjobj(u)一样简单.

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

总结:

  1. findjobj 放入您的工作文件夹中.
  2. 用此位置替换找到textsize的两行:

  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];

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

  • 利润.

  • PROFIT.

    P.S.

    我的推理可能有缺陷,并且对Swing的理解不正确,但是这种解释对我来说更有意义,更重要的是-它起作用.

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

    这篇关于文字“范围"属性未包含正确的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆