等宽 html 格式化 jlabel
Monospaced html formatting jlabel
我正在为处理矩阵的程序创建输出 window。它应该打印出预制命令以及矩阵的格式化版本。但是我在对齐方面遇到了问题。我知道 String.format
有效,因为我有一个 toString()
方法可以正常工作。
注意第二行和第三行的间距不正确。这是因为 100.00 已完全填充格式化字符串,而 0.00 需要额外的空格来填充字符串(请参阅 toHtml())。我相信这与 HTML 的显示方式有关,但我不确定。我的猜测是零后面的空格没有正确显示或正在合并。
这里是涉及到的方法。
public String toHtml(int dec)
{
String[] colors = {"#C0C0C0","#FFFFFF"};
String f = "%-"+(getLongestValue()+dec+1)+"."+dec+"f";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+String.format(f, contents[r][c])+"</span>";
}
res += "<p>";
}
return res;
}
创建要显示的 HTML 文本。方法 getLongestValue() returns 数组中小数点前任何数字的最大长度 'contents'。
和
newOutput("New Matrix ["+name+"]<br>"+m.toHtml());
public void newOutput(String s)
{
JLabel l = new JLabel("<html>"+s+"<br></html>");
l.setFont(new Font("Monospaced",1,18));
jPanel1.add(l);
}
将标签添加到输出 window
另外,这里提供toString()方法供参考
public String toString()
{
String f = "%-"+(getLongestValue()+3)+".2f ";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += String.format(f, contents[r][c]);
}
res += "\n";
}
return res;
}
通过 toString() 输出矩阵
toString Output
更极端的版本
在这种情况下,程序应该发现最大值为 -15 或 -20,并将格式长度的大小设置为 6(长度为 3,小数位数为 2,小数位数为 1)但相反,除了我提到的两个值之外,似乎没有任何值遵循该格式。
这是上一个例子的 toString() 的输出
toString() output
这修复了它,空格未正确表示为等宽
public String toHtml(int dec)
{
String[] colors = {"#C0C0C0","#FFFFFF"};
String f = "%-"+(getLongestValue()+dec+2)+"."+dec+"f";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+
String.format(f, contents[r][c]).replaceAll("\s", ((char)160)+"")+"</span>";
}
res += "<p>";
}
return res+"";
}
我正在为处理矩阵的程序创建输出 window。它应该打印出预制命令以及矩阵的格式化版本。但是我在对齐方面遇到了问题。我知道 String.format
有效,因为我有一个 toString()
方法可以正常工作。
注意第二行和第三行的间距不正确。这是因为 100.00 已完全填充格式化字符串,而 0.00 需要额外的空格来填充字符串(请参阅 toHtml())。我相信这与 HTML 的显示方式有关,但我不确定。我的猜测是零后面的空格没有正确显示或正在合并。
这里是涉及到的方法。
public String toHtml(int dec)
{
String[] colors = {"#C0C0C0","#FFFFFF"};
String f = "%-"+(getLongestValue()+dec+1)+"."+dec+"f";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+String.format(f, contents[r][c])+"</span>";
}
res += "<p>";
}
return res;
}
创建要显示的 HTML 文本。方法 getLongestValue() returns 数组中小数点前任何数字的最大长度 'contents'。
和
newOutput("New Matrix ["+name+"]<br>"+m.toHtml());
public void newOutput(String s)
{
JLabel l = new JLabel("<html>"+s+"<br></html>");
l.setFont(new Font("Monospaced",1,18));
jPanel1.add(l);
}
将标签添加到输出 window
另外,这里提供toString()方法供参考
public String toString()
{
String f = "%-"+(getLongestValue()+3)+".2f ";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += String.format(f, contents[r][c]);
}
res += "\n";
}
return res;
}
通过 toString() 输出矩阵 toString Output
更极端的版本
在这种情况下,程序应该发现最大值为 -15 或 -20,并将格式长度的大小设置为 6(长度为 3,小数位数为 2,小数位数为 1)但相反,除了我提到的两个值之外,似乎没有任何值遵循该格式。
这是上一个例子的 toString() 的输出 toString() output
这修复了它,空格未正确表示为等宽
public String toHtml(int dec)
{
String[] colors = {"#C0C0C0","#FFFFFF"};
String f = "%-"+(getLongestValue()+dec+2)+"."+dec+"f";
String res = "";
for(int r = 0;r<rows;r++)
{
for(int c = 0;c<columns;c++)
{
res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+
String.format(f, contents[r][c]).replaceAll("\s", ((char)160)+"")+"</span>";
}
res += "<p>";
}
return res+"";
}