我需要有选择地设置段落的一部分格式,而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());
}
}