- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一种不同的方式来创建多色 JLabel。(多色 = 不同前景色的文本部分)
到目前为止我找到的唯一解决方案(也是我目前使用的)是在 html 中设置文本。但是我遇到了问题...
当 LayoutManager 决定 JLabel 应该变窄时,在 JLabel 中使用纯文本,文本会被裁剪,并添加“...”。(例如:“My Long Text”-> 变成:“My Long T...”)
对于 JLabel 内的 html,文本被包裹在空格字符的某处,将其余部分留在可绘制区域之外,并且由于 JLabel 的高度不变而不可见。(例如:“我的长文” -> 变成:“我的长文”)
在我的例子中,JLabel 在 JTable 中呈现,它由用户调整大小,更不用说在不同的屏幕分辨率下了。
我尝试在 html 中添加一个“nowrap”属性或一个“”-tag,但它看起来像被忽略了。
留给我 - 我认为 - 一个解决方案:自己绘制标签。或不?有什么建议么?例子?
谢谢。
这是一个非常简单的例子:尝试水平调整此面板的大小,看看两个 JLabel 的...中的文本会发生什么......
(没有提示用户,第二个JLabel的文本不是完整的内容)
-> 在示例中,JLabel 的高度发生变化,但在框架的 JTable 中呈现时,行的高度没有变化,我不希望它发生变化。如果不使用 HTML,它也不会改变高度...
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MultiJLabel
extends JFrame
{
public MultiJLabel()
{
super("Multi-colored JLabel test");
JPanel pnl = new JPanel();
pnl.setLayout(new BorderLayout());
pnl.add(new JLabel("This is a test of My Long Text"), BorderLayout.NORTH);
pnl.add(new JLabel("<html>This is a test of <font color='#ffbebe'>My Long Text</font></html>"), BorderLayout.SOUTH);
this.getContentPane().add(pnl);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new MultiJLabel();
}
}
最佳答案
谢谢大家的评论,但我没有耐心,创建了我自己的 JLabel。我知道这可能是一个糟糕的编程版本,但它对我有用......您可以通过更改上面的示例来测试它:
JMultiColorLabel lbl = new JMultiColorLabel("This is a test of My Long Text");
lbl.setColors(new int[]{10,15}, new Color[]{Color.RED,Color.BLUE});
lbl.setPreferredSize(new Dimension(200,20));
pnl.add(lbl, BorderLayout.SOUTH);
并使用这个类:
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.util.HashMap;
import javax.swing.JLabel;
public class JMultiColorLabel
extends JLabel
{
private static final String STRING_OVERFLOW = "...";
private HashMap<Integer, Color> extraColors = new HashMap<Integer, Color>();
public JMultiColorLabel(String text)
{
super(text);
}
public void setColors(int[] indices, Color[] colors)
{
for (int i = 0; i < indices.length; i++)
this.extraColors.put(indices[i], colors[i]);
}
protected void paintComponent(Graphics g)
{
// Get text-contents of Label
String text = this.getText();
// No text in the JLabel? -> No risk: super
if (text == null || text.length() == 0)
{
super.paintComponent(g);
return;
}
// Content Array of characters to paint
char[] chars = text.toCharArray();
// Draw nice and smooth
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Draw background
if (this.isOpaque())
{
g2d.setColor(this.getBackground());
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
}
// FontMetrics to calculate widths and height
FontMetrics fm = g2d.getFontMetrics();
// Available space
Insets ins = this.getInsets();
int maxSpace = this.getWidth()-(ins.left+ins.right);
boolean overflow = (fm.stringWidth(text) > maxSpace);
// Starting offset
int offset = ins.left+1;
// The start Color is the default
g2d.setColor(this.getForeground());
// Loop over characters
for (int i = 0; i < chars.length; i++)
{
// Switch Color?
if (this.extraColors.containsKey(i))
g2d.setColor(this.extraColors.get(i));
// Check if we still have enough room for this character
if (overflow && offset >= (maxSpace-fm.stringWidth(STRING_OVERFLOW)))
{ // Draw overflow and stop painting
g2d.drawString(STRING_OVERFLOW, offset, (fm.getHeight()+ins.top));
return;
}
else // We have the space -> Draw the character
g2d.drawString(String.valueOf(chars[i]), offset, (fm.getHeight()+ins.top));
// Move cursor to the next horizontal position
offset += fm.charWidth(chars[i]);
}
}
}
关于java - 多色 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46542496/
我想要一种不同的方式来创建多色 JLabel。(多色 = 不同前景色的文本部分) 到目前为止我找到的唯一解决方案(也是我目前使用的)是在 html 中设置文本。但是我遇到了问题... 当 Layout
是否可以在 UILabel 中创建多色文本?即,如果我的文字是: “敏捷的棕色狐狸” q 和 b 为蓝色,其余文本为黑色? 我感觉我必须使用 UIWebView 并以 HTML 形式呈现文本才能完成此
我想在我的 Richtextbox 多色中制作一行文本。我尝试了网络上提供的各种实现,并阅读了 SelectedText 和其他主题,但似乎无法让它按照我想要的方式工作。 这是我到目前为止所拥有的 R
我想做的是在导航栏中获得多种颜色?当前栏如图所示 导航栏 标志 导航栏 除了我似乎只能得到一种颜色 这是我目前拥有的代码的链接:http://jsfiddle.net/ju47nkgu/
我想渲染一行文本,其中一些单词以不同的颜色突出显示。 理想情况下,我会使用带有反应的跨度标签来完成此操作。 想知道如何使用 React-native 做同样的事情? 最佳答案 您可以通过使用嵌套文本组
Google map 标记可以采用复杂的 svg 路径作为其图标,如下所示: var baseSvg = { x1 : "m 0,0 l45,0 l 190,225 l -45,0 l -19
在我的应用程序中,我在里面实现了一个 SwiftyCharts,我需要根据 JSON 值中的性别选择显示多种颜色。在我的例子中,我得到了一个值来自 json,仅返回多种颜色。但是我无法显示任何人都可以
我正在编写并行代码来枚举大量 CSV 文件,每个文件都包含历史股票数据(超过 6500 个代码),并计算每只股票是否已达到历史最高点。 我已经实现了一个线程池和 TThread 后代类,以在线程之间平
我是一名优秀的程序员,十分优秀!