java 如何调整 JPanel 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27364098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:40:55  来源:igfitidea点击:

How to resize a JPanel

javaswingjpanel

提问by Angelo

I am trying to resize the JPanels but there is a space under it . Here is a link to show :

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

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);
}

}

}

回答by tenorsax

You are setting a layout for a frame to GridLayoutin which all components are given equal size. You have two rows, add(panel)adds the panel to the first row of the grid. The second row is left empty. See How to Use GridLayout.

您正在为一个框架设置一个布局GridLayout,其中所有组件的大小都相同。您有两行,add(panel)将面板添加到网格的第一行。第二行留空。请参阅如何使用 GridLayout

Comment out setLayout(new GridLayout(2,1,1,1));and the extra space should go away. When you comment this line the layout of frame's content pane will be BorderLayout. The default layout of the JFrameis BorderLayout. So add(panel);will add the panel to the center of the frame's content pane. As a result the panel should occupy all the available space.

注释掉setLayout(new GridLayout(2,1,1,1));,多余的空间就会消失。当您评论此行时,框架的内容窗格的布局将为BorderLayout. 的默认布局JFrameBorderLayout。因此add(panel);会将面板添加到框架内容窗格的中心。因此,面板应占据所有可用空间。

As a side note, avoid setPreferredSize(), usually it is not necessary, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swingfor details.

作为旁注,避免setPreferredSize(),通常没有必要,请参阅我是否应该避免在 Java Swing 中使用 set(Preferred|Maximum|Minimum)Size 方法以获取详细信息。

You can specify the number of rows and columns for a text area and wrap it in the scroll pane, ie:

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

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

For more details see How to Use Text Areas

有关更多详细信息,请参阅如何使用文本区域

EDIT: example of getPreferredSize()

编辑: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);
            }
        });
    }
}

回答by brso05

You need to resize the JFrame not the JPanel. Try:

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

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

Or in your main method:

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

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