• java学习笔记 GUI编程


    GUI  graphics user interface 图形用户接口

    1. AWT    abstract window toolkit 抽象窗口开发包
    2. 组件管理器
    3. 布局管理器
    4. 事件处理
    5. java图形
    6. window事件

    AWT 抽象了图形元素,例如工具栏,菜单,下拉条,输入框等

    clip_image001[10]

    component:所有可以显示的图形元素

    window:   可以自由停泊的顶级窗口,一般通过其子类将其new出   panel:可容纳其他元素,但不能独立显示

    Frame

    frame 的各种属性,setBounds(x,y,width,height) x y是左上角坐标,setSize ,setLocation,setBackground,setVisible(要想看见,这个方法必须设为true),setTitle,setResizable

    直接new出各种frame

    import java.awt.*;
    
    public class Test  {
        public static void main(String[] args) throws Exception {
            Frame f1 = new Frame("f1");
            Frame f2 = new Frame("f2");
            Frame f3 = new Frame("f3");
            
            f1.setSize(300,400);
            f1.setTitle("Frame");
            f1.setResizable(false);
            //f1.setOpacity(0.1f);没搞懂
            f1.setBackground(Color.BLUE);
            
            f2.setBounds(300,0,300,400);
            f2.setBackground(Color.lightGray);
            
            f3.setBounds(0,0,300,400);
            f3.setBackground(Color.GRAY);
            f3.setLocation(0,400);
            
            f1.setVisible(true);//window方法
            f2.setVisible(true);
            f3.setVisible(true);
            
        }    
    }

    通过继承Frame类创建frame

    import java.awt.*;
    
    class Frame1 extends Frame {
        
        public Frame1(int x,int y,int z){
            super("Frame类");//父类的构造方法
            setSize(300,400);
            setBackground(new Color(x,y,z));
            setVisible(true);
        }    
    }
    
    public class TestFrame {
        public static void main(String[] args) {
            Frame1 f1 = new Frame1(204,204,255);
        }
    }        
    
    /*
    1.一个文件里面写了两个类,一个public声明,一个default声明,当javac编译TestFrame时,会把TestFrame类里使用的Frame1也编译
    
    */

    Panel

    panel自己不能独立的显示

    panel.setBounds()方法里面的参数是放在谁里面就是相对于谁,不是以屏幕左上角作为标准的

    一个frame含有是个panel里面是四种颜色

    import java.awt.*;
    
    public class Test {
        public static void main(String[] args) {
            Panel1  p1 = new Panel1(); 
        }    
    }
    
    class Panel1 extends Panel {
        public Panel1() {
            Frame f1 = new Frame();//新建Frame用于添加panel
            Panel p1 = new Panel();//Panel的构造方法,所使用的是Panel的默认布局管理器 flowLayout
            Panel p2 = new Panel();    
            Panel p3 = new Panel();
            Panel p4 = new Panel();
            f1.setBounds(500,500,400,400);
            p1.setBounds(0,0,200,200);
            p2.setBounds(200,0,200,200);
            p3.setBounds(0,200,200,200);
            p4.setBounds(200,200,200,200);
            p1.setBackground(Color.BLACK);
            p2.setBackground(Color.BLUE);
            p3.setBackground(Color.    CYAN);
            p4.setBackground(Color.DARK_GRAY);
            f1.add(p1);f1.add(p2);f1.add(p3);f1.add(p4);
            f1.setVisible(true);
        }    
    }

    一个panel在frame中间,背景色是黄色

    import java.awt.*;
    
    public class Test {
        public static void main(String[] args) {
            Panel1  p1 = new Panel1(200,400); 
        }    
    }
    
    class Panel1 extends Panel {
        //int i,j;
        public Panel1(int i,int j) {
            //this.i = i;
            //this.j = j;
            int x = 400,y = 500;
            Frame f1 = new Frame();//里面含有默认布局管理器
            Panel p1 = new Panel();
        
            f1.setBounds(0,0,x,y);
            f1.setLayout(null);//自己手动设置布局管理器,取代默认布局管理器
            p1.setBounds(x/2 - i/2,y/2 - j/2,i,j);
            p1.setBackground(Color.YELLOW);
            f1.add(p1);
            f1.setVisible(true);
        }    
    }
  • 相关阅读:
    Mybatis和Hibernate
    SpringMVC运行原理
    HTML中class属性里有多个属性值
    json对象(增加数据)
    Ajax请求
    <url-pattern>里的/和/*
    mysql忘记密码
    Eclipse无法正常连接mysql8.0.20
    a+1和&a+1
    可重入函数与不可重入函数
  • 原文地址:https://www.cnblogs.com/yhwsy/p/5843891.html
Copyright © 2020-2023  润新知