2021-10-17(java-gui)

一、第一个界面:Frame frame = new Frame();

import java.awt.*;
public class one {
    public static void main(String[] args) {
        Frame frame = new Frame("第一个图形界面");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(192,19, 100));
        //设置初始位置
        frame.setLocation(200,200);
        //大小固定
        frame.setResizable(false);
        //多个窗口
        Myframe myframe = new Myframe(100,100,200,200,Color.BLUE);
    }
}
//封装实现多个窗口
class Myframe extends Frame{
    static int id =0;
    public Myframe(int x,int y,int w,int h,Color color){
        super("Myframe"+(++id));
        setVisible(true);
        setBounds(x,y,w,h);
        setBackground(color);
    }
}

panle画板:

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

public class two {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        frame.setLayout(null);        //布局
        frame.setBounds(200,200,200,200);
        frame.setBackground(Color.blue);
        panel.setBounds(50,50,100,100);
        panel.setBackground(Color.BLACK);
        frame.add(panel);
        frame.setVisible(true);     
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

布局:

  1.  流式布局 :frame.setLayout( new FlowLayout() );     //默认居中 
    1. new FlowLayout(FlowLayout.LEFT)    //左对齐
  2. 东西南北中布局:BorderLayout.东南西北
  3. 网格布局:frame.setLayout( new GridLayout() ); 
    1. frame.pack()   //设置布局最优样式

事件监听:

        Frame frame =new Frame();

        Button button=new Button();

    因为addActionListener需要一个ActionListener,所以重写一个ActionListener(MyActionListener)

        MyActionListener myActionListener =new MyActionListener();

        button.addActionListener(myActionListener);

        frame.add(button,布局);

        frame.pack();

class MyActionListener implements ActionListener{方法}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值