gpt4 book ai didi

java - 如何知道html标签字符串的渲染长度

转载 作者:搜寻专家 更新时间:2023-11-01 03:09:15 25 4
gpt4 key购买 nike

我正在使用 html 标签按钮 上显示参数及其值。有些按钮包含一个参数,有些按钮有 2 个或 3 个参数。 "/" 用于分隔值。名称和值之间有一个空行。但在长值的情况下,我想使用该空行来显示值,如“Table H/V”按钮。

我正在尝试使用值字符串的长度来确定是否需要空行。它不能正常工作,因为字符编号不反射(reflect)该字符串的绘图长度。按钮大小是固定的。

我在问我怎么知道什么时候我应该再有一个代表那个空行的“
”?
或者我怎么知道什么时候值字符串将被包装到另一条线?

这是我的代码:

 private static String getBtnDisplayStr(String name, String value)
{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("<html><center><b>");
sBuilder.append(name);
if(value.length() <= 12) //add one empty line for short value string
{
sBuilder.append("</center></b><br><br><font size=\"2\">");
}
else
{
sBuilder.append("</center></b><br><font size=\"2\">");
}
sBuilder.append(value);
sBuilder.append("</font></html>");

return sBuilder.toString();
}

这是按钮: enter image description here

最佳答案

  • 默认没有理由计算PreferredSize对于 Html <=3.2Swing JComponents

  • 留给LayoutManager , 例如 GridLayout 从最大元素计算屏幕尺寸

enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class HtmlAndJButton {

final String buttonText = " Whatever words, <br> but nothing wise";
final String buttonText1 = " Whatever words, <br> but nothing wise, "
+ "<br> plus 1st. line, ";
final String buttonText2 = " Whatever words, <br> but nothing wise, "
+ "<br> plus 1st. line, <br> plus 2nd. line,";
private JButton btn1 = new JButton("Toggle");
private JButton button = new JButton(buttonText);
private JButton button1 = new JButton("Toggle");
private JButton button2 = new JButton("Toggle");

public HtmlAndJButton() {
btn1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
button.setText("<html><font color=" + (button.isEnabled()
? "blue" : "red") + ">" + buttonText + "</font></html>");
button.setEnabled(!button.isEnabled());
button1.setText("<html><font color=" + (button1.isEnabled()
? "red" : "green") + ">" + buttonText1 + "</font></html>");
button1.setEnabled(!button1.isEnabled());
button2.setText("<html><font color=" + (button2.isEnabled()
? "green" : "yellow") + ">" + buttonText2 + "</font></html>");
button2.setEnabled(!button2.isEnabled());
}
});
button.setText("<html><font color=red>" + buttonText + "</font></html>");
button1.setText("<html><font color=green>" + buttonText1 + "</font></html>");
button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>");
JFrame f = new JFrame("ButtonTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(2, 2));
f.add(button);
f.add(button1);
f.add(button2);
f.add(btn1);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
HtmlAndJButton t = new HtmlAndJButton();
}
});
}
}

关于java - 如何知道html标签字符串的渲染长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979413/

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