`
_kudy
  • 浏览: 15186 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

javase_20(Awt初步认识)

 
阅读更多

多线程的再度复习.class

package com.javami.kudy.Demo.ThreadStudy;
 
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 
 /*class MyArray
 {
     缺点:
     不能准确的去唤醒一个进程.消耗资源.并且要判断...  而下面的.可以准确的唤醒下一个需要执行的..
     private int arr[] = new int[10];
     private int savepos = 0;
     private int getpos = 0;
     private String lock = " ";
     private int count = 0;
     //数组的角标是从零开始的,如果关锁了,其他线程在等待..
     //但我不断的去抢到存的数据.那时候就乱套了...
     //存满了,两个存的线程等待,一个取的线程取了一个元素会唤醒一个存的线程
     //存的线程存了一个,又存满了,然而此时它会唤醒,就会唤醒另一个在等待的存的线程,出错了
     //解决这个问题很简单,将线程等待的条件放在一个循环里面去判断
     //而实际上wait方法允许发声虚假唤醒,所以最好放在一个循环里面
     //但是像上面的做法,存的线程会去唤醒存的线程,没有必要,非常影响程序的效率
 
     public void add(int num) throws InterruptedException 
     { //0 1 2 3   不while循环.有可能这里有两个存的进程在这里边等待
       //我不一定要等待你执行完毕~~~但是线程同步,我在执行.你就不能执行..但是锁一开~~我就是while保证了虚假的 唤醒
         synchronized (lock) {
             while(count==10)
             {
                 lock.wait();
             }
 
             if(savepos==10)
                 savepos = 0;
             arr[savepos++] = num;
             count++;
             lock.notify(); //唤醒等待中的进程
         }
     }
     
     public int get() throws InterruptedException 
     {
         synchronized(lock)
         {
             try
             {
             while(count==0){
                     lock.wait();
             }
             if(getpos==10)
                 getpos = 0;
             count--;
             return arr[getpos++];
             }finally
             {
                 lock.notify();
             }
         }
         
     }
 }*/
 
 
 //使用1.5的lock和condition解决存和取之间的通信问题
 class MyArray
 {
     private int[] arr = new int[10];
     private int savapos = 0;
     private int getpos = 0;
     private int count = 0;
     Lock lock = new ReentrantLock();
     Condition isFull = lock.newCondition(); //必须要获取同一把锁
     Condition isEmpty = lock.newCondition();
     
     public void add(int num) throws InterruptedException
     {
         try {
             lock.lock(); //开锁
             while(count==10)
                 isFull.await();  //等待
             if(savapos==10)
                 savapos = 0;
             arr[savapos++] = num;
             count++;
             isEmpty.signal();
         } finally
         {
             lock.unlock();//关锁
         }
 
     }
     
     public int get() throws InterruptedException
     {
         try {
             lock.lock(); //开锁
             while(count==0) 
                 isEmpty.await();
             if(getpos==10)
                 getpos = 0;
             count--;
             return arr[getpos++];
         }finally
         {
             isFull.signal();  //唤醒下一个锁
             lock.unlock(); //我才关闭
         }
     }
 }
 public class ArrayThread {
 
     /**
      * 写一个多线程的程序,实现两个线程存元素,两个线程取元素
      */
     static int num = 0;
     public static void main(String[] args) {
         final MyArray marr = new MyArray();
         
         new Thread(new Runnable() {
             public void run() {
                 for(int i=0; i<30; i++) {
                     try {
                         marr.add(num++);
                     } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
                 }
             }
         }).start();
         
 
         new Thread(new Runnable() {
             public void run() {
                 for(int i=0; i<30; i++)
                     try {
                         System.out.println(marr.get());
                     } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
             }
         }).start();
         
         
         new Thread(new Runnable() {
             public void run() {
                 for(int i=0; i<30; i++) {
                     try {
                         marr.add(num++);
                     } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
                 }
             }
         }).start();
         
         new Thread(new Runnable() {
             public void run() {
                 for(int i=0; i<30; i++)
                     try {
                         System.out.println(marr.get());
                     } catch (InterruptedException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
             }
         }).start();
         
     }
 
 }

 

GUI(图形用户界面)

全称:Graphical User Interface。

Java为GUI提供的对象都存在Awt,Swing两个包中。

两个包的特点。

理解组件(Component(父类))与容器(Container(子类))。

Awt

Awt与 Swing
Awt:依赖于本地系统平台,如颜色样式显示。
Swing:跨平台。

组件与容器
容器是组件的子类,是一个特殊的组件。
组件里面不可以存放组件,而容器可以。

布局管理器

FlowLayout(流式布局管理器)
从左到右的顺序排列。
BorderLayout(边界布局管理器)
东,南,西,北,中
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵

建立一个简单的窗体

Container常用子类:Window   Panel(面板,不能单独存在。)
 Window常用子类:Frame  Dialog
 简单的窗体创建过程:
 Frame  f = new Frame(“my window”);
 f.setLayout(new FlowLayout());
 f.setSize(300,400);
 f.setVisible(true);

 所有的AWT包中的类会运行在AWT线程上

 事件处理机制组成

事件
 用户对组件的一个操作,称之为一个事件

事件源
 发生事件的组件就是事件源

事件处理器
 某个Java类中负责处理事件的成员方法

事件分类

按产生事件的物理操作和GUI组件的表现效果进行分类:

1 MouseEvent
2 WindowEvent
3 ActionEvent
4       ……

 

按事件的性质分类:
低级事件
语义事件(又叫做高级事件)

 事件监听机制组成

事件源
 发生事件的组件对象
事件
 具体发生的事件
监听器
 监听器需要注册到具体的对象上,用于监听该对象上发生的事件
事件处理
 针对某一动作的具体处理办法

事件监听机制 

确定事件源(容器或组件)

通过事件源对象的addXXXListener(new XXXListener())方法将侦听器注册到该事件源上。

该方法中接收XXXListener的子类对象,或者XXXListener的对应的适配器XXXAdapter的子类对象。

一般用匿名内部类来实现。

在覆盖方法的时候,方法的形参一般是XXXEvent类型的变量。

事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)

事件监听机制的设计

 Event
Listener
Adapter

 

菜单

MenuBar,Menu,MenuItem
主要的认识~~

 

添加一个按钮的初步认识:

package com.javami.kudyDemo.AwtTest;
 import java.awt.Button;
 import java.awt.FlowLayout;
 import java.awt.Frame;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 public class AddButton {
 
     /**
      * @param args
      * 添加一个按钮,监听按钮.但按钮发生出异常的时候.我们应该做什么/.
      */
     private static Frame f;
     public static void main(String[] args) {
          f = new Frame("kudy add Button");
         f.setSize(300, 400);
         f.setLocation(100, 150);
         f.setLayout(new FlowLayout()); //设置布局
         Button bt = new Button("点我啊~");
         f.add(bt);
         HandleEvent(f,bt);
         f.setVisible(true);//可见的
     }
 
     private static void HandleEvent(Frame f, Button bt) {
         f.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
                 e.getWindow().dispose();
             }
         });
         
         //为按钮添加时间监,增加按钮
         bt.addMouseListener(new MouseAdapter(){
             public void mouseClicked(MouseEvent e){
                 addBtn();
             }
         });
     }
     
     //从外部实现~~
     protected static void addBtn() {
         int num = 1;
         Button bt = new Button("点就点~~"+num++);
         f.add(bt);
         f.setVisible(true);
         bt.addMouseListener(new MouseAdapter(){
             public void mouseClicked(MouseEvent e)
             {
                 
                 Button b = (Button)e.getComponent();
                 f.remove(b);
                 f.setVisible(true);
             }
         });
     }
 
 }
 
 
 //下面的内容是初懂的时候做的~~不好!!
 package com.javami.kudyDemo.AwtTest;
 import java.awt.Button;
 import java.awt.Component;
 import java.awt.FlowLayout;
 import java.awt.Frame;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 public class FrameTest3 {
     /*    
      * 添加一个按钮,点击按钮就添加一个新的按钮--点击新的按钮.
      * 容器继承于组件
      */
     
     public static void main(String[]args)
     {
         Frame f = new Frame();
         f.setTitle("钟姑娘,相信老公好好奋斗.以后让你过上幸福的生活");
         Button bt = new Button("美怡说:爱我吗?");
         f.add(bt);
         
         f.setLayout(new FlowLayout()); //设计窗口为流布式
         f.setSize(400,200);
         f.setVisible(true);
         //监听窗口的事件
         f.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
                 Frame f = (Frame)e.getWindow();
                 f.dispose(); //当触发时间的时候.咱们就把你窗口关了
             }
         });
         
             bt.addMouseListener(new MyMouseListener(f));
             
     }
 }
 
 class MyMouseListener implements MouseListener
 {
     
     /*
      * 组合模式~~~
      */
     Frame f ;
     public MyMouseListener(Frame f) {
         this.f = f;
     }
     @Override
     public void mouseClicked(MouseEvent e) {
         System.out.println("美怡说:爱我吗?");
         Component com =  (Component)e.getSource();
         Button b = (Button)com;
         b = new Button("威淏说:爱");
         f.add(b);
     }
 
     @Override
     public void mouseEntered(MouseEvent e) {
         // TODO Auto-generated method stub
         
     }
 
     @Override
     public void mouseExited(MouseEvent e) {
         // TODO Auto-generated method stub
         
     }
 
     @Override
     public void mousePressed(MouseEvent e) {
         // TODO Auto-generated method stub
         
     }
 
     @Override
     public void mouseReleased(MouseEvent e) {
         // TODO Auto-generated method stub
         
     }
     
 }

 

抓我啊~~小游戏~~嘿嘿:

package com.javami.kudyDemo.AwtTest;
 
 import java.awt.Button;
 import java.awt.Frame;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 
 public class ButtonGame {
 
     /**
      * @param args
      */
     private static Frame f; 
     public static void main(String[] args) {
         f = new Frame("Button Game_Beta1.0");
         f.setSize(300, 400);
         f.setLocation(100, 200);
         Button btOne = new Button("抓我啊~~");
         Button btTwo = new Button("抓我啊~~");
         btTwo.setVisible(false);
         f.add(btOne,"North");
         f.add(btTwo,"South");
         f.setVisible(true);
         
         handEvent(f,btOne,btTwo);
     }
     private static void handEvent(Frame f2, final Button btOne, final Button btTwo) {
         f2.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
                 e.getWindow().dispose();
             }
         });
         btOne.addMouseListener(new MouseAdapter(){
             public void mouseEntered(MouseEvent e)
             {
                 e.getComponent().setVisible(false); //事件源设计为不可见
                 btTwo.setVisible(true);
                 f.setVisible(true);
             }
         });
         btTwo.addMouseListener(new MouseAdapter(){
             public void mouseEntered(MouseEvent e)
             {
                 e.getComponent().setVisible(false); //事件源设计为不可见
                 btOne.setVisible(true);
                 f.setVisible(true);
             }
         });
     }
 
 }

 

简单的记事本实现功能(等待更新与完整)

package com.javami.kudyDemo.AwtTest;
 
 import java.awt.Button;
 import java.awt.Dialog;
 import java.awt.FileDialog;
 import java.awt.Frame;
 import java.awt.Label;
 import java.awt.Menu;
 import java.awt.MenuBar;
 import java.awt.MenuItem;
 import java.awt.TextArea;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 
 
 class MyMenu 
 {
     private Frame f; 
     private MenuBar mb; //菜单条
     private Menu fileMenu; //菜单栏部署的下拉式菜单组件。 
     private MenuItem open,save,close;
     private TextArea text;
     public MyMenu()
     {
         f = new Frame("kudy is notePad(Beta1.0)");
         f.setSize(500, 600);
         f.setLocation(430, 120);
         mb = new MenuBar();
         fileMenu = new Menu("File");
         open = new MenuItem("Open(N) Ctrl+N");
         save = new MenuItem("Save(S) Ctrl+S");
         close = new MenuItem("Close(X)Ctrl+X");
         text = new TextArea(100,120);
         //把下拉组件添加到菜单栏下面
         fileMenu.add(open);
         fileMenu.add(save);
         fileMenu.add(close);
         mb.add(fileMenu);
         f.setMenuBar(mb);
         f.add(text);
         f.setVisible(true);
         handieEvent();
     }
     
     private void handieEvent() {
         f.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
                 e.getWindow().dispose();//释放资源
             }
         });
         open.addActionListener(new ActionListener(){
 
             @Override
             public void actionPerformed(ActionEvent e) {
                 openFileDialog();
             }
             
         });
         save.addActionListener(new ActionListener(){
 
             @Override
             public void actionPerformed(ActionEvent e) {
                     SaveFileDialog();
             }
         }
             
         );
         close.addActionListener(new ActionListener(){
 
             @Override
             public void actionPerformed(ActionEvent e) {
                 f.dispose();//直接退出
             }
             
         });
         
         /*
          * 
          * 键盘的监听器
          * 
          */
         text.addKeyListener(new KeyAdapter(){
             public void keyPressed(KeyEvent e)
             {
                 //监听打开一个文件快捷键
                 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_N)
                     openFileDialog();
                 //监听另存为快捷键-->83
                 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_S)
                     SaveFileDialog();
                 //退出怎么监听呢?各位大牛~~
             }
         });
     }
 
     protected void SaveFileDialog() {
         //1.创建保存对话框(写入)
         FileDialog saveDialog = new FileDialog(f,"save as",FileDialog.SAVE);
         //2.设置对话框可见
         saveDialog.setVisible(true);                
         String dirName = saveDialog.getDirectory();//获取目录
         String fileName = saveDialog.getFile();//获取文件
         File file = new File(dirName,fileName);
         try {
             saveFile(file);
         } catch (IOException e1) {
             // TODO Auto-generated catch block
             e1.printStackTrace();
         }
     }
     
     /*
      * 另存为功能的实现
      */
     protected void saveFile(File file) throws IOException {
         BufferedWriter bw = null;
         try
         {
             bw = new BufferedWriter(new FileWriter(file));
             String data = text.getText();
             bw.write(data); //不需要换行.由于我们在读取数据的时候已经换行了
         }finally
         {
             if(bw!=null)
                 bw.close();
         }
     }
 
     protected void openFileDialog() {
         //1.创建一个文件对话框对象
         FileDialog openDialog = new FileDialog(f,"file",FileDialog.LOAD);
         //2.设置对话框为可见,会发生阻塞,直到用户选中文件
         openDialog.setVisible(true);
         //3.获取用户选中的文件所有的目录和文件的文件名
         String dirName = openDialog.getDirectory();
         String fileName = openDialog.getFile();
         //4.创建File对象
         File file = new File(dirName,fileName);
         //5.判断file是否存在,如果不存在,弹出错误的面板
         if(!file.exists()){
             //如果不存在,创建一个错误的面板
             openErrorDialog(file);
             return;//结束
         }
         //6.通过Io流将文件内容读取进来,存入Text
         try {
             openFile(file);
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
     
     
     private void openFile(File file) throws IOException {
         //1.创建流文件
         BufferedReader br = null;
         try
         {
             br = new BufferedReader(new FileReader(file));
             //2.清空文本域..把之前的内容请空
             text.setText("");
             //3.while读取,读一行,存一行
             String line;
             while((line=br.readLine())!=null)
             {
                 text.append(line);
                 text.append("\r\n");//读完一行换行
             }
         }finally
         {
             if(br!=null)
                 br.close();
         }
     }
 
     /*
      * 错误的对话框内容
      */
     private void openErrorDialog(File file) {
         Dialog error = new Dialog(f,"error!",true);
         error.setSize(300,100);
         error.setLocation(180, 250);
         //Label 对象是一个可在容器中放置文本的组件。一个标签只显示一行只读文本。文本可由应用程序更改,但是用户不能直接对其进行编辑。 
         error.add(new Label("Sorry,文件不存在 \t"+
                             file.getName()),"Center");
         Button bt = new Button("Confirm");
         error.add(bt,"South");
         bt.addMouseListener(new MouseAdapter(){
             //鼠标监听器
             public void mouseClicked(MouseEvent e)
             {
                 ((Window)(e.getComponent().getParent())).dispose();
             }
         });
         error.setVisible(true);
     }
 }
 
 
 public class MyMenuTest {
 
     /**
      * 记事本工具的实现
      */
     public static void main(String[] args) {
         MyMenu my = new MyMenu();
     }
 
 }

 

简单的资源管理器(还有很多功能没有做好啦~~)

package com.javami.kudyDemo.AwtTest;
 
 import java.awt.Button;
 import java.awt.Frame;
 import java.awt.List;
 import java.awt.Panel;
 import java.awt.TextField;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.File;
 
 class MyList
 {
     private Frame f;
     //TextField 对象是允许编辑单行文本的文本组件。
     private TextField tf;
     //按钮
     private Button bt;
     //List 组件为用户提供了一个可滚动的文本项列表。可设置此 list,使其允许用户进行单项或多项选择。 
     private List l;
     private Panel p;
     public MyList()
     {
         f = new Frame("资源管理Beta1.0");
         f.setSize(400,500);
         f.setLocation(100, 100); //位置
         
         tf = new TextField(42);
         bt = new Button("Go~!");
         l = new List(40);
         
         p = new Panel(); //面板
         p.add(tf,"West");//南
         p.add(bt,"East");//东
         p.add(l,"Center");//中
         
         f.add(p,"North"); //北
         f.add(l,"Center");
         f.setVisible(true);
         
         handleEvent();
     }
     
     
     //触发事件
     private void handleEvent() {
         f.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e)
             {
                 e.getWindow().dispose(); //关闭
             }
         });
         bt.addMouseListener(new MouseAdapter(){
             public void mouseClicked(MouseEvent e)
             {
                 disPlayFile();
             }
         });
         
         //监听List
         l.addActionListener(new ActionListener(){
 
             @Override
             public void actionPerformed(ActionEvent e) {
                 //获取一个文件名
                 String fileName = l.getSelectedItem();
                 //获得文件的所在目录
                 String dirName = tf.getText();
                 if(dirName.endsWith(":"));
                 dirName+="\\";
                 //创建File对象
                 File file = new File(dirName,fileName);
                 //判断是目录还是标准的文件
                 if(file.isDirectory())
                 {
                     //取出文件名,将文件名给文本域
                     tf.setText(file.getAbsolutePath());
                     //
                     disPlayFile();//又走入下一个目录
                 }else
                 {
                     try
                     {
                         Runtime.getRuntime().exec("cmd /c " +
                                             file.getAbsolutePath());
                     }catch(Exception ex)
                     {
                         ex.printStackTrace();
                         System.out.println("打开失败");
                     }
                 }
             }
             
         });
     }
 
 
     protected void disPlayFile() {
         //1.获得文本域输入的内容
         String dirName = tf.getText();
         //2.创建File对象
         //如果是以冒号结尾,应该加上:\\
         if(dirName.endsWith(":"))
             dirName+="\\";
         File dir = new File(dirName);
         if(!dir.isDirectory())
             return ;
         //4.如果是目录,遍历目录下所有的文件
         String[]fileNames = dir.list();
         //5.list要清空
         l.removeAll();
         for(String fileName :fileNames )
                 l.add(fileName);
     }
 }
 public class FileList {
     public static void main(String[]args)
     {
         MyList ml = new MyList();
     }
 }

 

个人学习心得:

总体来说都坚持过来了~~但是时间方面处理不够好~~由于晚上精神不是很好!!代码需要复习.重要的是掌握好思路..

加油..

坚持相信都会有回报的啦~~~

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics