------- android培训、java培训、期待与您交流! ----------
1.继承关系
Component
Container(可以添加其他组件通过add)
Window
Frame
Dialog
FileDialog
Panel
Button
Label
Checkbox
TextComponent
Textarea
TextField
2.布局
(1)FlowLayout(流式布局管理器):从左到右的顺序排列,Panel默认的布局管理器
(2)BorderLayout(边界布局管理器):东南西北中,Frame默认的布局管理器
(3)GridLayout(网格布局管理器):规则的矩阵
(4)CardLayout(卡片布局管理器):选项卡
(5)GradBagLayout(网格包布局管理器):非规则的矩阵。
3.创建图形化界面步骤:
(1)创建frame窗体
(2)对窗体进行基本设置(位置、大小、布局)
(3)定义组件
(4)将组件通过窗体add方法添加到窗体中
(5)让窗体显示,通过setVisible(true)
4.事件监听机制
特点:事件源,事件,监听器,事件处理
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AwtDemo {
/**定义一个窗体,在窗体中添加一个按钮具备关闭该窗体的功能。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f = new Frame("my awt");//新建一个Frame窗体
f.setSize(500,400);//设置大小
f.setLocation(300,200);//位置
f.setLayout(new FlowLayout());//布局
Button b = new Button("我是一个按钮");//定义组件
f.add(b);//将组件添加进窗体
//添加事件监听机制
f.addWindowListener(new WindowAdapter()//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口,并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可.
{
public void windowClosing(WindowEvent e)//关闭操作
{
System.exit(0);
}
});//覆盖需要用到的方法
f.setVisible(true);//将窗体显示
}
}
按钮的监听事件:
<span style="white-space:pre"> </span>Button but=new Button("关闭");
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
想要哪个组件具备什么样的特有监听器,需要查看该组件对象的功能。
button.addActionListener(ActionListener al)ActionListener没有Adapter,只有一个方法actionPerformed,只有覆盖该方法即可。
5.共性事件:鼠标和键盘
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseAndKeyEvent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
private Frame f;
private Button but;
private TextField tf;
MouseAndKeyEvent()
{
init();
}
public void init()
{
f = new Frame("my frame");//创建新的窗体
f.setBounds(300,100,600,500);//设置窗体的显示位置和窗体大小
f.setLayout(new FlowLayout());//设置窗体的布局
tf = new TextField(20);//定义一个单行文本组件
but = new Button("my button");//定义一个按钮
f.add(tf);//将单行文本加入到窗体
f.add(but);//将按钮添加到窗体
myEvent();//调用我的自定义事件事件
f.setVisible(true);//将窗体显示
}
private void myEvent()
{
f.addWindowListener(new WindowAdapter()//创建一个WindowAdapter覆盖其windowClosing方法
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//关闭操作
}
});
tf.addKeyListener(new KeyAdapter()//添加键盘监听机制,新建一个KeyAdapter覆盖keyPressed方法
{
public void keyPressed(KeyEvent e)//按键按下时调用此方法
{
//e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER//组合键
int code = e.getKeyCode();//获取键人的值
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))//判断是否是0-9的数字,即只能用数字键
{
System.out.println(code+".....是非法的");
e.consume();//如果满足就不处理事件,结束
}
}
});
//给But添加一个键盘监听。
but.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
//System.exit(0);
System.out.println("ctrl+enter is run");
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
}
});
but.addMouseListener(new MouseAdapter()//添加鼠标事件监听器
{
private int count = 1;
private int clickCount = 1;
public void mouseEntered(MouseEvent e) //复写鼠标进入方法
{
System.out.println("鼠标进入到该组件"+count++);
}
public void mouseClicked(MouseEvent e)//鼠标点击方法
{
if(e.getClickCount()==2)
System.out.println("双击动作"+clickCount++);
}
});
}
}
6.对话框:Dialog
也是一个窗体,但是依赖于原窗体,里面有两个组件:label和button
Dialog d=new Dialog(frame f,“窗体名”,true)最后一个参数表示模式true表示如果不操作对话框,所属的那个窗体不能操作。
7.菜单
MenuBar类封装绑定到框架的菜单栏的平台概念,为了将该菜单栏与frame对象关联,可以调用该框架的setMenuBar方法。
MenuBar mb=new MenuBar();Menu m=new Menu(“文件”);MenuItem closeItem=new MenuItem(“退出”);Frame f=new Frame("我的窗体")
m.add(closeItem);mb.add(m);f.setMenuBar(mb);
FileDialog fd=new FileDialog(f,"打开",FileDialog.LOAD)打开
FileDialog fd1=new FileDialog(f,"保存",FileDialog.SAVE)保存