黑马程序员——GUI图形化界面

来源:互联网 发布:剑三捏脸数据萝莉可爱 编辑:程序博客网 时间:2024/04/27 16:10

                                      --------------------- android培训、java培训、期待与您交流! ----------------------



一丶概论

1、用图形的方式,来显示计算机操作的界面,更方便直观2、Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中3、java.Awt--需要调用本地系统方法实现功能,也就是挂平台性较差,属于重量级控件4、javax.Swing--在AWT的基础上,建立的一套图形化界面系统,其中提供了更多的组件,无论在什么平台,展现的都一样|--属于轻量级控件5.继承关系图Component--组件|--Button--按钮|--Lable--标签|--Checkbox--复选框|--TextComponent--文本组件|--TextAree--文本区域|--TextField--文本框|--Container--容器|--Windows--窗口|--Frame--框架|--Dialog--对话框|--FileDialog--文件对话框 |--Panel--面板6、常见布局管理器|--FlowLayout(流式布局管理器)|--从左到右的顺序排列,Panel默认的布局管理器|--BorderLayout(边界布局管理器)|--东,南,西,北,中,Frame默认布局管理器|--GridLayout(网格布局管理器)|--规则的矩阵|--CardLayout(卡片布局管理器)|--选项卡|--GridBagLayout(网格包布局管理器)|--非规则矩阵

7、事件监听机制

                             

8、图形化界面的创建

package study_note_22;import java.awt.*;import java.awt.event.*;/* *1.创建图形化界面 *|--创建frame窗体 *|--对窗体进行基本设置--大小,位置,布局...... *|--定义组件 *|--将组建通过窗体的add方法添加到窗体中 *|--通过setVisible显示或隐藏窗体 *2.事件监听机制 *|--事件源 *|--就是awt包或者swing包中的那些图形界面组件 *|--事件 *|--每一个事件源都有自己特有的对应事件和共享事件 *|--监听器 *|--将可一个出发某一个事件的动作(不止一个动作),都封装到了监听器中 *|--事件处理 */public class AwtDemo{public static void main(String[] args){Frame f = new Frame("my awt");// 构造一个最初不可见的 Frame 新实例。f.setSize(500, 400);// 设置长和宽f.setLocation(300, 100);// 设置显示位置f.setLayout(new FlowLayout());// 设置布局方式Button b = new Button("我是一个按钮");f.add(b);f.addWindowListener(new MyWin());// //匿名内部类写法// f.addWindowListener(new WindowAdapter()// {// public void windowClosing(WindowEvent e)// {// //System.out.println("window closing!");//// System.exit(0);// }// });f.setVisible(true);// 取代show方法,显示或隐藏此组件System.out.println("Hello World!");}}// WindowAdapter实现了WindowListtence接口,覆盖了集中所有的方法,那么只要继承WindowAdapter覆盖所需要的方法即可class MyWin extends WindowAdapter{public void windowClosing(WindowEvent e){// System.out.println("window closing!");System.exit(0);}}

9、Action事件示例
package study_note_22;import java.awt.*;import java.awt.event.*;/* *Action事件  */public class FrameDemo{// 定义该图形中所需的组件的引用private Frame f;private Button but;FrameDemo(){init();}public void init(){f = new Frame("my frame");// 对frame进行基本设置f.setBounds(300, 100, 600, 500);f.setLayout(new FlowLayout());but = new Button("my button");// 将组件添加到frame中f.add(but);// 加载窗体事件myEvent();// 显示窗体f.setVisible(true);}private void myEvent(){f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});// 添加一个退出按钮/* * 按钮就是事件源 ,查看该组件对象的功能,确定使用什么监听器通过查阅API发现按钮特有的监听是addActionListener */but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});}public static void main(String[] args){new FrameDemo();}}
10、鼠标和键盘事件
package study_note_22;import java.awt.*;import java.awt.event.*;/* *鼠标和键盘事件  */public class MouseAndKeyEvent{private Frame f;private Button but;private TextField tf;MouseAndKeyEvent(){init();}public void init(){f = new Frame("my frame");// 对frame进行基本设置f.setBounds(300, 100, 600, 500);f.setLayout(new FlowLayout());tf = new TextField(20);but = new Button("my button");f.add(tf);// 将组件添加到frame中f.add(but);// 加载窗体事件myEvent();// 显示窗体f.setVisible(true);}private void myEvent(){f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){int code = e.getKeyCode();if (!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)){System.out.println(code + "......是非法的!");e.consume();}}});// 键盘监听but.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_ESCAPE)// 组合键System.exit(0);System.out.println(e.getKeyChar() + "......" + e.getKeyCode() + KeyEvent.getKeyText(e.getKeyCode()));}});but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.out.println("action ok");}});// 鼠标监听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++);}});}public static void main(String[] args){new MouseAndKeyEvent();}}



                                     --------------------- android培训、java培训、期待与您交流! ----------------------

                                                                  详细请查看:http://edu.csdn.net/heima

原创粉丝点击