java_GUI界面_面板-按钮-布局 基础

TestFrame

package GUI.lesson01;

import java.awt.*;
//第一个GUI界面
public class TestFrame {
    
    
    public static void main(String[] args) {
    
    

        //Frame,JDK,看源码
        Frame frame = new Frame("first GUI windows");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(255,255,255));
        //弹出的初始位置
        frame.setLocation(0,0);
        //设置大小固定
        frame.setResizable(false);

    }
}

TestFrame2

package GUI.lesson01;

import java.awt.*;

public class TestFrame2 {
    
    
    public static void main(String[] args) {
    
    
        //展示多个窗口
        MyFrame myFrame1=new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2=new MyFrame(300,100,200,200,Color.yellow);
        MyFrame myFrame3=new MyFrame(100,300,200,200,Color.red);
        MyFrame myFrame4=new MyFrame(300,300,200,200,Color.magenta);


    }
}

class MyFrame extends Frame {
    
    
    static  int id =0;//可能存在多个窗口,需要计数器

    public MyFrame(int x,int y,int w,int h,Color color){
    
    
        super("MyFrame"+(++id));
        setResizable(false);
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);

    }

}

面板TestPanel

package GUI.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Panel 可以看成一个空间,但是不能单独存在
public class TestPanel {
    
    
    public static void main(String[] args) {
    
    
        Frame frame =new Frame();
        //布局概念
        Panel panel=new Panel();


        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.green);

        //Panel设置坐标,相对于frame
         panel.setBounds(50,50,400,400);
         panel.setBackground(Color.red);

         //frame.add(panel)
        frame.add(panel);
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件  System.exit()
        frame.addWindowListener(new WindowAdapter() {
    
    
            //窗口关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        } );
    }
}

布局管理器

流式布局

package GUI.lesson01;

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

public class TestFlowLayout {
    
    
    public static void main(String[] args) {
    
    
        Frame frame =new Frame("按钮测试");
        frame.setSize(400,200);
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
        //监听事件,监听窗口关闭事件  System.exit()
        frame.addWindowListener(new WindowAdapter() {
    
    
            //窗口关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        } );
        //组件-按钮
        Button button01 = new Button("button01");
        Button button02 = new Button("button02");
        Button button03 = new Button("button03");

        //设置为流式布局
        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));


        //添加按钮
        frame.add(button01);
        frame.add(button02);
        frame.add(button03);

        frame.setVisible(true);
    }
}

东西南北中布局

package GUI.lesson01;

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

public class TestBorderLayout {
    
    
    public static void main(String[] args) {
    
    
        Frame frame=new Frame("TestBorderLayout");
        //监听事件,监听窗口关闭事件  System.exit()
        frame.addWindowListener(new WindowAdapter() {
    
    
            //窗口关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        } );
        Button east= new Button("east");
        Button west= new Button("west");
        Button south= new Button("south");
        Button north= new Button("north");
        Button center= new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);


        frame.setSize(400,400);
        frame.setVisible(true);



    }
}

表格布局

package GUI.lesson01;

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

public class TestGridLayout {
    
    
    public static void main(String[] args) {
    
    
        Frame frame=new Frame("TestGridLayout");
        //监听事件,监听窗口关闭事件  System.exit()
        frame.addWindowListener(new WindowAdapter() {
    
    
            //窗口关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        } );
        Button btn1= new Button("btn1");
        Button btn2= new Button("btn2");
        Button btn3= new Button("btn3");
        Button btn4= new Button("btn4");
        Button btn5= new Button("btn5");
        Button btn6= new Button("btn6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);


        //frame.setSize(400,400);
        frame.pack();//java函数:自动填充
        frame.setVisible(true);



    }
}

猜你喜欢

转载自blog.csdn.net/m0_47937557/article/details/117677340
今日推荐