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

老子叫甜甜 提交于 2019-12-02 00:16:18

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!