简体   繁体   中英

too much empty space inside jpanel with gridlayout

I have a JPanel and inside I use a GridLayout like this:

    JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));

    JPanel p1 = new JPanel(new FlowLayout());
    JLabel label = new JLabel("SOMETHING");
    JTextField tf = new JTextField(30);

    JPanel p2 = new JPanel();

    JTextArea txt = new JTextArea(6, 30);
    JScrollPane sp = new JScrollPane(txt);

    p1.add(label);
    p1.add(tf);

    p2.add(sp);

    panel.add(p1);
    panel.add(p2);

Unfortunately, the space between the JTextArea and the upper elements if very big. What can I do to bring the JTextArea up?

http://img20.imageshack.us/img20/1086/screenshot1412201213550.png

Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER .

Screenshot of the code below:

截图

public static void main(String[] args) {

    JFrame frame = new JFrame("Test");

    frame.add(new JPanel(new FlowLayout()) {{
        add(new JLabel("something"));
        add(new JTextField(30));
    }}, BorderLayout.NORTH);

    frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

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