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



    }
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皈臧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值