[英]Text Overlaps when JTextArea background is set to transparent
I'm trying to create a transparent JTextArea but the updated text overlaps with the previous one.我正在尝试创建一个透明的 JTextArea,但更新后的文本与前一个文本重叠。
I tried using scrollpane, borderlayout and setOpaque(false) method of TextArea but none of them worked.我尝试使用 TextArea 的 scrollpane、borderlayout 和 setOpaque(false) 方法,但它们都不起作用。
Transparent Background - >
透明背景 - >
Here is the code这是代码
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();
}
Don't use a new Color(1.0f, 1.0f, 1.0f, 1.0f)
in order to make the JTextArea
transparent.不要使用
new Color(1.0f, 1.0f, 1.0f, 1.0f)
来使JTextArea
透明。 Use setOpaque(false)
instead.请改用
setOpaque(false)
。
Also, do not make updates to components outside of event dispatch thread.另外,不要在事件分派线程之外对组件进行更新。 When you want to run a "heavy/long" task you should use a
SwingWorker
and publish updates to the UI thread (called Event dispatch thread) by using publish
method.当您想运行“繁重/长时间”任务时,您应该使用
SwingWorker
并使用publish
方法将更新发布到 UI 线程(称为事件调度线程)。 I recommend you to read Concurrency in Swing in order to understand how things should work in a Swing environment when it comes to threading.我建议您阅读Swing 中的并发,以了解线程在 Swing 环境中应该如何工作。
A full example that reads terminal's output when executing java -version
.执行
java -version
时读取终端 output 的完整示例。 Notice the process.getErrorStream
.注意
process.getErrorStream
。 java -version
command writes to error stream. You might want to change that. java -version
命令写入错误 stream。您可能想要更改它。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.