更改窗口大小时,BorderLayout NORTH SOUTH CENTER窗格高度不会自动调整大小

时间:2018-04-22 00:04:00

标签: java swing layout-manager autoresize border-layout

我正在尝试使用BorderLayout向南添加2个面板,向南添加另一个面板但是当我尝试通过拖动/最大化窗口来更改窗口宽度/高度时,高度不会调整大小但是会被切断,虽然宽度调整得很好。 我希望所有面板在播放窗口时一次性自动调整大小,我缺少什么?

import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testss extends JFrame
{
    JPanel red = new ColorPanel(Color.red,300,300);
    JPanel grn = new ColorPanel(Color.green,100,600);
    JPanel orange = new ColorPanel(Color.ORANGE,100,50);
    JPanel blue = new ColorPanel(Color.BLUE,100,50);
    public  testss()
    {
        JPanel panel = new JPanel( new BorderLayout(10, 10) );
        blue.setBorder(BorderFactory.createLineBorder(Color.red));
        grn.setBorder(BorderFactory.createLineBorder(Color.red));
        orange.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.add(grn, BorderLayout.NORTH);
        panel.add(blue,BorderLayout.SOUTH);
        panel.add(orange,BorderLayout.CENTER);

        this.getContentPane().add(panel);   
        this.setVisible(true);   
        this.pack();   
    }   


    public static void main(String[] args)   
    {   
        new testss();   
    }   
}   


class ColorPanel extends JPanel   
{   
    public ColorPanel(Color color, int W, int H)   
    {   
        this.setBackground(color);   
        this.setPreferredSize(new Dimension(W,H));   
    }   
}

0 个答案:

没有答案