JScrollPane 在 JOptionsPane 内部表现得很有趣

JScrollPane acts funny inside of a JOptionsPane

我已经尝试将滚动视图添加到 JOptionsPane,以便信息 window 可以处理更多文本。它确实向 window 添加了一个滚动窗格。但是,它在滚动时表现得很有趣。第一个可见文本显示清晰,但当您开始滚动时,文本部分会相互重叠,直到文本区域全黑。

您是否解释了这是怎么回事,也许可以解决问题?

我的代码如下所示:

public void showInfoNoTranslation(String info) {
    frame.requestFocusInWindow();

    // create a JTextArea
    JTextArea textArea = new JTextArea(info, 6, 25);
    textArea.setEditable(false);
    textArea.setBackground(new Color(255, 255, 255, 0));
    textArea.setBorder(BorderFactory.createEmptyBorder());
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    // if (textArea.getLineCount() > 5) {
    JScrollPane scrollPane = new JScrollPane(textArea);
    JOptionPane.showMessageDialog(_frame, scrollPane, "title", JOptionPane.INFORMATION_MESSAGE);
}

调用 textArea.setOpaque(false); 而不是将其背景颜色设置为完全透明,它会起作用。


来自docs

public void setOpaque(boolean isOpaque)

If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.

The default value of this property is false for JComponent. However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree) is look-and-feel dependent.