JScrollpane loses size after minimize

人走茶凉 提交于 2019-12-13 02:49:53

问题


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

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