【问题标题】:JScrollPane acts funny inside of a JOptionsPaneJScrollPane 在 JOptionPane 内部的行为很有趣
【发布时间】:2015-11-20 13:16:47
【问题描述】:

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

你有解释这是怎么回事,也许是解决问题的方法吗?

我的代码如下所示:

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);
}

【问题讨论】:

  • _frame 是当前窗口框架

标签: java swing jscrollpane jtextarea joptionpane


【解决方案1】:

调用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.

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 2012-11-26
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2015-09-12
    • 2017-02-07
    • 2010-11-26
    相关资源
    最近更新 更多