黑马程序员--JAVA基础之GUI

来源:互联网 发布:极点五笔 centos 编辑:程序博客网 时间:2024/05/02 01:11

    JAVA学习第23天

/*
创建图形化界面:
1,创建frame窗体
2,对窗体进行基本设置
 比如:大小,位置,布局
3,定义组件
4,将组件通过窗体的add方法添加到窗体中
5,让窗体显示,通过setVisible(true);

 


事件监听机制的特点:
1,事件源
2,事件
3,监听器
4,事件处理

事件源:就是awt包或者swing包中的那些图形界面组件

事件:每一个事件源都有自己特有的对应事件和共性事件

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

以上三者,在java中都已经定义好了
直接获取其对象来用就可以

我们要做的事情是:就是对产生的动作进行处理

*/

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("I am Button"); //创建一个按钮
  
  f.add(b);      //将按钮添加到窗体中
  
  //f.addWindowListener(new MyWin()); //事件监听,首先得有事件,被封装成对象

  //使用匿名内部类,监听事件
  f.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.out.println("window close");
    System.exit(0);
   }

   public void windowActivated(WindowEvent e)
   {
    System.out.println("actived");
   }
   
   public void windowOpened(WindowEvent e)
   {
    System.out.println("打开,ok。。。。。。。。。。");
   }
  });
  
  
  f.setVisible(true);  //通过setVisible(true)方法显示窗体
  //System.out.println("Hello World!");
 }
}
/*
class MyWin implements WindowListener
{
 //WindowListener是接口,有七个抽象方法,要new 事件对象,要覆盖7个方法,可是我只用到了关闭的动作
 /其他动作都没有用到,可是却必须复写,该方法不行,得另找他路
}
*/

//因为WwindowListener 的子类WindowAdapter已经实现了WindowListener接口
//并覆盖了其中的所有方法,那么我只要继承自WindowAdapter 覆盖我需要的方法即可
class MyWin extends WindowAdapter
{
 public void windowClosing(WindowEvent e)
 {
  //System.out.println("关闭"+e.toString());
  System.exit(0);
 }
}

/**/
import java.awt.*;
import java.awt.event.*;

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

 public void myEvent()
 {
  f.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });

  /*
  让按钮具备退出程序的功能

  按钮就是事件源
  那么选择那个监听器呢?
  通过关闭窗体事例了解到,要想知道那个组件具备什么样的特有监听器
  需要查看该组件对象的功能
  通过查阅button的描述,发现按钮支持一个特有监听 addActionLisrener
  */
  but.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("单击按钮退出");
    System.exit(0);
   }
  });
 }
 
 public static void main(String[] args)
 {
  new FrameDemo();
 }
}

 

/**/
import java.awt.*;
import java.awt.event.*;

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

  but = new Button("my button");
  tf = new TextField(20);

  //将组件添加到frame中
  f.add(tf);
  f.add(but);

  //加载一下窗体上的事件
  myEvent();
  
  //显示窗体
  f.setVisible(true);
 }

 private void myEvent()
 {
  //添加窗体监听器  WindowAdapter窗体适配器
  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.getKeyCode()==KeyEvent.VK_ENTER)  //组合键 control + enter
    {
     System.out.println("control + enter_key press");
    }
    
    //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());
    //e.getKeyCode() 返回的是字符的代表数字
   }
  });


  //对按钮添加鼠标监听器,事件源是按钮,事件是鼠标的进入,单击
  but.addMouseListener(new MouseAdapter()
  {
   int count = 1;
   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++);
   }

  });

  /*
  让按钮具备退出程序的功能

  按钮就是事件源
  那么选择那个监听器呢?
  通过关闭窗体事例了解到,要想知道那个组件具备什么样的特有监听器
  需要查看该组件对象的功能
  通过查阅button的描述,发现按钮支持一个特有监听 addActionLisrener
  
  but.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.out.println("单击按钮退出");
    //System.exit(0);
   }
  });
  */
 }
 
 public static void main(String[] args)
 {
  new MouseAndKeyEvent();
 }
}

 

/*
菜单制作,MenuBar Menu  MenuItem

*/

import java.awt.*;
import java.awt.event.*;

class  MyMenuDemo
{
 private Frame f;
 private MenuBar mb;
 private Menu m,m1;
 private MenuItem m11,closeItem;

 MyMenuDemo()
 {
  init();
 }
 public void init()
 {
  f = new Frame("my frame");
  f.setBounds(200,300,500,450);
  f.setLayout(new FlowLayout());

  //设置菜单啦
  mb = new MenuBar();
  m = new Menu("文件");
  m1 = new Menu("子菜单");

  m11 = new MenuItem("子条目");
  closeItem = new MenuItem("退出");

  mb.add(m);
  m.add(m1);
  m.add(closeItem);
  m1.add(m11);
  f.setMenuBar(mb); //将此窗体的菜单栏设置为指定的菜单栏

  /*
  设置后的菜单关系如下:
  文件(Menu)
  子菜单(Menu) |--子目录(MenuItem)
  退出(MenuItem)
  
  由此得出结论,菜单下还有子菜单,要用Menu,是最后的子菜单用MenuItem
  
  */

  myEvent();

  f.setVisible(true);
 }

 private void myEvent()
 {
  //菜单的退出监听事件,因为使用键盘或者鼠标都可以操作退出,实现退出功能,所以使用ActionListener
  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();
 }
}

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  MyWindowDemo
{
 private Frame f;
 private TextField tf;
 private Button but;
 private TextArea ta;

 private Dialog d;
 private Label lab;
 private Button okBut;

 MyWindowDemo()
 {
  init();
 }

 public void init()
 {
  //对窗体进行设置
  f = new Frame("my frame");
  f.setBounds(300,100,600,500);
  f.setLayout(new FlowLayout());

  tf = new TextField(60);
  but = new Button("转到");
  ta = new TextArea(25,70);

  f.add(tf);
  f.add(but);
  f.add(ta);
  
  /////////////////////////对 对话框进行设置
  d = new Dialog(f,"提示信息-self",true);
  d.setBounds(200,300,300,200);
  d.setLayout(new FlowLayout());

  okBut = new Button("确定");
  lab = new Label();
  
  d.add(lab);
  d.add(okBut);

  ////////////////////////

 

  myEvent();

  f.setVisible(true);
 }
 
 public void myEvent()
 {
  /*
  设置的界面上现在有 一个输入文本框,一个按钮,一个文本区域,需要实现的功能是
  在输入文本框中输入信息,按下按钮,可以将输入文本框中的信息复制到文本区域中
  
  思路:对按钮添加监听器
  */
  
  //对话框的确定按钮监听
  okBut.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    d.setVisible(false);
   }
  });
  
  //按下转到按钮,监听器
  but.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    showDir();


    //ta.setText(text); //这样的设置文本会将前一次输入的信息覆盖,解决办法是结合文件流
    //tf.setText("");
   }
  });
  
  //对文本输入框添加键盘监听器,当按下回车键时,执行
  tf.addKeyListener(new KeyAdapter()
  {
   public void keyPressed(KeyEvent e)
   {
    if (e.getKeyCode()==KeyEvent.VK_ENTER)
    {
     showDir();
    }
   }
  });
  
  //对话框的关闭监听
  d.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    d.setVisible(false);
   }
  });
  
  //窗体的关闭监听
  f.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
 }

 public void showDir()
 {
  String dirPath = tf.getText();  //输入的是文件夹名称
  File dir = new File(dirPath);  //将文件封装成对象
  if (dir.exists() && dir.isDirectory()) //判断文件是目录
  {
   String[] names = dir.list();  //遍历文件目录,取得文件夹中的文件名称
   ta.setText("");  //将上次复制的文件名清空
   for (String name : names )   //取得文件名
   {
    ta.append(name+"\r\n");
    //ta.setText(name+"\r\n");//将文件名传入到文本区域中.小心会将文件名覆盖
   }
  }
  else   //当输入的文件名称不存在时,需要弹出一个对话框
  {
   String info = "你输入的信息:"+dirPath+" 不存在,请重新输入";
   lab.setText(info);
   d.setVisible(true);
  }

 }

 public static void main(String[] args)
 {
  new MyWindowDemo();
 }
}

/*
菜单制作,MenuBar Menu  MenuItem
打开,保存菜单,对话框

*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class  OpenCloseFileDemo
{
 private Frame f;
 private MenuBar bar;
 private Menu file;
 private MenuItem openFile,saveFile,closeItem;

 private FileDialog openDia;
 private FileDialog saveDia;

 private TextArea ta;

 private File fi;

 OpenCloseFileDemo()
 {
  init();
 }
 public void init()
 {
  f = new Frame("my frame");
  f.setBounds(200,300,500,450);
  //f.setLayout(new FlowLayout()); 因为只有一个文本组件,使用默认的边界布局

  //设置菜单啦
  bar = new MenuBar();
  file = new Menu("文件");
  openFile = new MenuItem("打开");
  saveFile = new MenuItem("保存");
  closeItem = new MenuItem("退出");

  bar.add(file);
  file.add(openFile);
  file.add(saveFile);
  file.add(closeItem);
  f.setMenuBar(bar); //将此窗体的菜单栏设置为指定的菜单栏

  //当点击打开,保存菜单是,会弹出对话框
  openDia = new FileDialog(f,"打开文件",FileDialog.LOAD);
  saveDia = new FileDialog(f,"打开文件",FileDialog.SAVE);

  //定义文本框
  ta = new TextArea();
  f.add(ta);

  myEvent();

  f.setVisible(true);
 }

 private void myEvent()
 {
  //点击 保存菜单
  //
  saveFile.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    if(fi==null)
    {
     saveDia.setVisible(true);
     String dirPath = saveDia.getDirectory();  //获取对话框的目录名称
     String fileName = saveDia.getFile(); 
     if(dirPath == null || fileName == null)
      return ;
     fi = new File(dirPath,fileName); 
    }

    try    //将数据写入到文件中
    {
     BufferedWriter bufw = new BufferedWriter(new FileWriter(fi));
     String text = ta.getText();  //一下就将数据全部取到了
     
     bufw.write(text);
     //bufw.flush();
     bufw.close();     
    }
    catch (IOException en)
    {
     throw new RuntimeException("数据写入失败");
    }
   }
  });
  //当点击打开菜单时,弹出打开对话框
  //当选择某个文件后,该文件中的内容显示在窗体的文本框中,
  openFile.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    openDia.setVisible(true);
    String dirPath = openDia.getDirectory();  //获取对话框的目录名称
    String fileName = openDia.getFile();   //获取对话框选择的文件名称
    //System.out.println(dirPath+"..."+fileName);
    
    if(dirPath == null || fileName == null)
     return ;
    File file = new File(dirPath,fileName);  //将对话框获取到的文件封装成对象
    
    try
    {
     BufferedReader bufr = new BufferedReader(new FileReader(file));
     String line = null;
     while ((line = bufr.readLine()) != null)
     {
      ta.append(line+"\r\n");
     }
     bufr.close();
    }
    catch (IOException ex)
    {
     throw new RuntimeException("文件读取失败");
    }
    
   }
  });

  //菜单的退出监听事件,因为使用键盘或者鼠标都可以操作退出,实现退出功能,所以使用ActionListener
  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 OpenCloseFileDemo();
 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击