Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中HTML化字符串_Java_Html_Swing_Newline_Jlabel - Fatal编程技术网

如何在Java中HTML化字符串

如何在Java中HTML化字符串,java,html,swing,newline,jlabel,Java,Html,Swing,Newline,Jlabel,我有一个带有JLabel的JFrame,当我的程序运行时,我想通过setText()更改JLabel上的文本。我很清楚,为了在JLabel中创建新行,必须在希望创建新行的字符串周围放置标记,然后在标记中,还必须放置以创建新行 然而,我遇到了一个轻微的打嗝。这是我的MVCE import javax.swing.JFrame; import javax.swing.JLabel; public class GearsWindow extends JFrame { public int

我有一个带有
JLabel
JFrame
,当我的程序运行时,我想通过
setText()
更改
JLabel
上的文本。我很清楚,为了在
JLabel
中创建新行,必须在希望创建新行的字符串周围放置
标记,然后在标记中,还必须放置

以创建新行

然而,我遇到了一个轻微的打嗝。这是我的MVCE

import javax.swing.JFrame;
import javax.swing.JLabel;

public class GearsWindow extends JFrame 
{

   public int x = 200;
   public int y = 200; 
   public int speed = 5;

   public String running;

   public JLabel l;

   public GearsWindow()
   {

      setSize(300, 200);
      setLocation(x, y);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      l = new JLabel();
      add(l);

      running = "RUNNING";

      updateLabel();

      setVisible(true);

   }

   public static void main(String[] args)
   {

      GearsWindow gw = new GearsWindow();

   }

   public void updateLabel()
   {

      String result = running + "<html><br ></html>" + speedStatus();

      l.setText(result);

   }

   public String speedStatus()
   {

      String result = "Speed - " + speed;

      return result;

   }

}
现在,我知道简单的方法是在每个
字符串
周围放置
标记,然而,由于我的程序将具有大量复杂性,这将变得非常困难。我只展示了一个MVCE,这个程序比这个要大得多

是否可以使用方法或其他方法对字符串进行HTML格式化?

public void updateLabel()
public void updateLabel()
   {

      String result = "<html>" + running + "<br >" + speedStatus()+"</html>";

      l.setText(result);

   }
{ 字符串结果=“+running+”
“+speedStatus()+”; l、 setText(结果); }
如果您希望这样准备每个字符串:

public String HTMLize(String temp)
{
    return "<html>"+temp+"</html>";
}
publicstringhtmlize(stringtemp)
{
返回“+temp+”;
}

像这样的通用方法应该有帮助:

public String toHtml(String strPlain){
    if(strPlain==null || strPlain.trim().length()==0) return "";

    String res = strPlain.replaceAll("\\n","<BR/>");
    res = "<html>"+res+"</html>";
    return res;
}
publicstringtohtml(stringstrplain){
if(strPlain==null | | strPlain.trim().length()==0)返回“”;
字符串res=strPlain.replaceAll(\\n“,“
”); res=”“+res+”; 返回res; }
Pahaha,原谅我,在speed-5之前,re不应该是一个换行符,显然stackoverflow会监听我的代码,但不是我的实际应用程序不要把运行放在htmlRUNNING之外,speed-5让我疯狂,但看起来你并没有关闭
标记-你有两个开始标记。String running=“running”;字符串结果=“+running+”
“+speedStatus()+”;jLabel1.setText(结果);以什么方式?它仍然显示标签吗?是的,我得到了完全相同的东西,我仍然看到html标签和br所以输出是
运行
速度-5
?啊,这一个也是很有帮助的!非常感谢。
public String toHtml(String strPlain){
    if(strPlain==null || strPlain.trim().length()==0) return "";

    String res = strPlain.replaceAll("\\n","<BR/>");
    res = "<html>"+res+"</html>";
    return res;
}