黑马Java学习笔记之-----GUI
---------------------- android培训、java培训、期待与您交流! ----------------------
GUI:
1、布局
(1)FlowLayout(流式布局管理器),从左到右的顺序排列;是Panel的默认布局管理器。
(2)BorderLayout(边界布局管理器),东南西北中,在边界上布局,默认是居中填充;是Frame的默认布局管理器。
(3)GridLayout(网格布局管理器),跪着的矩阵。
(4)CardLayout(卡片布局管理器),即选项卡。
(5)GridBagLayout(网格包布局管理器),非规则的矩阵。
2、Frame
(1)new Frame(String title),建立窗口对象。
(2)setSize(int w,int h),设置窗口大小;setLocation(int x,int y),设置屏幕上显示的位置;setLayout(new 某种Layout类型),即传递一个布局管理器对象,控制 Frame的布局。setBounds(int x,int y,int w,int h),设置窗口的大小和屏幕初始位置。
(3)add(组件对象),向窗口里添加组件。
(4)setVisible(boolean b),设置窗口可见性。
3、窗体事件
(1)addWindowListener(WindowListener wl),添加一个窗口监听器对象给窗口,用于监控窗口事件。
(2)WindowListener接口规定的方法:windowActivated(WindowEvent e),将窗口看设置为活动窗口时调用;windowClosed(WindowEvent e)对窗口调用dispose而将其 关闭时调用;windowClosing(WindowEvent e)用户试图从窗口的系统菜单中关闭窗口时调用;windowDeactivated(windowEvent e)当窗口不再是活动窗口时调用; windowDeiconified(WindowEvent e)窗口从最小化状态变为正常状态时调用;windowIconified(WindowEvent e)窗口从正常状态变为最小化状态时调用; windowOpened(WindowEvent e)窗口首次变为可见时调用。
(3)WindowAdapter类是WindowListener接口的实现类,用于快速建立事件监听器,在实际使用中,只要建立一个继承WindowAdapter类并根据需要覆写了对应窗口事件的方法的 类,然后在addWindowListener的参数里传递这个类的对象即可。
(4)除了WindowListener规定的方法,WindowAdapter还建立一些自己的方法,具体使用中可以查看API文档。
4、Action事件
(1)为Button(按钮)添加按钮事件监听器:but.addActionListener(ActionListener e);语句为but按钮添加一个监听器。
(2)ActionListener和WindowListener一样,也是一个接口,在建立作为参数的对象前,需要建立一个类继承它并覆写actionPerformed(ActionEvent e)方法。
5、鼠标事件
(1)为Button(按钮)添加鼠标事件监听器:but.addMouseListener(MouseListener e),同样MouseListener也是一个接口,它有一个实现子类MouseAdapter,具体使用时一般参数都是传递一个继承并覆写MouseAdapter的类对象。
(2)MouseAdapter具有的方法:
a、mouseClicked(MouseEvent e),响应单击事件;
b、mouseEntered(MouseEvent e),鼠标进入到组件时调用;
c、mouseExited(MouseEvent e),鼠标离开组件时调用;
d、mousePressed(MouseEvent e),鼠标按键在组件上按下时调用;
e、mouseReleased(MouseEvent e),鼠标在组件上释放时调用;
f、mouseDragged(MouseEvent e),鼠标按键在组件上按下并拖动时调用;
g、mouseMoved(MouseEvent e),鼠标光标移动到组件上但无按键按下时调用;
h、mouseWheelMoved(MouseEvent e),鼠标滚轮旋转时调用;
(3)判断双击事件:MouseEvent类具有点击次数判断方法getClickCount(),所以只要判断e.getClickCount()==2,就可以了。以及左右键等等的数据判断方法都存在于MouseEvent类中。
6、键盘事件
(1)为Button(按钮)添加键盘事件监听器:but.addKeyListener(KeyListener e),同样KeyListener也是一个接口,它有一个实现子类KeyAdapter,具体使用时一般参数都是 传递一个继承并覆写KeyAdapter的类对象。
(2)KeyAdapter具有的方法:
a、keyPressed(KeyEvent e),按下某个按键时调用此方法;
b、keyRelease(KeyEvent e),释放某个按键时调用次方法;
c、keyTyped(KeyEvent e),键入某个键时调用此方法;
(3)KeyEvent类的常用方法:
a、getKeyChar();返回与事件中的键相关联的字符;
b、getKeyCode();返回事件中的键关联的整数KeyCode;
c、static String getKeyText(int keycode),这是一个静态方法,返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。一般使用为 KeyEvent.getKeyCode(e.getKeyCode())。
7、其他窗口组件类
TextFiled 文本行;TextArea 文本区;Dialog 对话框;
创建图形化界面:
1,创建frame窗体
2,对窗体进行基本设置
比如大小,位置,布局
3,定义组件
4,将组件通过窗体的add方法添加到窗体中
5,让窗体显示,通过setVisible(true)
事件监听机制的特点:
1)事件源
2)事件
3)监听器
4)事件处理
事件源:就是awt包或者swing包中的那些图形界面组件
事件:每一个事件源都有自己特有的对应事件和共性事件
监听器:将可以触发某一个事件的动作(不止一个动作)都封装到监听器中
import java.awt.*; import java.awt.event.*; class AwtDemo { public static void main(String[] args) { Frame f=new Frame("my awt"); f.setSize(500,400); f.setLocation(300,200); f.setLayout(new FlowLayout()); Button b=new Button("我是一个按钮"); f.add(b); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("我关"); System.exit(0); } public void windowActivated(WindowEvent e) { System.out.println("我活动"); } public void windowOpened(WindowEvent e) { System.out.println("我被打开,haha"); } }); f.setVisible(true); } }
需求:
建立一个菜单栏选项窗口
import java.awt.*; import java.awt.event.*; import java.io.*; class MyMenuDemo { private Frame f; private MenuBar mb; private Menu m,subMenu; private MenuItem closeItem,subItem; MyMenuDemo() { init(); } public void init() { f=new Frame("my window"); f.setBounds(300,100,500,600); f.setLayout(new FlowLayout()); mb=new MenuBar(); m=new Menu("文件"); subMenu=new Menu("子菜单"); subItem=new MenuItem("子条目"); closeItem=new MenuItem("退出"); m.add(subMenu); subMenu.add(subItem); m.add(closeItem); mb.add(m); f.setMenuBar(mb); myEvent(); f.setVisible(true); } public void myEvent() { closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new MyMenuDemo(); } }
---------------------- android培训、java培训、期待与您交流! ----------------------