gpt4 book ai didi

java - 打破字符串以在 jlabel 中使用 html

转载 作者:行者123 更新时间:2023-12-01 13:16:50 24 4
gpt4 key购买 nike

这是我的代码:

JLabel JL_Output = new JLabel(Reponse);
JL_Output.setBorder(BorderFactory.createTitledBorder("Output: "));
JL_Output.setPreferredSize(new Dimension(450, 175));
JL_Output.setBackground(Color.red);

所以我的问题是我的响应字符串太长而无法在一行中查看。所以我想将 html 与/br nut 一起使用,但我无法集成这些应答器,因为我无法在字符串中“输入”:/

我想以4行显示,像这样响应值:

Enter the angle offset
Units : [degrees]
Range : [-090<+090]
Current : [+000] >> ERROR

最佳答案

假设Response是一个字符串,你可以这样做:

String formattedResponse = Response.replaceAll("\\]", "\\]<br />");
JLabel JL_Output = new JLabel(formattedResponse);
...

这应该用闭括号和断线语句替换所有右括号,这应该可以满足您的需要。

编辑:根据您的评论,您可以使用类似下面的代码来执行您需要的操作:

String formattedResponse = Response.replaceAll("\\s{4,}", "<br />");
JLabel JL_Output = new JLabel("<html>" + formattedResponse + "</html>");

上面的代码将匹配 Response 的任何部分由 4 个或更多连续空白字符组成的字符串,并将其替换为 <br /> .

关于java - 打破字符串以在 jlabel 中使用 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22406019/

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