JTextArea expands as I enter text even though I have a JScrollPane, but the scrollbar doesn't show up either

后端 未结 1 1473
名媛妹妹
名媛妹妹 2021-01-22 00:27

I have this JTextArea and it keeps expanding as text is entered. I know this looks like a repeat question but I\'ve followed the answers and they haven\'t worked for me. I also

1条回答
  •  野的像风
    2021-01-22 00:45

    Your problem occurs here...

    consolePanel.add(console);
    consolePanel.add(scrollBar);
    

    Basically a component can only belong to a single parent, by adding the console to the consolePanel you are removing it from the scroll pane.

    The console is already contained within a container, so you simply only need to add the parent container (the scroll pane) to consolePane...

    //consolePanel.add(console);
    consolePanel.add(scrollBar);
    

    On a side note, you should be careful of Tookit#getScreenSize is it returns the "whole" screen and not the viewable screen size (that area which the application can safely use). So instead of ...

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();
    
    //sets the size of the GUI
    this.setSize(600, 400);
    
    //centers the GUI
    int xPos = (dim.width / 2) - (this.getWidth() /2);
    int yPos = (dim.height / 2) - (this.getHeight() /2);
    
    this.setLocation(xPos, yPos);
    

    You could use setLocationRelativeTo(null) and achieve a better result

    I would also suggest sizing the component after you have added the content and before you make it visible, for example...

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    

    This ensures that the window takes into consideration in difference between different platforms more accurately...

    0 讨论(0)
提交回复
热议问题