【问题标题】:JButton.setText() in a for loop javafor循环java中的JButton.setText()
【发布时间】:2014-06-03 22:19:52
【问题描述】:

我正在尝试在 JButton 中获取两行文本

文本应该显示在
和 /> 和 just like this 之间 但由于某种原因,它在我的 for 循环中不起作用

  JButton title[] = new JButton[6];
  JButton button[] = new JButton[30];
  String[] titleText = {"World Religion", "New Title", "New Title", "New Title", "New Title", "New Title"};


  //
  for (int i=0; i<6; i++) {
     title[i] = new JButton();
     title[i].setText("<html> <br /> </html>"+titleText[i]);
     add(title[i]); 
  }

【问题讨论】:

  • 因为你没有在&lt;html&gt; 标签之间附加它?
  • 那我该怎么做呢? @Origineil
  • 不确定为什么要在文本之前或之后空行,但您应该考虑使用按钮边距作为替代方案

标签: java swing for-loop jbutton


【解决方案1】:

您需要将文本放在&lt;html&gt;&lt;/html&gt; 标记之间,如下所示:

title[i].setText("&lt;html&gt;&lt;br/&gt;" + titleText[i] + "&lt;/html&gt;");

&lt;html&gt;&lt;/html&gt; 标签之间的内容告诉按钮将其解释为 HTML 内容。

【讨论】:

    【解决方案2】:

    我想您正在尝试使用空格作为分隔符将每个标题分成几行。如果是这样的话,我认为应该这样做:

    for (int i=0; i<6; i++) {
       title[i] = new JButton();
       title[i].setText("<html>" + titleText[i].replace(" ", "<br />") + "</html>");
       add(title[i]); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 2015-08-03
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多