多线程的再度复习.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 blocke.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 blocke.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 blocke.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 blocke.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

菜单

1 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;}@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("美怡说:爱我吗?");Component com =  (Component)e.getSource();Button b = (Button)com;b = new Button("威淏说:爱");f.add(b);}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic 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(){@Overridepublic void actionPerformed(ActionEvent e) {openFileDialog();}});save.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {SaveFileDialog();}});close.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {f.dispose();//直接退出}});/** * 键盘的监听器* */text.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){//监听打开一个文件快捷键if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_N)openFileDialog();//监听另存为快捷键-->83if(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 blocke1.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流将文件内容读取进来,存入Texttry {openFile(file);} catch (IOException e) {// TODO Auto-generated catch blocke.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();}});//监听Listl.addActionListener(new ActionListener(){@Overridepublic 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();}}

个人学习心得:

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

加油..

javase_20(Awt初步认识)相关推荐

  1. 【JAVA】贪吃蛇的初步实现(五)

    JAVA实现贪吃蛇游戏的实践记录(五)[完结] 一.总结: 二.功能扩展报告: 三.预期功能展望: 四.游戏效果展示: 五.程序代码展示: 本文链接 相关文章(一) 相关文章(二) 相关文章(三) 相 ...

  2. Java创意画板初步

    ** Java创意画板初步 ** Swing 体系 样例工具 SwingSet2.jar 下载链接:https://download.csdn.net/download/qq_41892714/108 ...

  3. Java初学练手小项目---基于awt库,swing库以及MySQL数据库制作简易电影管理系统(一)

    前言 本人是个小小白,初学Java语言,想与一众身为程序猿的各位分享一下自己的知识和想法,达到共同学习的目的,所以想通过写博客的方式分享自己的心得体会,这也是本人第一次写博客,希望能够帮助同样在学习的 ...

  4. TensorRT 7.2.1开发初步

    TensorRT 7.2.1开发初步 TensorRT 7.2.1开发人员指南演示了如何使用C ++和Python API来实现最常见的深度学习层.它显示了如何采用深度学习框架构建现有模型,并使用该模 ...

  5. SOC,System on-a-Chip技术初步

    SOC,System on-a-Chip技术初步 S O C(拼作S-O-C)是一种集成电路,它包含了电子系统在单个芯片上所需的所有电路和组件.它可以与传统的计算机系统形成对比,后者由许多不同的组件组 ...

  6. 《OpenCV3编程入门》学习笔记3 HighGUI图形用户界面初步

    第3章 HighGUI图形用户界面初步 3.1 图像的载入.显示和输出到文件 1.OpenCV命名空间2种访问方法 (1)代码开头加:usingnamespace cv; (2)每个类或函数前加:cv ...

  7. java basicstroke_使用java.awt.BasicStroke动画化虚线

    使用虚线,线程(或Swing Timer)&将它们与repaint()结合起来,并对破折号的起点和终点进行一些调整 – 然后就可以了. 例 package test; import java. ...

  8. 初步判断内存泄漏方法

    有时候,内存泄漏不明显,或者怀疑系统有内存泄漏,我们可以通过下面介绍的方法初步确认系统是否存在内存泄漏. 首先在Java命令行中增加-verbose:gc参数, 然后重新启动java进程. 当系统运行 ...

  9. android蓝牙4.0(BLE)开发之ibeacon初步

    一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...

最新文章

  1. SpringBoot b2b2c 多用户商城系统 ssm b2b2c
  2. Apache服务器部署(1)
  3. 15-shell 输入/输出重定向
  4. java获取整点与凌晨的时间戳
  5. python跨函数调用变量_对python中不同模块(函数、类、变量)的调用详解
  6. 使用 ADO 向数据库中存储一张图片
  7. opencv3.4.2调用训练好的Openpose模型
  8. 如何在Azure Data Studio中导出数据库?
  9. msn自身头像存放位置
  10. matlab二元积分函数求导,多元函数求积分求导.ppt
  11. 【SQL】小CASE
  12. redis源码--SDS结构解析
  13. C语言度量代码质量常用指标,代码度量标准
  14. 雷神笔记本关闭触摸板
  15. python实时检测键盘输入函数
  16. 电脑中找不到ie浏览器怎么办
  17. 电商相关:SKU概念
  18. AES实现加解密-Java
  19. Python 自动化办公:Excel 自动绘制图表
  20. System.Diagnostics 记录

热门文章

  1. java计算机毕业设计人口普查信息管理系统源代码+数据库+系统+lw文档
  2. hmmlearn源代码
  3. 结构变异SV的鉴定--smartie-sv与bayestyper
  4. 基于语音的情绪识别系统(Python)
  5. HDFS加密存储(Ranger集成KMS方式)
  6. 论ArcGIS10.2的Band Collection Statistics工具计算相关系数的正确性
  7. 2019/01/29 一位前端实习生 艰辛过程 励志 实习周记(五)——第六周
  8. Java核心技术第一周学习总结
  9. svn服务器现存的库文件导入,svn导入版本库及相关知识
  10. java飞行棋项目_Java实现飞行棋 - 源码下载|行业应用软件|教育/学校应用|源代码 - 源码中国...