java GUI编程


java GUI编程参照狂神老师的课程学习,记录笔记


java GUI编程

1. AWT

1.1 AWT介绍

  1. 包含了很多类和接口

  2. 元素:窗口,按钮,文本框

  3. java.awt

1.2组件和容器

1. Frame

​ 1. 设置窗口

​ 2. 设置为可见

​ 3.设置大小

​ 4.设置背景

​ 5.设置弹出的初始位置

​ 6.设置窗口的固定大小

package GUI;

import java.awt.*;

public class Frame_01 {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        //设置窗口标题
        frame.setTitle("我的第一个窗口");
        //设置为可见
        frame.setVisible(true);
        //设置大小
        frame.setSize(400,300);
        //设置背景
        frame.setBackground(Color.yellow);
        //弹出的初始位置
        frame.setLocation(450,350);
        //设置固定大小
        frame.setResizable(false);
    }
}

​ 问题:窗口点❌关不到,(需要监听事件),需要关闭程序。

2. 面板 Panel

​ 1.设置窗口的排版方式:setLayout()

​ 2.添加组件:add()

​ 3.设置panel的大小:setSize()

​ 4.设置panel在窗口中的初始位置:setLocation()

​ 5.设置panel背景:setBackground()

package GUI;

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

public class Panel_02 {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        //设置窗口的排版方式
        frame.setLayout(null);
        frame.setTitle("面板学习");
        frame.setSize(500,500);
        frame.setResizable(false);
        frame.setBackground(Color.yellow);
        Panel panel = new Panel();
        //设置panel的大小
        panel.setSize(200,200);
        //设置panel在窗口中的初始位置
        panel.setLocation(20,60);
//        设置panel背景
        panel.setBackground(Color.blue);
        //添加组件
        frame.add(panel);
        frame.setVisible(true);
        //解决关闭问题  添加窗口监听事件,监听窗口关闭指令
        frame.addWindowListener(new WindowAdapter() {
   
            @Override
            public void windowClosing(WindowEvent e) {
   
                System.exit(0);
            }
        });
    }
}

遗留问题:setLayout 有关布局的问题

3.布局管理器
  • 流式布局

    一排一排的排列,一排放完,放下一排

    package GUI;
    
    import java.awt.*;
    
    public class FlowLayout_03 {
         
        public static void main(String[] args) {
         
            Frame frame = new Frame();
            frame.setTitle("流式布局");
            frame.setVisible(true);
            frame.setSize(200,200);
            //按钮
            Button btn1 = new Button("按钮1");
            Button btn2 = new Button("按钮2");
            Button btn3 = new Button("按钮3");
            //设置布局
    //        frame.setLayout(new FlowLayout());//默认放在中间
    //        frame.setLayout(new FlowLayout(FlowLayout.LEADING));//从左边起
    //        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左边开始
            frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右边开始
            frame.setLayout(new FlowLayout(FlowLayout.TRAILING));//右边开始
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            
        }
    }
    
  • 东西南北中

    package GUI;
    
    import java.awt.*;
    
    public class BorderLayout_04 {
         
        public static void main(String[] args) {
         
            Frame frame = new Frame();
            frame.setTitle("流式布局");
            frame.setVisible(true);
            frame.setSize(200,200);
            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);
        }
    }
    
  • 表格布局

    package GUI;
    
    import java.awt.*;
    @SuppressWarnings({
         "all"})
    public class GridLayout_05 {
         
        public static void main(String[] args) {
         
            Frame frame = new Frame();
            frame.setTitle("流式布局");
            frame.setVisible(true);
            frame.setSize(200,200);
            //按钮
            Button btn1 = new Button("按钮1");
            Button btn2 = new Button("按钮2");
            Button btn3 = new Button("按钮3");
            Button btn4 = new Button("按钮4");
            Button btn5 = new Button("按钮5");
            Button btn6 = new Button("按钮6");
            //添加布局
            //GridLatout() 行 列 左间隔 上间隔
            frame.setLayout(new GridLayout(3,2));
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
        }
    }
    
    
4.事件监听

事件监听:当某个事情发生的时候,干什么?

package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionEvent_07 {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        frame.setTitle("事件监听");
        frame.setVisible(true);
        frame.setSize(200,200);
        Button button = new Button("按钮");
        //设置按钮监听
        button.addActionListener(new ButtonLister());
        frame.add(button);
    }
}
//按钮监听类 ,需要实现ActionListener
class ButtonLister implements ActionListener{
   
    @Override
    public void actionPerformed(ActionEvent e) {
   
        //需要干什么事?
        System.out.println("hihihi");
    }
}

多个按钮,共享一个事件

package GUI;

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

public class ActionEventUpdate_08 {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();
        frame.setTitle("事件监听");
        frame.setVisible(true);
        frame.setSize(200,200);
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        ButtonLisetener buttonLisetener = new ButtonLisetener();
        button1.addActionListener(buttonLisetener);
        button2.addActionListener(buttonLisetener);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.addWindowListener(new WindowAdapter() {
   
            @Override
            public void windowClosing(WindowEvent e) {
   
          
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alonzo de blog

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

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

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

打赏作者

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

抵扣说明:

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

余额充值