我在 JLabel 中进行 HTML 渲染时遇到问题。我不太确定会发生什么,但是当我第一次调用 setFadedVisible 时,它似乎计算了未呈现 html 的大小,绘制它,呈现 html,重新计算并重新绘制 agian。在正确重新粉刷之前,我只能瞥见错误的尺寸,但这足以让我恼火。当我将非 HTML 文本放入 textlabel 时,它不会发生(因为我猜它是单行)。你能帮我解决这个问题吗?
public class LevelEndLayeredPane extends FadedLayer {
...
private void showMessageAndSetVisible(String mes) {
mes = mes.replaceAll("\n", "<br>");
mes = "<html><center>" + mes + "</center></html>";
textLabel.setText(mes);
setFadedVisible(true);
}
...
}
public class FadedLayer extends JLayeredPane{
private JComponent fadedLayer;
private JComponent groundLayer;
private JComponent topLayer;
public FadedLayer(JComponent groundLayer) {
setLayout(new OverlayLayout(this));
setOpaque(false);
createFadedLayer();
add(groundLayer, Integer.valueOf(0));
add(fadedLayer, Integer.valueOf(1));
fadedLayer.setVisible(false);
}
public FadedLayer(JComponent groundLayer, JComponent topLayer) {
this(groundLayer);
setTopLayer(topLayer);
}
public void setFadedVisible(boolean show){
fadedLayer.setVisible(show);
}
public boolean isFadedVisible() {
return fadedLayer.isVisible();
}
...
}