【问题标题】:HTML in a JLabel not displaying correctlyJLabel 中的 HTML 无法正确显示
【发布时间】:2014-09-01 11:53:05
【问题描述】:

我使用 BorderLayout 的 JPanel 运行良好,直到我决定添加一个在其文本中包含 HTML 的 JLabel。我已经花了几个小时与遇到相同问题但没有解决的人仔细检查。当我注释掉处理这个 JLabel 的行时,一切正常。我尝试改用 JTextArea,但遇到了同样的问题。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Gui extends JFrame implements KeyListener {

JFrame jFrame;
JPanel backgroundPanel;
JLabel multiLineTextLabel;
JLabel statusLabel;

int xValue = 1000;

Gui() {
    jFrame = new JFrame("Frame Name");
    jFrame.setSize(1200, 600);
    jFrame.setLayout(new BorderLayout());
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    backgroundPanel = new JPanel();
    backgroundPanel.setBackground(Color.BLACK);

    String htmlString = "<html>Word1<br>Word2</html>";
    multiLineTextLabel = new JLabel(htmlString);

    statusLabel = new JLabel("Some Text");

    jFrame.add(backgroundPanel, BorderLayout.CENTER);
    jFrame.add(multiLineTextLabel, BorderLayout.EAST);
    jFrame.add(statusLabel, BorderLayout.SOUTH);
    jFrame.setFocusable(true);
    jFrame.addKeyListener(this);

}

public void keyTyped(KeyEvent e)
{
    //Won't be using
}

public void keyPressed(KeyEvent e)
{
    int keyCode = e.getKeyCode();

    if(keyCode == KeyEvent.VK_LEFT)
    {
        xValue -= 20;
        statusLabel.setText("You are clicking left");
    }
    else if(keyCode == KeyEvent.VK_RIGHT)
    {
        xValue += 20;
        statusLabel.setText("You are clicking right"); //You are "pressing" right
    }
    else if(keyCode == KeyEvent.VK_A)
    {
        statusLabel.setText("You are clicking A"); //You are "pressing" A
    }
    else if(keyCode == KeyEvent.VK_S)
    {
        statusLabel.setText("You are clicking S");
    }
    else if(keyCode == KeyEvent.VK_ESCAPE)
    {
        jFrame.setVisible(false); 
        jFrame.dispose();
    }
    else if(keyCode == KeyEvent.VK_P)
    {
        String positionString = new String("Your position/XValue = " + xValue);

        statusLabel.setText(positionString);
    }

}

public void keyReleased(KeyEvent e)
{
    //Won't be using
}

public static void main(String[] args)
{
    Gui go = new Gui();
}
}

【问题讨论】:

  • 不幸的是,这在 JLabels 内部不起作用。
  • 私有组件控制KeyPanel是做什么的;做 ?您添加到布局中但没有制作任何像 JPanel 或其他任何对象?你有这个标签 multiLineTextLabel 但你没有将它添加到你的布局中?
  • 当我将 multiLineTextLabel 添加到您的布局时,它可以完美运行
  • 对不起,我忘记编辑了。它只是应该说 multiLineTextLabel 。
  • 你现在有什么问题,因为它对我很有效?

标签: java swing jlabel


【解决方案1】:

您的代码执行正确,但我猜您不知道点击按钮和按下按钮之间的区别,因为在您的 keyPressed 函数中您说 “您正在点击正确”感觉。您应该将其更改为 您正在推动向右箭头

您只需按下右、左、S 或 A 按钮即可查看您的代码正在执行您想要的操作。

另外一点,在您没有将multiLineTextLabel 添加到您的JFrame 之前,但是当我告诉您时,一切都按您的预期进行

关于JTextArea的一点,你可以这样做

 String htmlString = "<html>Word1\n<br>Word2</html>";
 JTextArea jta = new JTextArea(htmlString);
 jFrame.add(jta, BorderLayout.EAST);

因此您的 html 代码可以按您的意愿工作

【讨论】:

    【解决方案2】:

    解决方案是将框架设置在最后可见。我不小心设置了可见,然后添加了所有组件。

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2021-02-23
      • 2015-04-08
      • 2023-03-29
      • 1970-01-01
      • 2018-07-11
      • 2015-05-09
      • 2011-07-04
      • 2014-11-19
      相关资源
      最近更新 更多