Java - 在 setBorder 中设置字体/颜色
有没有办法可以为 Text1 和 Text2 文本定义自己的字体和颜色方案 在 setBorder 方法中。 java新手,在SUN教程中找不到它。
我的代码
//Create Positions Table
JPanel SpreadPanel = new JPanel();
SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1 Text 2"));
问候 西蒙
Is there a way I can define my own font and color schemes for Text1 AND Text2 text
within the setBorder method. New to java and cannot find it in the SUN tutorials.
My code
//Create Positions Table
JPanel SpreadPanel = new JPanel();
SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1 Text 2"));
Regards
Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
第一个参数 null 或另一个边框(对于复合边框)
您显示的第二个参数文本
中文本的位置
第 3 个和第 4 个参数的对齐方式以及第 2 个参数和第 4 个参数
第 5 个参数是设置字体和颜色的两个参数
the first parameter null or another border (for compound borders)
2nd param text that you're displaying
3rd and 4th param justification and location of the text from param 2
4th param
and 5th param are the two to set font and color
如果您希望同一字符串中的每个字符串(例如
Text1
和Text2
)使用不同的字体和颜色TitledBorder
,您可能需要扩展AbstractBorder
并覆盖paintBorder()
。现有的实现对于单个标题只有一种字体和一种颜色。If you want a different font and color for each of the strings (e.g.
Text1
andText2
) in the sameTitledBorder
, you may be need to extendAbstractBorder
and overridepaintBorder()
. The existing implementation only has one font and one color for a single title.文字字体:
文字颜色:
Text Font:
Text Color:
如果您是 Java 和 Swing 的新手,那么用于执行此操作的 JavaDocs 有点让人不知所措。 BorderFactory 的 JavaDocs 位于:http:// download.oracle.com/javase/1.5.0/docs/api/javax/swing/BorderFactory.html
下面是将无衬线字体的文本设置为红色的示例:
The JavaDocs for doing this are somewhat overwhelming if you are new to Java and Swing. The JavaDocs for BorderFactory are here: http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/BorderFactory.html
Here's an example of making the text red in a sans serif font:
我知道这是一个老问题。
我想我想复活它,因为也许有人知道如何解决这个问题。我只有“部分解决方案”。
我很快就实现了你想要的边界。我重用了 Java 提供的功能,即 swing 组件中 HTML 的解释。
一切都很顺利,对于纯文本或 HTML 文本,边框绘制得很好,但您尝试为文本使用不同字体大小的情况除外。
我不知道如何解决这个问题。但我对解决方案非常感兴趣。
我知道该过程是在计算 textLengthInPixels 变量时将每个字符串以其自己的字体大小的宽度相加。
问题是我不知道如何获取它,也许是从视图中获取它,但不知道如何获取?
I know it is an old question.
Thought I would like to resurrect it as maybe someone knows how to solve this problem. I have only 'a partial solution'.
I have very quickly implemented the border which does what you want. I have reused what Java gives, i.e. interpretation of HTML in swing components.
All works sweet, the border is painted fine for a plain or HTML text, with exception for a situation where you are trying to have different font sizes for the texts.
I do not have idea how to solve this issue. But I am very much interested in a solution.
I know the procedure would be to sum up width of each string in its own font size when calculating the textLengthInPixels variable.
The problem is that I do not know how to get it, maybe from the View, but no idea how?
试试这个:
Try this: