简体   繁体   中英

Resize the JPanel width based on the JFrame size

I would like to know if there is a way to resize only the width of a panel by taking it from the frame size, whereas, for the height value, I would like to leave the default one.

Thanks in advance!

Use the BorderLayout Luke!

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class BorderLayoutExample {

    public static void main(String[] args) {
        JTextArea area = new JTextArea(15, 40);
        JFrame frm = new JFrame("Horizontal resizing");
        // not required, because the default layout here is the BorderLayout.
        // but in common case you probably need to do it.
        frm.setLayout(new BorderLayout());
        // Use BorderLayout.SOUTH - when you want that the top space will not be unused.
        frm.add(new JScrollPane(area), BorderLayout.NORTH);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

When you resize the frame, only the width of the text area will be modified. For more information read about layout managers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM