【Java AWT】AWT 组件及属性类

AWT 组件

Java.awt 包主要由组件类,事件类,布局类,菜单组件类等组成。

在这里插入图片描述

组件

关键字–》Component
组件(Component)是构成图形用户界面的基本成分和核心元素。

组件的特性对象

运行可见,具有坐标位置,尺寸,字体,颜色的属性。

组件的声明


import java.awt.*;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

public abstract class 组件 implements ImageObserver, MenuContainer, Serializable {//组件类
    public int getWidth(){//获得宽度
        return 0;
    }
    public int getHeight(){//获得高度
        return 0;
    }
    public void setSize(int widt,int hright){//设置宽度为:widht,高度为:height
    }
    public int getX(){//返回位置的X坐标值
        return 0;
    }
    public int grtY(){//返回位置的Y坐标值
        return 0;
    }
    public void setBounds(int x,int y){//设置位置为(x,y)

    }
    public void setBounds(int x,int y,int width,int height){//设置位置和宽度,高度

     }

    public Color getForeground() {//获得文本颜色
        return Color.red;
    }
    public void setForeground(Color color){//设置文本颜色为color
        return;
    }
    public Font getFount() throws IOException, FontFormatException {//获得字体
        return Font.createFont(0, (File) null);
    }
    public void setFount(Font font){//设置字体为font

    }
    public void setVisible(boolean visible){//设置是否可见

    }
    public void setEnabled(boolean enabled){//设置是否为有效状态

    }
}

容器

关键字–》Container
容器(Container)是一种能够容纳其他组件的特殊组件。
注意:容器类时组件类的子类。

容器类的声明


import java.awt.*;
//容器(Container)
public class 容器 extends Component {//容器类
    public void setLayout(LayoutManager layout){//设置布局管理器为lanyout,其类型是接口

    }
    public Component add(Component comp){//添加comp引用的任意组件
        return comp;
    }
    public void remove(int i){//删除i(i>0)个组件

    }
}

窗口

窗口的关键字–》Window
窗口有标题栏和关闭控制按钮,有边框,可添加菜单栏;窗口可以独立存在,运行时可以移动,被改变大小。
注意:窗口时顶层容器。

窗口类的声明

import javax.accessibility.Accessible;
import java.awt.*;

//window(窗口)
public class 窗口 extends Container implements Accessible {//窗口类
    //设置窗口相对于组件comp的位置。若comp为null,则将窗口置于屏幕中央
    public void setLocationRelativeTo(Component comp){

    }
}

相关的编程题


import java.awt.*;

public class 窗口例题 extends Frame {
    public 窗口例题(){//构造方法
        super("窗口");//标题
        this.setSize(500,350);//设置宽度和高度
        this.setLocation(10,10);//设置位置为(x,y)
        this.setForeground(Color.cyan);//设置文本颜色
        this.setBackground(Color.lightGray);//设置背景颜色
        this.setVisible(true);//设置是否可见
        this.setEnabled(false);//设置是否有效状态
    }

    public static void main(String[] args) {
        new 窗口例题();
    }
}

运行结果:在这里插入图片描述

面板

关键字–》Panel
面板没有标题,没有边框,不可添加菜单栏;
注意:面板不能独立存在。

面板类声明


import javax.accessibility.Accessible;
import java.awt.*;

public class 面板 extends Container implements Accessible {//面板
    public 面板(){//构造方法,默认FlowLayout布局,居中对齐

    }
    public 面板(LayoutManager layout){//构造方法,lanyout指定布局管理器

    }
}

相关的编程题

import java.awt.*;

public class 面板例题 extends Frame {
    public 面板例题(){
        super("面板");
        this.setBounds(10,10,400,400);
        this.setBackground(Color.cyan);
        this.setLayout(null);
        this.setVisible(true);
        Panel p = new Panel();
        p.setSize(200,200);
        p.setBackground(Color.red);
        p.setVisible(true);
        this.add(p);
    }

    public static void main(String[] args) {
        new 面板例题();
        //new Panel();
    }
}

运行结果:
在这里插入图片描述

框架

关键字–》Frame
框架是一种窗口,是Java Application 应用程序的主窗口,带有最大化,最小化和关闭控制按钮。

框架类声明

package 第六章.AwT组件;

import java.awt.*;

//框架(Frame)
public class 框架 extends 窗口 implements MenuContainer{//框架类
    public 框架(){//构造方法,默认BorderLanyout布局

    }
    public 框架(String title){//构造方法,title指定标题

    }
    public String getTitle(String title){//获得标题
        return title;
    }
    public void setTitle(){//设置标题

    }
    public void setResizeable(boolean resizeable){//设置是否可变大小,默认true

    }
}

相关的编程题


import java.awt.*;

public class 框架 extends Frame {
    public 框架(String s, Color c,int l, int t, int w, int h){
        super(s);       //设置框架标题
        this.setBackground(c);
        this.setBounds(l,t,w,h);
        this.setVisible(true);
        this.setEnabled(true);
    }

    public static void main(String[] args) {
        new Frame();
        for (int j = 0; j < 10; j++) {
         new 框架("窗口"+(j+1),new Color(0,(j+1)*25,0),j*150,j*80,300,200);
        }
    }
}

运行结果:
在这里插入图片描述

对话框

关键字–》Dialog
对话框也是一种窗口,但不能作为应用程序的主窗口,通常依赖框架。
注意:如果不关闭模式对话框,则不能对其他窗口进行操作。

对话框类的声明


import java.awt.*;

//对话框(Dialog)
public class 对话框 extends 窗口 {//对话框类
    public 对话框(Frame owner){//构造方法,owner指明拥有对话框的框架,默认BorderLayout布局

    }
    public 对话框(Frame owner,String title){//title指定对话框标题

    }
    public 对话框(Frame owner,boolean modal){//指定模式窗口,默认false

    }
    public 对话框(Frame owner,String title,boolean modal){

    }
}

相关的编程题

//测试-->对话框
import java.awt.*;

public class Frame1 extends Frame {
    Frame f = new Frame("测试");
    Dialog d = new Dialog(f,"模式对话框",true);
    Dialog d1 = new Dialog(f,"非模式对话框",false);
    public void init(){
        f.setSize(800,400);//设置宽度和高度
        d.setBounds(20,30,400,300);//设置位置
        d1.setBounds(100,80,200,100);
        f.setVisible(true);//是否可见
        d1.setVisible(true);
    }

    public static void main(String[] args) {
        new Frame1().init();
    }
}

运行结果:
在这里插入图片描述

标签

关键字–》Label
标签组件用于显示字符串。标签只能显示信息,不能用输入。

标签类的声明

import javax.accessibility.Accessible;
import java.awt.*;

//标签(Label)
//标签组件用于显示字符串。
public class 标签 extends Component implements Accessible {//标签类
    public static final int LEFT=0,CENTER = 1,RIGHT = 2;//对齐方式常量,左对齐,居中,右对齐
    public 标签(){//构造方法

    }
    public 标签(String text){//text指定字符串,默认左对齐

    }
    public 标签(String text,int align){//指定对齐方式,默认为Label常量

    }
    public String getText(String text){//获得字符串,字符串为text
        return text;
    }
    public void setText(String text){//设置字符串

    }
}

文本行

关键字–》TextField
文本行是一个单行文本编辑框,用于输入一行文本。

文本行类的声明

import java.awt.*;

//文本行(TextFie)
public class 文本行{}
public class 文本行 extends TextComponent {//文本行类
  public 文本行(){//构造方法

    }
    public 文本行(String text){//构造方法,text指定显示字符串

    }
    public 文本行(int columns){//构造方法,columns指定宽度(字符数)

    }
    public 文本行(String text,int columns){//构造方法

    }
    public String getText(String text){//获得字符串,字符串为text
     return text;
    }
    public void setText(){//设置字符串

    }
}

按钮

关键字–》Button
按钮用于显示操作命令,执行一种特定的操作。


import javax.accessibility.Accessible;
import java.awt.*;

//按钮(Button)
public class 按钮 extends Component implements Accessible {//按钮类
    public 按钮(String text){//构造方法,text指定标题

    }
}

AWT 相关的编程题

第一个:


import java.awt.*;//导入AWT包

public class AddFrame extends Frame {//加法运算器框架类,继承框架类
    public AddFrame(){//构造方法
        //以下设置框架的标题,尺寸,位置,背景色,布局等属性
        super("加法运算");//设置框架标题
        this.setSize(400,100);//设置组件尺寸
        this.setLocation(300,240);//设置组件的显示位置
        this.setLayout(new FlowLayout());//设置框架流布局,居中
        //以下在框架上添加标签,文本行,按钮等组件

        this.add(new TextField("10",8));//添加文本行(宽度)//括号里面可以写初值
        this.add(new Button("+"));//添加标签组件

        this.add(new TextField("20",8));
        this.add(new Button("="));//添加按钮(标题)
        this.add(new TextField(10));//添加文本行(初值默认为"")
        this.setVisible(true);//显示框架,必须在添加组件后
    }

    public Color getForeground(){//获得文本颜色
        return Color.MAGENTA;
    }
    public void setForeground(){

    }
    public Color getBackground(){//获得背景颜色
        return Color.gray;
    }
    public void setBackground(Color color){

    }
    public static void main(String[] args) {
        new AddFrame();
    }

}

运行结果:
在这里插入图片描述

第二个:

package 第六章.练习题;
//Frame-->框架
import java.awt.*;

public class MyFrame extends Frame {
    public static int id=0;
    public MyFrame(int x, int y, int w, int h , Color color){
        super("窗口"+(++id));
        this.setBackground(color);
        this.setBounds(x,y,w,h);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        MyFrame myFrame1= new MyFrame(100,100,300,200,Color.blue);
        MyFrame myFrame2= new MyFrame(500,100,300,200,Color.GREEN);
        MyFrame myFrame3= new MyFrame(900,100,300,200,Color.YELLOW);
        MyFrame myFrame4= new MyFrame(100,400,300,200,Color.RED);
        MyFrame myFrame5= new MyFrame(500,400,300,200,Color.pink);
        MyFrame myFrame6= new MyFrame(900,400,300,200,Color.MAGENTA);
    }
}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

汇潮学堂

你的鼓励才是我的最大收获

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

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

打赏作者

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

抵扣说明:

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

余额充值