gpt4 book ai didi

java - 获取 TextArea 边框和内部文本之间的像素

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:09 25 4
gpt4 key购买 nike

enter image description here

我希望获取并修改文本区域内包含的文本与文本区域边框之间的像素数。提供更具描述性的视觉效果:

enter image description here

其中蓝线的长度是我想要的。当我在应用程序中收到文本区域的内边距和边距时,我得到了 0px。我假设这是 TextArea 相对于外部的填充/边距,而不是相对于 Ttext 区域的内部。

非常感谢。

最佳答案

java中,要设置textareainsets,您可以使用setMargin()

public void setMargin(Insets m)

Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored). This causes a redraw of the component. A PropertyChange event ("margin") is sent to all listeners.

Parameters: m - the space between the border and the text

例如:

JTextArea txtArea = new JTextArea("Hello world!");
txtArea.setMargin( new Insets(15,15,15,15) );

更多关于insetssetMargin() .

或者另一种方法是添加一个复合边框,然后在其上设置insets,如下所示:

 JTextArea txtArea = new JTextArea("Hello world!");
Border border = BorderFactory.createLineBorder(Color.RED);
txtArea.setBorder(BorderFactory.createCompoundBorder(
border, BorderFactory.createEmptyBorder(15, 15, 15, 15))
);

参见this answer .

关于java - 获取 TextArea 边框和内部文本之间的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48274051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com