黑马程序员---AWT编程基础知识课堂笔记---七个程序

<a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流!

程序一、

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

class  AwtDemo1 *************************************************

{     public static void main(String[] args)

       {     Frame f = new Frame("my awt");

              f.setSize(500,100);

              f.setLocation(300,200);

              f.setLayout(new FlowLayout());

              Button b = new Button("我是一个按钮");

              f.add(b);

              f.setVisible(true);

              //System.out.println("Hello World!");

       }

}******************************************************************************

程序二、

事件监听机制的特点:

1,事件源。2,事件。3,监听器。4,事件处理。

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

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

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

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

我们要做的事情是,就是对产生的动作进行处理。实现关闭按钮的功能。

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("我被打开了,hahahhahah");  }

              });

              f.setVisible(true);   //System.out.println("Hello World!");

       }

}

class MyWin implements WindowListener

{//覆盖7个方法。可以我只用到了关闭的动作。//其他动作都没有用到,可是却必须复写。}

//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。

//并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。

class MyWin extends WindowAdapter

{     public void windowClosing(WindowEvent e)

       {     //System.out.println("window closing---"+e.toString());     System.exit(0);

       }

}

程序三、

定义一个窗体,在窗体中添加一个按钮具备关闭该窗体的功能。对程序二进行代码格式的优化

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

              });

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

              按钮就是事件源。

              那么选择哪个监听器呢?    通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。需要查看该组件对象的功能。       通过查阅button的描述。发现按钮支持一个特有监听addActionListener

              but.addActionListener(new ActionListener()

              {     private int count = 1;

                     public void actionPerformed(ActionEvent e)

                     {     //System.out.println("退出,按钮干的");

                            //System.exit(0);   

                            //f.add(new Button("Button-"+(count++)));

                            //f.setVisible(true);

                            //f.validate();

                            //System.out.println(e.getSource());

                            Button b = (Button)e.getSource();                     

                            Frame f1 = (Frame)b.getParent();

                            f1.add(new Button("button-"+count++));

                            f1.validate();

                     }

              });

       }

       public static void main(String[] args)

       {     new FrameDemo();       }

}******************************************************************************

程序四、

演示键盘监听和鼠标监听

class MouseAndKeyEvent *********************************************************

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

              {     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添加一个键盘监听。

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

}******************************************************************************

程序五、

练习:制作以下界面

当在文本框中输入路径点击“转到”或者“回车”后会在下面的文本区域显示该路径中所包的所有文件和文件夹的名称,如果路径不存在会弹出提示对话框。

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 window");

              f.setBounds(300,100,600,500);

              f.setLayout(new FlowLayout());

              tf = new TextField(60);

              but = new Button("转到");

              ta = new TextArea(25,70);

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

              f.add(tf);

              f.add(but);

              f.add(ta);

              myEvent();

              f.setVisible(true);

       }

       private void  myEvent()

       {     okBut.addActionListener(new ActionListener()

              {     public void actionPerformed(ActionEvent e)

                     {     d.setVisible(false);         }

              });

              d.addWindowListener(new WindowAdapter()

              {     public void windowClosing(WindowEvent e)

                     {     d.setVisible(false);  }

              });

              tf.addKeyListener(new KeyAdapter()

              {     public void keyPressed(KeyEvent e)

                     {     if(e.getKeyCode()==KeyEvent.VK_ENTER)

                                   showDir();

                     }

              });

              but.addActionListener(new ActionListener()

              {     public void actionPerformed(ActionEvent e)

                     {     showDir();      }

              });

              f.addWindowListener(new WindowAdapter()

              {     public void windowClosing(WindowEvent e)

                     {     System.exit(0);      }

              });

       }

       private void showDir()

       {     String dirPath = tf.getText();                     

              File dir = new File(dirPath);

              if(dir.exists() && dir.isDirectory())

              {     ta.setText("");

                     String[] names = dir.list();

                     for(String name : names)

                     {     ta.append(name+"\r\n"); }

              }

              else

              {     String info = "您输入的信息:"+dirPath+"是错误的。请重输";

                     lab.setText(info);

                     d.setVisible(true);

              }

       }

       public static void main(String[] args)

       {     new MyWindowDemo();       }

}******************************************************************************

程序六、

菜单的实现,完成菜单栏中添加菜单、菜单中添加菜单项和子菜单

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

import java.awt.event.*;

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("退出");          

              subMenu.add(subItem);

              m.add(subMenu);

              m.add(closeItem);

              mb.add(m);

              f.setMenuBar(mb);

              myEvent();

              f.setVisible(true);

       }

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

}******************************************************************************

程序七、

制作“文件”菜单,包含“打开”、“保存”、“退出”三个菜单项,并实现三个菜单项的功能。

package mymenu;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class MyMenuTest************* ********************************************

{     private Frame f;

       private MenuBar bar;

       private TextArea ta;

       private Menu fileMenu;

       private MenuItem openItem,saveItem,closeItem;

       private FileDialog openDia,saveDia;

       private File file;

       MyMenuTest()

       {     init();      }

       public void init()

       {     f = new Frame("my window");

              f.setBounds(300,100,650,600);

              bar = new MenuBar();

              ta = new TextArea();

              fileMenu = new Menu("文件");          

              openItem = new MenuItem("打开");

              saveItem = new MenuItem("保存");

              closeItem = new MenuItem("退出");          

              fileMenu.add(openItem);

              fileMenu.add(saveItem);

              fileMenu.add(closeItem);

              bar.add(fileMenu);

              f.setMenuBar(bar);

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

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

              f.add(ta);

              myEvent();

              f.setVisible(true);

       }

       private void myEvent()

       {     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.flush();

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

//                          System.out.println(dirPath+"..."+fileName);

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

                                   return ;

                            ta.setText("");

                            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("读取失败");              }

                     }

              });

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

}******************************************************************************

八、制作双击执行的jar

1,将多个类封装到了一个包(package)中。

2,定义一个jar包的配置信息。

       定义一个文件a.txt 。文件内容内容为:

       Main-Class:(空格)包名.类名(回车)

3,打jar包。

       jar -cvfm my.jar a.txt 包名

4,通过winrar程序进行验证,查看该jar的配置文件中是否有自定义的配置信息。

5,通过工具--文件夹选项--文件类型--jar类型文件,通过高级,定义该jar类型文件的打开动作的关联程序。

       jdk\bin\javaw.exe -jar

6,双击试试!。哦了。

<a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

详细请查看:<ahref="http://edu.csdn.net" target="blank"> http://edu.csdn.net</a>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么需要GUI? 我们前面编写的程序命令行的,用户的体验度极差。 抽象窗口工具包(Abstract Window Tookit)是为java程序提供建立图形用户界面(Graphics User Interface GUI)的工具集。主要包含如下组件: 1. 用户界面组件 2. 事件处理模型 3. 图形图像工具(形状、颜色、字体) 4. 布局管理器 AWT的优势: 1. 最早的JAVA GUI包,内嵌到JDK中,无需引入其他类,功能稳定 2. AWT组件都是线程安全的 3. 界面编程基础. 学会AWT, 很容易去学swing/SWT等功能较强大的GUI组件. AWT的缺点: 1. 组件的创建完全依赖操作系统实现,导致不同的操作系统下出现不同的外观。 说白了,awt的组件是由操作系统绘制的,我们也说awt组件是重量级的组件。 这个工具包提供了一套与本地图形界面进行交互的接口。AWT 中的图形函数与操作系统所提供的图形函数之间有着一一对应的关系,我们把它称为peers。也就是说,当我们利用 AWT 来构件图形用户界面的时候,我们实际上是在利用操作系统所提供的图形库。由于不同操作系统的图形库所提供的功能是不一样的,在一个平台上存在的功能在另外一个平台上则可能不存在。为了实现Java语言所宣称的"一次编译,到处运行"的概念,AWT 不得不通过牺牲功能来实现其平台无关性,也就是说,AWT 所提供的图形功能是各种通用型操作系统所提供的图形功能的交集。由于AWT 是依靠本地方法来实现其功能的,我们通常把AWT控件称为重量级控件。 AWT并没有为GUI组件提供真正的实现,而是调用运行平台的GUI组件来创建和平台一致的对等体peers,因此程序中Textarea实际上是windows的多行文本域组件的对等体,具有和他相同的行为。所以,你右键单击textarea会出现菜单… 2. 线程安全导致运行速度慢 3. 为了保证程序的可移植性,AWT组件集遵循最大公约数原则,即AWT只拥有所有平台上都存在的组件的公有集合。有些常用的组件不支持,比如:表、树、进度条等。字体也只能支持4种。 为什么还需要学习AWT? 实际开发中使用AWT的情况非常少,但是我们仍然有必要学习AWT。主要原因如下: 1. Swing是在AWT基础上构建的,事件模型和一些支持类(形状、颜色、字体)都一样。掌握AWT有利于后面学习SWING. Eclipse不是swing开发的,是swt开发的。 2. 学习一下GUI编程。事实上,编程思路和其他语言类似

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值