![](/img/trans.png)
[英]Java Swing: Ghost text showing up when updating text on a JTextArea with partially transparent background
[英]Text Overlaps when JTextArea background is set to transparent
我正在嘗試創建一個透明的 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();
}
不要使用new Color(1.0f, 1.0f, 1.0f, 1.0f)
來使JTextArea
透明。 請改用setOpaque(false)
。
另外,不要在事件分派線程之外對組件進行更新。 當您想運行“繁重/長時間”任務時,您應該使用SwingWorker
並使用publish
方法將更新發布到 UI 線程(稱為事件調度線程)。 我建議您閱讀Swing 中的並發,以了解線程在 Swing 環境中應該如何工作。
執行java -version
時讀取終端 output 的完整示例。 注意process.getErrorStream
。 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.