布局管理器

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Exercise {
    public static void main(String[] args) {
        //总Frame
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(new Color(0x2020DE));
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
        //四个面板
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));
         //第一个面板
        panel1.add(new Button("button"),BorderLayout.EAST);
        panel1.add(new Button("button"),BorderLayout.WEST);
        //第二个面板
        panel2.add(new Button("button"));
        panel2.add(new Button("button"));
        //把二面板放一面板上
        panel1.add(panel2,BorderLayout.CENTER);
        //第三个面板
        panel3.add(new Button("button"),BorderLayout.EAST);
        panel3.add(new Button("button"),BorderLayout.WEST);
        //第四个面板
        panel4.add(new Button("button"));
        panel4.add(new Button("button"));
        panel4.add(new Button("button"));
        panel4.add(new Button("button"));
        //第四个面板放在第三个面板上
        panel3.add(panel4,BorderLayout.CENTER);
        //放入Frame
        frame.add(panel1);
        frame.add(panel3);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

布局管理器_第1张图片

 

你可能感兴趣的:(java,开发语言)