javase_20(Awt初步认识)
多线程的再度复习.class
1 package com.javami.kudy.Demo.ThreadStudy; 2 3 import java.util.concurrent.locks.Condition; 4 import java.util.concurrent.locks.Lock; 5 import java.util.concurrent.locks.ReentrantLock; 6 7 8 /*class MyArray 9 { 10 缺点: 11 不能准确的去唤醒一个进程.消耗资源.并且要判断... 而下面的.可以准确的唤醒下一个需要执行的.. 12 private int arr[] = new int[10]; 13 private int savepos = 0; 14 private int getpos = 0; 15 private String lock = " "; 16 private int count = 0; 17 //数组的角标是从零开始的,如果关锁了,其他线程在等待.. 18 //但我不断的去抢到存的数据.那时候就乱套了... 19 //存满了,两个存的线程等待,一个取的线程取了一个元素会唤醒一个存的线程 20 //存的线程存了一个,又存满了,然而此时它会唤醒,就会唤醒另一个在等待的存的线程,出错了 21 //解决这个问题很简单,将线程等待的条件放在一个循环里面去判断 22 //而实际上wait方法允许发声虚假唤醒,所以最好放在一个循环里面 23 //但是像上面的做法,存的线程会去唤醒存的线程,没有必要,非常影响程序的效率 24 25 public void add(int num) throws InterruptedException 26 { //0 1 2 3 不while循环.有可能这里有两个存的进程在这里边等待 27 //我不一定要等待你执行完毕~~~但是线程同步,我在执行.你就不能执行..但是锁一开~~我就是while保证了虚假的 唤醒 28 synchronized (lock) { 29 while(count==10) 30 { 31 lock.wait(); 32 } 33 34 if(savepos==10) 35 savepos = 0; 36 arr[savepos++] = num; 37 count++; 38 lock.notify(); //唤醒等待中的进程 39 } 40 } 41 42 public int get() throws InterruptedException 43 { 44 synchronized(lock) 45 { 46 try 47 { 48 while(count==0){ 49 lock.wait(); 50 } 51 if(getpos==10) 52 getpos = 0; 53 count--; 54 return arr[getpos++]; 55 }finally 56 { 57 lock.notify(); 58 } 59 } 60 61 } 62 }*/ 63 64 65 //使用1.5的lock和condition解决存和取之间的通信问题 66 class MyArray 67 { 68 private int[] arr = new int[10]; 69 private int savapos = 0; 70 private int getpos = 0; 71 private int count = 0; 72 Lock lock = new ReentrantLock(); 73 Condition isFull = lock.newCondition(); //必须要获取同一把锁 74 Condition isEmpty = lock.newCondition(); 75 76 public void add(int num) throws InterruptedException 77 { 78 try { 79 lock.lock(); //开锁 80 while(count==10) 81 isFull.await(); //等待 82 if(savapos==10) 83 savapos = 0; 84 arr[savapos++] = num; 85 count++; 86 isEmpty.signal(); 87 } finally 88 { 89 lock.unlock();//关锁 90 } 91 92 } 93 94 public int get() throws InterruptedException 95 { 96 try { 97 lock.lock(); //开锁 98 while(count==0) 99 isEmpty.await(); 100 if(getpos==10) 101 getpos = 0; 102 count--; 103 return arr[getpos++]; 104 }finally 105 { 106 isFull.signal(); //唤醒下一个锁 107 lock.unlock(); //我才关闭 108 } 109 } 110 } 111 public class ArrayThread { 112 113 /** 114 * 写一个多线程的程序,实现两个线程存元素,两个线程取元素 115 */ 116 static int num = 0; 117 public static void main(String[] args) { 118 final MyArray marr = new MyArray(); 119 120 new Thread(new Runnable() { 121 public void run() { 122 for(int i=0; i<30; i++) { 123 try { 124 marr.add(num++); 125 } catch (InterruptedException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } 129 } 130 } 131 }).start(); 132 133 134 new Thread(new Runnable() { 135 public void run() { 136 for(int i=0; i<30; i++) 137 try { 138 System.out.println(marr.get()); 139 } catch (InterruptedException e) { 140 // TODO Auto-generated catch block 141 e.printStackTrace(); 142 } 143 } 144 }).start(); 145 146 147 new Thread(new Runnable() { 148 public void run() { 149 for(int i=0; i<30; i++) { 150 try { 151 marr.add(num++); 152 } catch (InterruptedException e) { 153 // TODO Auto-generated catch block 154 e.printStackTrace(); 155 } 156 } 157 } 158 }).start(); 159 160 new Thread(new Runnable() { 161 public void run() { 162 for(int i=0; i<30; i++) 163 try { 164 System.out.println(marr.get()); 165 } catch (InterruptedException e) { 166 // TODO Auto-generated catch block 167 e.printStackTrace(); 168 } 169 } 170 }).start(); 171 172 } 173 174 }
GUI(图形用户界面)
全称:Graphical User Interface。
Java为GUI提供的对象都存在Awt,Swing两个包中。
两个包的特点。
理解组件(Component(父类))与容器(Container(子类))。
Awt
Awt与 Swing
Awt:依赖于本地系统平台,如颜色样式显示。
Swing:跨平台。
组件与容器
容器是组件的子类,是一个特殊的组件。
组件里面不可以存放组件,而容器可以。
布局管理器
FlowLayout(流式布局管理器)
从左到右的顺序排列。
BorderLayout(边界布局管理器)
东,南,西,北,中
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
建立一个简单的窗体
1 Container常用子类:Window Panel(面板,不能单独存在。) 2 Window常用子类:Frame Dialog 3 简单的窗体创建过程: 4 Frame f = new Frame(“my window”); 5 f.setLayout(new FlowLayout()); 6 f.setSize(300,400); 7 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
添加一个按钮的初步认识:
1 package com.javami.kudyDemo.AwtTest; 2 import java.awt.Button; 3 import java.awt.FlowLayout; 4 import java.awt.Frame; 5 import java.awt.event.MouseAdapter; 6 import java.awt.event.MouseEvent; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 public class AddButton { 10 11 /** 12 * @param args 13 * 添加一个按钮,监听按钮.但按钮发生出异常的时候.我们应该做什么/. 14 */ 15 private static Frame f; 16 public static void main(String[] args) { 17 f = new Frame("kudy add Button"); 18 f.setSize(300, 400); 19 f.setLocation(100, 150); 20 f.setLayout(new FlowLayout()); //设置布局 21 Button bt = new Button("点我啊~"); 22 f.add(bt); 23 HandleEvent(f,bt); 24 f.setVisible(true);//可见的 25 } 26 27 private static void HandleEvent(Frame f, Button bt) { 28 f.addWindowListener(new WindowAdapter(){ 29 public void windowClosing(WindowEvent e) 30 { 31 e.getWindow().dispose(); 32 } 33 }); 34 35 //为按钮添加时间监,增加按钮 36 bt.addMouseListener(new MouseAdapter(){ 37 public void mouseClicked(MouseEvent e){ 38 addBtn(); 39 } 40 }); 41 } 42 43 //从外部实现~~ 44 protected static void addBtn() { 45 int num = 1; 46 Button bt = new Button("点就点~~"+num++); 47 f.add(bt); 48 f.setVisible(true); 49 bt.addMouseListener(new MouseAdapter(){ 50 public void mouseClicked(MouseEvent e) 51 { 52 53 Button b = (Button)e.getComponent(); 54 f.remove(b); 55 f.setVisible(true); 56 } 57 }); 58 } 59 60 } 61 62 63 //下面的内容是初懂的时候做的~~不好!! 64 package com.javami.kudyDemo.AwtTest; 65 import java.awt.Button; 66 import java.awt.Component; 67 import java.awt.FlowLayout; 68 import java.awt.Frame; 69 import java.awt.event.MouseEvent; 70 import java.awt.event.MouseListener; 71 import java.awt.event.WindowAdapter; 72 import java.awt.event.WindowEvent; 73 public class FrameTest3 { 74 /* 75 * 添加一个按钮,点击按钮就添加一个新的按钮--点击新的按钮. 76 * 容器继承于组件 77 */ 78 79 public static void main(String[]args) 80 { 81 Frame f = new Frame(); 82 f.setTitle("钟姑娘,相信老公好好奋斗.以后让你过上幸福的生活"); 83 Button bt = new Button("美怡说:爱我吗?"); 84 f.add(bt); 85 86 f.setLayout(new FlowLayout()); //设计窗口为流布式 87 f.setSize(400,200); 88 f.setVisible(true); 89 //监听窗口的事件 90 f.addWindowListener(new WindowAdapter(){ 91 public void windowClosing(WindowEvent e) 92 { 93 Frame f = (Frame)e.getWindow(); 94 f.dispose(); //当触发时间的时候.咱们就把你窗口关了 95 } 96 }); 97 98 bt.addMouseListener(new MyMouseListener(f)); 99 100 } 101 } 102 103 class MyMouseListener implements MouseListener 104 { 105 106 /* 107 * 组合模式~~~ 108 */ 109 Frame f ; 110 public MyMouseListener(Frame f) { 111 this.f = f; 112 } 113 @Override 114 public void mouseClicked(MouseEvent e) { 115 System.out.println("美怡说:爱我吗?"); 116 Component com = (Component)e.getSource(); 117 Button b = (Button)com; 118 b = new Button("威淏说:爱"); 119 f.add(b); 120 } 121 122 @Override 123 public void mouseEntered(MouseEvent e) { 124 // TODO Auto-generated method stub 125 126 } 127 128 @Override 129 public void mouseExited(MouseEvent e) { 130 // TODO Auto-generated method stub 131 132 } 133 134 @Override 135 public void mousePressed(MouseEvent e) { 136 // TODO Auto-generated method stub 137 138 } 139 140 @Override 141 public void mouseReleased(MouseEvent e) { 142 // TODO Auto-generated method stub 143 144 } 145 146 }
抓我啊~~小游戏~~嘿嘿:
1 package com.javami.kudyDemo.AwtTest; 2 3 import java.awt.Button; 4 import java.awt.Frame; 5 import java.awt.event.MouseAdapter; 6 import java.awt.event.MouseEvent; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 10 public class ButtonGame { 11 12 /** 13 * @param args 14 */ 15 private static Frame f; 16 public static void main(String[] args) { 17 f = new Frame("Button Game_Beta1.0"); 18 f.setSize(300, 400); 19 f.setLocation(100, 200); 20 Button btOne = new Button("抓我啊~~"); 21 Button btTwo = new Button("抓我啊~~"); 22 btTwo.setVisible(false); 23 f.add(btOne,"North"); 24 f.add(btTwo,"South"); 25 f.setVisible(true); 26 27 handEvent(f,btOne,btTwo); 28 } 29 private static void handEvent(Frame f2, final Button btOne, final Button btTwo) { 30 f2.addWindowListener(new WindowAdapter(){ 31 public void windowClosing(WindowEvent e) 32 { 33 e.getWindow().dispose(); 34 } 35 }); 36 btOne.addMouseListener(new MouseAdapter(){ 37 public void mouseEntered(MouseEvent e) 38 { 39 e.getComponent().setVisible(false); //事件源设计为不可见 40 btTwo.setVisible(true); 41 f.setVisible(true); 42 } 43 }); 44 btTwo.addMouseListener(new MouseAdapter(){ 45 public void mouseEntered(MouseEvent e) 46 { 47 e.getComponent().setVisible(false); //事件源设计为不可见 48 btOne.setVisible(true); 49 f.setVisible(true); 50 } 51 }); 52 } 53 54 }
简单的记事本实现功能(等待更新与完整)
1 package com.javami.kudyDemo.AwtTest; 2 3 import java.awt.Button; 4 import java.awt.Dialog; 5 import java.awt.FileDialog; 6 import java.awt.Frame; 7 import java.awt.Label; 8 import java.awt.Menu; 9 import java.awt.MenuBar; 10 import java.awt.MenuItem; 11 import java.awt.TextArea; 12 import java.awt.Window; 13 import java.awt.event.ActionEvent; 14 import java.awt.event.ActionListener; 15 import java.awt.event.KeyAdapter; 16 import java.awt.event.KeyEvent; 17 import java.awt.event.MouseAdapter; 18 import java.awt.event.MouseEvent; 19 import java.awt.event.WindowAdapter; 20 import java.awt.event.WindowEvent; 21 import java.io.BufferedReader; 22 import java.io.BufferedWriter; 23 import java.io.File; 24 import java.io.FileReader; 25 import java.io.FileWriter; 26 import java.io.IOException; 27 28 29 class MyMenu 30 { 31 private Frame f; 32 private MenuBar mb; //菜单条 33 private Menu fileMenu; //菜单栏部署的下拉式菜单组件。 34 private MenuItem open,save,close; 35 private TextArea text; 36 public MyMenu() 37 { 38 f = new Frame("kudy is notePad(Beta1.0)"); 39 f.setSize(500, 600); 40 f.setLocation(430, 120); 41 mb = new MenuBar(); 42 fileMenu = new Menu("File"); 43 open = new MenuItem("Open(N) Ctrl+N"); 44 save = new MenuItem("Save(S) Ctrl+S"); 45 close = new MenuItem("Close(X)Ctrl+X"); 46 text = new TextArea(100,120); 47 //把下拉组件添加到菜单栏下面 48 fileMenu.add(open); 49 fileMenu.add(save); 50 fileMenu.add(close); 51 mb.add(fileMenu); 52 f.setMenuBar(mb); 53 f.add(text); 54 f.setVisible(true); 55 handieEvent(); 56 } 57 58 private void handieEvent() { 59 f.addWindowListener(new WindowAdapter(){ 60 public void windowClosing(WindowEvent e) 61 { 62 e.getWindow().dispose();//释放资源 63 } 64 }); 65 open.addActionListener(new ActionListener(){ 66 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 openFileDialog(); 70 } 71 72 }); 73 save.addActionListener(new ActionListener(){ 74 75 @Override 76 public void actionPerformed(ActionEvent e) { 77 SaveFileDialog(); 78 } 79 } 80 81 ); 82 close.addActionListener(new ActionListener(){ 83 84 @Override 85 public void actionPerformed(ActionEvent e) { 86 f.dispose();//直接退出 87 } 88 89 }); 90 91 /* 92 * 93 * 键盘的监听器 94 * 95 */ 96 text.addKeyListener(new KeyAdapter(){ 97 public void keyPressed(KeyEvent e) 98 { 99 //监听打开一个文件快捷键 100 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_N) 101 openFileDialog(); 102 //监听另存为快捷键-->83 103 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_S) 104 SaveFileDialog(); 105 //退出怎么监听呢?各位大牛~~ 106 } 107 }); 108 } 109 110 protected void SaveFileDialog() { 111 //1.创建保存对话框(写入) 112 FileDialog saveDialog = new FileDialog(f,"save as",FileDialog.SAVE); 113 //2.设置对话框可见 114 saveDialog.setVisible(true); 115 String dirName = saveDialog.getDirectory();//获取目录 116 String fileName = saveDialog.getFile();//获取文件 117 File file = new File(dirName,fileName); 118 try { 119 saveFile(file); 120 } catch (IOException e1) { 121 // TODO Auto-generated catch block 122 e1.printStackTrace(); 123 } 124 } 125 126 /* 127 * 另存为功能的实现 128 */ 129 protected void saveFile(File file) throws IOException { 130 BufferedWriter bw = null; 131 try 132 { 133 bw = new BufferedWriter(new FileWriter(file)); 134 String data = text.getText(); 135 bw.write(data); //不需要换行.由于我们在读取数据的时候已经换行了 136 }finally 137 { 138 if(bw!=null) 139 bw.close(); 140 } 141 } 142 143 protected void openFileDialog() { 144 //1.创建一个文件对话框对象 145 FileDialog openDialog = new FileDialog(f,"file",FileDialog.LOAD); 146 //2.设置对话框为可见,会发生阻塞,直到用户选中文件 147 openDialog.setVisible(true); 148 //3.获取用户选中的文件所有的目录和文件的文件名 149 String dirName = openDialog.getDirectory(); 150 String fileName = openDialog.getFile(); 151 //4.创建File对象 152 File file = new File(dirName,fileName); 153 //5.判断file是否存在,如果不存在,弹出错误的面板 154 if(!file.exists()){ 155 //如果不存在,创建一个错误的面板 156 openErrorDialog(file); 157 return;//结束 158 } 159 //6.通过Io流将文件内容读取进来,存入Text 160 try { 161 openFile(file); 162 } catch (IOException e) { 163 // TODO Auto-generated catch block 164 e.printStackTrace(); 165 } 166 } 167 168 169 private void openFile(File file) throws IOException { 170 //1.创建流文件 171 BufferedReader br = null; 172 try 173 { 174 br = new BufferedReader(new FileReader(file)); 175 //2.清空文本域..把之前的内容请空 176 text.setText(""); 177 //3.while读取,读一行,存一行 178 String line; 179 while((line=br.readLine())!=null) 180 { 181 text.append(line); 182 text.append("\r\n");//读完一行换行 183 } 184 }finally 185 { 186 if(br!=null) 187 br.close(); 188 } 189 } 190 191 /* 192 * 错误的对话框内容 193 */ 194 private void openErrorDialog(File file) { 195 Dialog error = new Dialog(f,"error!",true); 196 error.setSize(300,100); 197 error.setLocation(180, 250); 198 //Label 对象是一个可在容器中放置文本的组件。一个标签只显示一行只读文本。文本可由应用程序更改,但是用户不能直接对其进行编辑。 199 error.add(new Label("Sorry,文件不存在 \t"+ 200 file.getName()),"Center"); 201 Button bt = new Button("Confirm"); 202 error.add(bt,"South"); 203 bt.addMouseListener(new MouseAdapter(){ 204 //鼠标监听器 205 public void mouseClicked(MouseEvent e) 206 { 207 ((Window)(e.getComponent().getParent())).dispose(); 208 } 209 }); 210 error.setVisible(true); 211 } 212 } 213 214 215 public class MyMenuTest { 216 217 /** 218 * 记事本工具的实现 219 */ 220 public static void main(String[] args) { 221 MyMenu my = new MyMenu(); 222 } 223 224 }
简单的资源管理器(还有很多功能没有做好啦~~)
1 package com.javami.kudyDemo.AwtTest; 2 3 import java.awt.Button; 4 import java.awt.Frame; 5 import java.awt.List; 6 import java.awt.Panel; 7 import java.awt.TextField; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.awt.event.MouseAdapter; 11 import java.awt.event.MouseEvent; 12 import java.awt.event.WindowAdapter; 13 import java.awt.event.WindowEvent; 14 import java.io.File; 15 16 class MyList 17 { 18 private Frame f; 19 //TextField 对象是允许编辑单行文本的文本组件。 20 private TextField tf; 21 //按钮 22 private Button bt; 23 //List 组件为用户提供了一个可滚动的文本项列表。可设置此 list,使其允许用户进行单项或多项选择。 24 private List l; 25 private Panel p; 26 public MyList() 27 { 28 f = new Frame("资源管理Beta1.0"); 29 f.setSize(400,500); 30 f.setLocation(100, 100); //位置 31 32 tf = new TextField(42); 33 bt = new Button("Go~!"); 34 l = new List(40); 35 36 p = new Panel(); //面板 37 p.add(tf,"West");//南 38 p.add(bt,"East");//东 39 p.add(l,"Center");//中 40 41 f.add(p,"North"); //北 42 f.add(l,"Center"); 43 f.setVisible(true); 44 45 handleEvent(); 46 } 47 48 49 //触发事件 50 private void handleEvent() { 51 f.addWindowListener(new WindowAdapter(){ 52 public void windowClosing(WindowEvent e) 53 { 54 e.getWindow().dispose(); //关闭 55 } 56 }); 57 bt.addMouseListener(new MouseAdapter(){ 58 public void mouseClicked(MouseEvent e) 59 { 60 disPlayFile(); 61 } 62 }); 63 64 //监听List 65 l.addActionListener(new ActionListener(){ 66 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 //获取一个文件名 70 String fileName = l.getSelectedItem(); 71 //获得文件的所在目录 72 String dirName = tf.getText(); 73 if(dirName.endsWith(":")); 74 dirName+="\\"; 75 //创建File对象 76 File file = new File(dirName,fileName); 77 //判断是目录还是标准的文件 78 if(file.isDirectory()) 79 { 80 //取出文件名,将文件名给文本域 81 tf.setText(file.getAbsolutePath()); 82 // 83 disPlayFile();//又走入下一个目录 84 }else 85 { 86 try 87 { 88 Runtime.getRuntime().exec("cmd /c " + 89 file.getAbsolutePath()); 90 }catch(Exception ex) 91 { 92 ex.printStackTrace(); 93 System.out.println("打开失败"); 94 } 95 } 96 } 97 98 }); 99 } 100 101 102 protected void disPlayFile() { 103 //1.获得文本域输入的内容 104 String dirName = tf.getText(); 105 //2.创建File对象 106 //如果是以冒号结尾,应该加上:\\ 107 if(dirName.endsWith(":")) 108 dirName+="\\"; 109 File dir = new File(dirName); 110 if(!dir.isDirectory()) 111 return ; 112 //4.如果是目录,遍历目录下所有的文件 113 String[]fileNames = dir.list(); 114 //5.list要清空 115 l.removeAll(); 116 for(String fileName :fileNames ) 117 l.add(fileName); 118 } 119 } 120 public class FileList { 121 public static void main(String[]args) 122 { 123 MyList ml = new MyList(); 124 } 125 }
个人学习心得:
总体来说都坚持过来了~~但是时间方面处理不够好~~由于晚上精神不是很好!!代码需要复习.重要的是掌握好思路..
加油..