javaGUI

1. 创建frame窗体

Frame f = new Frame(“标题”)

2,对窗体进行基本设置,如大小,布局

f.setSize(长;宽)
f.setLocation(x;y)
f.setBounds(int x, int y, int width,int height)
  • x - 组件的新 x 坐标
  • y - 组件的新 y 坐标
  • width - 组件的新 width
  • height - 组件的新 height

3. 定义组件,并用add方法添加

f.add(组件)

4. 让窗体显示

f.setVisiable(true)

4. 监听

button.addActionListener是活动监听,比button.addMouseListener后执行。
+ 激活窗口:

//窗体前置
windowActivated(WindowEvent e)
  • 打开窗口:
windowOpened(WindowEvent e)

每次打开时,很多方法放在awt包中,如TextField,TextArea类中的方法。

5.布局:

  • 流式布局:默认居中,从上往下布局
  • 式布局:东南西北式,如果不指定,将会整个填充整个frame
  • 网格式布局:如计算器
  • 卡片式布局:默认为全部填充

若要改变布局,用f.setLayout(new FlowLayout());

6,常见类

  • Component类
  • Container类(extends Component):
    • add方法
    • addMouseListener(MouseListener l) 方法
    • MouseListener()方法。
  • 获取鼠标事件(MouseEvent)
    • getClickCount()方法

用法:

b.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
        if(e.getClickCount == n){}
    }
})
  • Frame类:
    Window(方法)有addWindowListener(参数)监听器方法
    setBounds(int x, int y, int width, int height) 方法,位于Component类中。

7. 添加窗口监听器

参数即是监听器,有很多的动作。
但监听器(WindowListener)是接口,因此要去找它的子类WindowAdapter(适配器),已将WindowListener
接口中的方法全部覆盖。因此实际用的是WindowAdapter类。
WindowAdapter类中的方法
用法:

f.addWindowListener(new WindowAdapter(){
    //注意导入awt.event包
    public void windowClosing(WindowEvent e){
    }
});
windowActivated(WindowEvent e) //方法,窗体前置
windowOpened(WindowEvent e) //窗口首次被打开时调用

8. 活动监听:

addActionListener(new ActiveListener(){
    public void actionPerformed(ActionEvent e){}
});

监听什么,就传入什么事件。ActiveListener——ActionEvent
ActiveListener没有适配器的接口,只要覆盖其中唯一的方法actionPerformed(ActionEvent e) ,就可以创建对象。

9. 键盘事件

new button().addKeyListener(new KeyAdapter(){
    //此时button为当前事件源
    public void KeyPressed(KeyEvent e){
    }
});

谁是事件源,后面的方法就操作谁。

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值