如何调整JPanel的大小

时间:2014-12-08 18:05:24

标签: java swing jpanel

我正在尝试调整JPanel的大小,但它下面有一个空间。这是一个显示链接:

enter image description here

  And this is the code :


import java.awt.*;
import javax.swing.*;

public class Ex1 extends JFrame{
private JTextArea textarea = new JTextArea ();
private JTextField field = new JTextField ();``
private JButton buton = new JButton ("Trimite");

public Ex1(){
    JPanel panel = new JPanel (new BorderLayout(2,2));
    JPanel panel1 = new JPanel (new BorderLayout(2,2));
    JPanel panel2 = new JPanel (new BorderLayout(2,2));
    JLabel label1 = new JLabel ("Mesaje");
    JLabel label2 = new JLabel ("Scrieti un mesaj");
    panel1.setPreferredSize(new Dimension(350,100));
    panel2.setPreferredSize(new Dimension(350,25));
    panel1.add(label1, BorderLayout.NORTH);
    panel1.add(textarea, BorderLayout.CENTER);
    panel2.add(label2, BorderLayout.WEST);
    panel2.add(field, BorderLayout.CENTER);
    panel2.add(buton, BorderLayout.EAST);
    setLayout(new GridLayout(2,1,1,1));
    panel.add(panel1, BorderLayout.NORTH);
    panel.add(panel2, BorderLayout.CENTER);
    add(panel);

}

public static void main(String[] args) {
    JFrame frame = new Ex1();
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

2 个答案:

答案 0 :(得分:4)

您正在将帧的布局设置为GridLayout,其中所有组件的大小相等。您有两行,add(panel)将面板添加到网格的第一行。第二行留空。见How to Use GridLayout

评论setLayout(new GridLayout(2,1,1,1));,额外的空间应该消失。当您对此行进行评论时,框架内容窗格的布局将为BorderLayoutJFrame的默认布局为BorderLayout。因此add(panel);会将面板添加到框架内容窗格的中心。因此,面板应占用所有可用空间。

作为旁注,请避免setPreferredSize(),通常没有必要,请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing了解详情。

您可以指定文本区域的行数和列数,并将其包装在滚动窗格中,即:

textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);

有关详细信息,请参阅How to Use Text Areas

编辑:getPreferredSize()

的示例
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.*;

public class Ex1 extends JPanel{
    private JTextArea textarea = new JTextArea ();
    private JTextField field = new JTextField ();
    private JButton buton = new JButton ("Trimite");

    public Ex1() {
        setLayout(new BorderLayout());

        JPanel panel1 = new JPanel (new BorderLayout(2,2));
        JPanel panel2 = new JPanel (new BorderLayout(2,2));

        JLabel label1 = new JLabel ("Mesaje");
        JLabel label2 = new JLabel ("Scrieti un mesaj");

        panel1.add(label1, BorderLayout.NORTH);
        panel1.add(new JScrollPane(textarea), BorderLayout.CENTER);

        panel2.add(label2, BorderLayout.WEST);
        panel2.add(field, BorderLayout.CENTER);
        panel2.add(buton, BorderLayout.EAST);

        add(panel1, BorderLayout.CENTER);
        add(panel2, BorderLayout.SOUTH);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(350, 300);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                JFrame frame = new JFrame("Test");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);

                Ex1 panel = new Ex1();

                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

您需要调整JFrame的大小而不是JPanel。尝试:

this.setPreferredSize(new Dimension(350, 25);// in Ex1

或者在您的主​​要方法中:

frame.setPreferredSize(new Dimension(350, 25);