黑马程序员_java之图形用户界面

图形用户界面

JAVA为GUI(图形用户界面)提供的对象都存在于java.Awt和javax.Swing两个包中。

Java.Awt对平台依赖性强,属重量级控件;javax.Swing完全由java实现,增加了可移植性,属于轻量级控件。那么图形化界面提供了哪些组件呢?

容器中组件的排列方式,就是布局。

常见的布局方式:FlowLayout(流式)布局 从左到右 Panel默认的布局管理器

BoundLayout(边界)布局 东南西北中Frame默认的布局管理器 若没有指定组件位置,将最大面积填充窗体

GridLayout(网格)布局 规则矩阵

CardLayout(卡片)布局 选项卡

GridBagLayout(网格包布)布局 非规则矩阵

坐标式布局

创建普通的图形化界面需要哪些步骤呢?

注意:container之所以可以添加其他组件式因为内部封装了集合,开启图形化界面就是开启了一个线程。

总体来说,一般是先创建窗体、设置窗体、定义组件、组件添加到窗体、加载事件、显示窗体。

GUI事件监听机制

机制:事件源(组件)、Awt或Swing包中的图形界面组件

           事件(Event) 每一个事件源都有自己特有的对应事件和共行事件

           监听器(Listener) 将可以触发某一事件的动作都已经封装到监听器中

           事件处理:引发事件后处理方式

 

GUI窗体事件

WindowListener接口有7个方法,而WindowListener子类WindowAdapter已实现WindowListener接口并覆盖所有方法,

那么只需继承WindowAdapter覆盖需要的方法。

例:      f.addWindowListener(new WindowAdapter(){

              public void windowClosing(WindowEvent e){

                  System.out.println("我关");

                  System.exit(0);

              }

GUI—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, 500, 400);

       f.setLayout(new FlowLayout());

       but=new Button("my button");

       //将组件加载到frame中

       f.add(but);

       //加载窗体事件

       myEvent();

       //显示窗体

       f.setVisible(true);

    }

    public void myEvent(){

       f.addWindowListener(new WindowAdapter(){

           public void windowClosing(WindowEvent e){

              System.exit(0);

           }

       });

        //让按钮具备推出程序功能

       /*按钮 事件源 选择监听器 ActionListener 无适配器监听器,

        方法超过3个基本都有适配器监听器*/

       but.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

              System.exit(0);

           }

       });

    }

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       new FrameDemo();

    }

}

GUI—鼠标事件

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++);

           }

       });

GUI—键盘事件

    //添加键盘监听

    but.addKeyListener(new KeyAdapter(){

       public void keyPressed(KeyEvent e){

          System.out.println(e.getKeyChar()+".."+e.getKeyCode());

           System.out.println(KeyEvent.getKeyText(e.getKeyCode())+".."+e.getKeyCode());

           //按回车和ctrl键退出

           if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)

                  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();

              }

           }

       });

GUI—对话框Dialog

注:初始化不能有对话框,出错的时候才有,

//true 窗体不能操作f,false窗体能操作f

注意:设置对话框,对话框也是窗体

       Dialog d=new Dialog(f,"提示信息——self",true);

       d.setBounds(400, 200, 240, 150);

       d.setLayout(new FlowLayout());

       lab=new Label();

       okBut=new Button("确定");

       d.add(lab);

       d.add(okBut);

GUI菜单

Menu extends MenuItem

Menu add(MenuItem) MenuBar  add(Menu) Frame setMenuBar(MenuBar )

MenuItem支持活动监听

closeItem.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

              System.exit(0);

           }

       });

GUI—打开保存文件

关键代码:记得判断

openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);

saveDia=new FileDialog(f,"我要保存",FileDialog.SAVE);

saveItem.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

              if(file==null){

                  saveDia.setVisible(true);

                  String dirPath=saveDia.getDirectory();

                  String fileName=saveDia.getFile();

                  if(dirPath==null || fileName==null)

                     return;

                   file=new File(dirPath,fileName);

              }

              try {

                  BufferedWriter bufw=new BufferedWriter(new FileWriter(file));

                  String text=ta.getText();

                  bufw.write(text);

                  bufw.close();

              } catch (IOException ex) {

                  throw new RuntimeException("保存失败");

              }  

           }

       });

       openItem.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e){

              openDia.setVisible(true);

              String dirPath=openDia.getDirectory();

              String fileName=openDia.getFile();

              if(dirPath==null || fileName==null)

                  return;

              ta.setText("");

              File file=new File(dirPath,fileName);

              BufferedReader bufr=null;

              try {

                  bufr=new BufferedReader(new FileReader(file));

                  String line=null;

                  while((line=bufr.readLine())!=null){

                     ta.append(line+"\r\n");

                  }

              } catch (IOException ex) {

                  throw new RuntimeException("读取失败");

              }  

              finally{

                  try {

                     if(bufr!=null){

                         bufr.close();

                     }

                  } catch (Exception exc) {

                     throw new RuntimeException("读取关闭失败");

                  }

              }

           }

       });

posted on 2013-01-28 16:43  念满  阅读(277)  评论(0编辑  收藏  举报