Weird white space in a java JFrame

后端 未结 2 1132
野趣味
野趣味 2021-01-17 03:04

Here is my problem. When i use the following code:

package xyz.lexium.giapb.ui;

 import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java         


        
2条回答
  •  北海茫月
    2021-01-17 03:43

    You're forgetting that the JScrollPane has a border.

    frame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
    

    Set it to null.

    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setBorder(null);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    

    Another issue: this isn't Swing thread-safe:

    public synchronized void run() {
        try {
            while (Thread.currentThread() == reader) {
                try {
                    this.wait(100);
                } catch (InterruptedException ie) {
                }
                if (pin.available() != 0) {
                    String input = this.readLine(pin);
                    textArea.append(input);  // **************
                }
                if (quit)
                    return;
            }
    
            while (Thread.currentThread() == reader2) {
                try {
                    this.wait(100);
                } catch (InterruptedException ie) {
                }
                if (pin2.available() != 0) {
                    String input = this.readLine(pin2);
                    textArea.append(input);  // **************
                }
                if (quit)
                    return;
            }
        } catch (Exception e) {
            textArea.append("\nConsole reports an Internal error.");  // **************
            textArea.append("The error is: " + e);  // **************
        }
    
    }
    

    You're making textArea.append(...) calls off of the Swing event thread, and this can cause hard to debug intermittent exceptions to be thrown. Be sure to only append to this text component on the event dispatch thread.

提交回复
热议问题