问题
For some reason the jscrollpane somehow messes up the layout when the window is minimized and then restored.
Note that this is not happening if the text in the text area is not longer than the size of the text area (i.e. if it doesnt make a vertical scroll bar appear).
Can anyone see what's wrong with it?
My code below:
super (new GridBagLayout());
textField = new JTextArea(20,80);
textField.addKeyListener(null);
JScrollPane scrollPane1 = new JScrollPane(textField);
textArea = new JTextArea(20, 80);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
String[] dropValues = {"Format code","Add commas","Add quotes"};
dropdown = new JComboBox(dropValues);
dropdown.setSelectedIndex(0);
myButton = new JButton("Do it babe!!!");
myButton.addActionListener(this);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
// c.fill = GridBagConstraints.HORIZONTAL;
add(dropdown, c);
c.anchor = GridBagConstraints.CENTER;
// c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane1, c);
add(myButton, c);
add(scrollPane, c);
When first launching the app

When text longer than textarea height

After restoring

回答1:
Use c.fill = GridBagConstraints.BOTH;
with weightx
and weighty
properties for your JScrollPane
's. It will helps.
来源:https://stackoverflow.com/questions/19773355/jscrollpane-loses-size-after-minimize