简体   繁体   English

文本窗格颜色错误

[英]Text Pane color error

For some reason my text pane is white. 出于某种原因,我的文本窗格是白色的。 It's a text pane (output) nested inside aj scrollpane. 它是嵌套在aj scrollpane中的文本窗格(输出)。

        jScrollPane1.setBackground(new java.awt.Color(0, 0, 0));
        jScrollPane1.setBorder(null);
        jScrollPane1.setOpaque(false);

        output.setEditable(false);
        output.setBackground(new java.awt.Color(0, 0, 0));
        output.setBorder(null);
        output.setCaretColor(new java.awt.Color(255, 255, 255));
        output.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        output.setHighlighter(null);
        output.setOpaque(false);
        jScrollPane1.setViewportView(output);

在此输入图像描述

That's the only code affecting it. 这是影响它的唯一代码。 I don't know why this is happening, but I want the text pane to be black. 我不知道为什么会这样,但我希望文本窗格是黑色的。

First of all, setting the background color of the JTextPane should be more than enough 首先,设置JTextPane的背景颜色应该足够了

回来了

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BlackTextPane {

    public static void main(String[] args) {
        new BlackTextPane();
    }

    public BlackTextPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextPane tp = new JTextPane();
                tp.setForeground(Color.WHITE);
                tp.setBackground(Color.BLACK);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(tp));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

How ever, you seem to making it transparent for some reason, output.setOpaque(false); 然而,你似乎因某种原因使它透明, output.setOpaque(false); . Now you've made the JScrollPane transparent as well, which is good, but you forgot to make the view port transparent jScrollPane1.getViewport().setOpaque(false); 现在你已经使JScrollPane透明了,这很好,但你忘了让视图端口透明jScrollPane1.getViewport().setOpaque(false);

Scroll panes are made up three components, the JScrollPane itself, the JViewport which determines what gets displayed and you component (the view) 滚动窗格由三个组件组成, JScrollPane本身, JViewport确定显示的内容和组件(视图)

ScrollPane 滚动窗格

Take a closer look at How to Use Scroll Panes for more details 仔细查看如何使用滚动窗格以获取更多详细信息

Set the look and feel from "Nimbus" to "Windows" and make sure the Text Pane's "Opaque" is true. 将外观从“Nimbus”设置为“Windows”,并确保文本窗格的“不透明”为真。

Don't worry, these errors happen when you code at 1 in the morning. 不用担心,当您在早上1点编码时会发生这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM