当JTextArea背景设置为透明时,文本重叠

问题描述

我正在尝试创建一个透明的JTextArea,但是更新后的文本与上一个重叠。

我尝试使用TextArea的scrollpane,borderlayout和setOpaque(false)方法,但是它们都不起作用。

enter image description here

透明背景->

enter image description here

这是代码

const servers = await client.guilds.cache.size

解决方法

请勿使用new Color(1.0f,1.0f,1.0f)来使JTextArea透明。请改用setOpaque(false)

此外,请勿在事件分发线程之外对组件进行更新。当您要运行“重/长”任务时,应使用SwingWorker并使用publish方法将更新发布到UI线程(称为事件分发线程)。我建议您阅读Concurrency in Swing,以便了解有关线程的事物在Swing环境中的工作方式。

执行java -version时读取终端输出的完整示例。注意process.getErrorStreamjava -version命令写入错误流。您可能要更改它。

public class PanelTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            PanelTest test = new PanelTest();
            test.createUI();
        });
    }

    public void createUI() {
        JFrame frame = new JFrame("Panel Test");
        frame.setLayout(new BorderLayout());
        frame.setUndecorated(true);
        frame.setBackground(new Color(1.0f,0.0f));

        JTextArea jTextArea = new JTextArea("");
        jTextArea.setEditable(false);
        jTextArea.setOpaque(false);
        jTextArea.setRows(12);
        jTextArea.setColumns(7);

        frame.add(jTextArea);
        frame.setResizable(true);
        frame.pack();
        frame.setVisible(true);

        SwingWorker<Void,String> terminalReader = new SwingWorker<Void,String>() {

            @Override
            protected Void doInBackground() throws Exception {

                while (true) {
                    String s = executeTerminalCommand("java -version");
                    publish(s);
                    Thread.sleep(2000);
                }
            }

            @Override
            protected void process(List<String> chunks) {
                if (!chunks.isEmpty()) {
                    String text = chunks.get(0);
                    jTextArea.setText("");
                    jTextArea.setText(text);
                    jTextArea.repaint();
                }
            }

            @Override
            protected void done() {
                try {
                    get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        };
        terminalReader.execute();
    }

    private static String executeTerminalCommand(String command) throws IOException {
        Process process = Runtime.getRuntime().exec(command);
        return readInputStreamAsString(process.getErrorStream());
    }

    public static String readInputStreamAsString(InputStream in) throws IOException {
        try (BufferedInputStream bis = new BufferedInputStream(in);
                ByteArrayOutputStream buf = new ByteArrayOutputStream();) {
            int result = bis.read();
            while (result != -1) {
                byte b = (byte) result;
                buf.write(b);
                result = bis.read();
            }
            return buf.toString();
        }
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...