【问题标题】:Text Overlaps when JTextArea background is set to transparentJTextArea 背景设置为透明时文本重叠
【发布时间】:2020-09-20 07:23:12
【问题描述】:

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

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

透明背景->

这里是代码

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

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


        JTextArea jTextArea = new JTextArea("");
        jTextArea.setEditable(false);
        jTextArea.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
        jTextArea.setRows(12);
        jTextArea.setColumns(7);

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

        Thread t = new Thread(){
            public void run(){
                while (true) {
                    
                    String s = execute("cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq");
                    System.out.println(s.length());
                    jTextArea.setText(s);
        
                    try {
                        this.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
        
                }
            }
        };
        t.start();
        
    }

【问题讨论】:

    标签: java swing jtextarea


    【解决方案1】:

    不要使用new Color(1.0f, 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, 1.0f, 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();
            }
        }
    }
    

    【讨论】:

    • 我确实运行了你的代码并发现了同样的事情。您认为我的桌面环境与此有关吗?我目前在 GNOME 3.36.4 上。你能告诉我你的代码在哪个环境下工作吗?
    • @jspphani 我在 Windows 7 中进行了测试。您是按原样测试我的示例,还是尝试根据需要进行调整?
    • 我按原样对其进行了测试,并根据我的原因进行了修改,但都没有奏效。也许是 gnome 在做的事情
    猜你喜欢
    • 2017-04-30
    • 2013-06-21
    • 2015-08-06
    • 1970-01-01
    • 2018-12-25
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多