1

我需要有选择地格式化段落的一部分,而 JEditorPane 是我能找到的唯一可以为我做这件事的东西。但是当我在编辑器窗格中有一条环绕的行并且在 SpringLayout 中它下方有一个元素绑定时,编辑器窗格不会显示其所有文本:它只显示与没有文本环绕的行一样多的行。如下面的示例所示,您放置在编辑器窗格下方的任何元素都具有正确的间距,如果文本被包裹,并且如果您仔细观察或尝试复制/粘贴内容,您会看到文本就在背景后面.

当你去调整它的大小时,事情真的很有趣。起初看起来调整窗口大小可以解决问题,但后来我注意到了一些事情。如果您一次更改一个像素的窗口大小,当文本换行到新行或停止换行到旧行时,编辑器窗格将保持其旧显示大小。它下面的元素移动到它应该在的位置,但是在您再次调整窗口大小之前,窗格不会正确打印。就像窗格正在重新绘制,然后查看是否需要调整大小的工作在之后完成,但从未显示。所以编辑器窗格总是会落后一步。

package main.java.com.characterCreator.GUI;

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

import static javax.swing.SpringLayout.*;

public class JavaPlz extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(()  -> new JavaPlz().buildMainScreen());
    }

    public JavaPlz() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(1080, 720);
        // because I'm edgy, and once you go black, you never go back.
        setBackground(Color.BLACK);
    }

    public void buildMainScreen() {
        setContentPane(new JPanel());
        SpringLayout layout = new SpringLayout();
        getContentPane().setLayout(layout);

        JLabel topLabel = new JLabel("This is the first thing");
        getContentPane().add(topLabel);
        layout.putConstraint(NORTH, topLabel, 10, NORTH, getContentPane());
        layout.putConstraint(WEST, topLabel, 10, WEST, getContentPane());
        layout.putConstraint(EAST, topLabel, -10, EAST, getContentPane());

        JEditorPane problematicDisplay = new JEditorPane("text/html", null);
        problematicDisplay.setText("<b>Bold stuff:</b> Not bold stuff<br><b>More Bold stuff</b> Lots of not bold stuff. Like, a copious, gargantuan, insanely way too much amount of not bold stuff. So much, even, that it will not all fit on the same line and will be forced to spill over onto the next, causing the problem I have that I am trying to solve.<br><b>Second to last line</b><br><i>jazz hands</i>");
        getContentPane().add(problematicDisplay);
        layout.putConstraint(NORTH, problematicDisplay, 10, SOUTH, topLabel);
        layout.putConstraint(WEST, problematicDisplay, 10, WEST, getContentPane());
        layout.putConstraint(EAST, problematicDisplay, -10, EAST, getContentPane());

        JLabel bottomLabel = new JLabel("This is the last thing.");
        getContentPane().add(bottomLabel);
        layout.putConstraint(NORTH, bottomLabel, 10, SOUTH, problematicDisplay);
        layout.putConstraint(WEST, bottomLabel, 10, WEST, getContentPane());
        layout.putConstraint(EAST, bottomLabel, -10, EAST, getContentPane());
    }
}
4

0 回答 0