Weird white space in a java JFrame

后端 未结 2 1133
野趣味
野趣味 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.

    0 讨论(0)
  • 2021-01-17 03:45

    The problem is because a JScrollPane also has a border.

    In this case you can remove it with:

    JScrollPane scroll = new JScrollPane(textArea);
    scroll.setBorder(null);
    

    And adding the JScrollPane to your frame as:

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

    But that will remove the border between the button and the text area

    So you need to create a MatteBorder on the top of your JButton:

    button.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.white));
    

    So it will look like this:

    I had forgotten to place how I ran your code in my own main method, because you forgot to add it as part of your MCVE:

    It will place the program on the Event Dispatch Thread (EDT)

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ConsoleWindow();
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题