Java程序设计实用教程 by 朱战立 & 沈伟
学习笔记之JAVA多线程(http://www.cnblogs.com/pegasus923/p/3995855.html)
国庆休假前学习了多线程,休假花了两天时间把整本书学完了。看书还就得一鼓作气。
第一章 概述
第二章 JAVA语言基础
第三章 类和对象
1 package classAndObject; 2 3 public class MyMatrix 4 { 5 private int[][] table; 6 private int height; 7 private int width; 8 9 private void init(int m, int n) 10 { 11 table = new int[m][n]; 12 13 for (int i = 0; i < m; i ++) 14 for (int j = 0; j < n; j ++) 15 { 16 table[i][j] = (int)(Math.random() * 100); 17 } 18 } 19 20 public MyMatrix(int n) 21 { 22 height = n; 23 width = n; 24 this.init(height, width); 25 } 26 27 public MyMatrix(int m, int n) 28 { 29 height = m; 30 width = n; 31 this.init(height, width); 32 } 33 34 public int getHeight() 35 { 36 return height; 37 } 38 39 public int getWidth() 40 { 41 return width; 42 } 43 44 public int[][] getTable() 45 { 46 return table; 47 } 48 49 public MyMatrix add(MyMatrix b) 50 { 51 if (this.getHeight() != b.getHeight() && this.getWidth() != b.getWidth()) 52 { 53 System.out.println("the two matrix don't match"); 54 return null; 55 } 56 57 MyMatrix result = new MyMatrix(b.getHeight(), b.getWidth()); 58 for (int i = 0; i < b.getHeight(); i ++) 59 for (int j = 0; j < b.getWidth(); j ++) 60 { 61 result.table[i][j] = this.table[i][j] + b.table[i][j]; 62 } 63 64 return result; 65 } 66 67 public MyMatrix subtract(MyMatrix b) 68 { 69 if (this.getHeight() != b.getHeight() && this.getWidth() != b.getWidth()) 70 { 71 System.out.println("the two matrix don't match"); 72 return null; 73 } 74 75 MyMatrix result = new MyMatrix(b.getHeight(), b.getWidth()); 76 for (int i = 0; i < b.getHeight(); i ++) 77 for (int j = 0; j < b.getWidth(); j ++) 78 { 79 result.table[i][j] = this.table[i][j] - b.table[i][j]; 80 } 81 82 return result; 83 } 84 }
1 package classAndObject; 2 3 public class TestMyMatrix 4 { 5 public static void main(String[] args) 6 { 7 MyMatrix mm1 = new MyMatrix(4, 4); 8 MyMatrix mm2 = new MyMatrix(4, 4); 9 MyMatrix mm3 = new MyMatrix(4, 5); 10 MyMatrix mm4 = new MyMatrix(4, 5); 11 12 MyMatrix add_result = mm1.add(mm2); 13 int [][] add_table = add_result.getTable(); 14 MyMatrix subtract_result = mm3.subtract(mm4); 15 int [][] subtract_table = subtract_result.getTable(); 16 17 System.out.println("two matrix add result: "); 18 for (int i = 0; i < add_result.getHeight(); i ++) 19 { 20 for (int j = 0; j < add_result.getWidth(); j ++) 21 { 22 System.out.println(add_table[i][j] + " "); 23 } 24 System.out.println(); 25 } 26 27 System.out.println("two matrix subtract result: "); 28 for (int i = 0; i < subtract_result.getHeight(); i ++) 29 { 30 for (int j = 0; j < subtract_result.getWidth(); j ++) 31 { 32 System.out.println(subtract_table[i][j] + " "); 33 } 34 System.out.println(); 35 } 36 } 37 }
第四章 类与继承
1 package classAndInherit; 2 3 class Shape 4 { 5 public void draw() 6 { 7 System.out.println("Draw a Shape"); 8 } 9 } 10 11 class Circle extends Shape 12 { 13 public void draw() 14 { 15 System.out.println("draw a Circle"); 16 } 17 } 18 19 class Ellipse extends Shape 20 { 21 public void draw() 22 { 23 System.out.println("draw a Ellipse"); 24 } 25 } 26 27 public class FInherit 28 { 29 public static void main(String args[]) 30 { 31 Shape s = new Shape(); 32 Shape c = new Circle(); 33 Shape e = new Ellipse(); 34 35 s.draw(); 36 c.draw(); 37 e.draw(); 38 } 39 }
1 package classAndInherit; 2 3 public class MyInter implements PrintMessage 4 { 5 private String[] v; 6 private int i; 7 8 public MyInter() 9 { 10 v = new String[3]; 11 i = 0; 12 this.putMessage("Hello world!"); 13 this.putMessage("Hello China!"); 14 this.putMessage("Hello XSYU!"); 15 } 16 17 public void putMessage(String str) 18 { 19 v[i ++] = str; 20 } 21 22 @Override 23 public void printAllMessage() 24 { 25 for (int k = 0; k < v.length; k ++) 26 { 27 System.out.println(v[k]); 28 } 29 } 30 31 @Override 32 public void printLastMessage() 33 { 34 System.out.println(v[v.length - 1]); 35 } 36 37 @Override 38 public void printFirstMessage() 39 { 40 System.out.println(v[0]); 41 } 42 43 public static void main(String[] args) 44 { 45 MyInter mi = new MyInter(); 46 System.out.println("print all messages"); 47 mi.printAllMessage(); 48 System.out.println("print the first messages"); 49 mi.printFirstMessage(); 50 System.out.println("print the last messages"); 51 mi.printLastMessage(); 52 } 53 }
第五章 JAVA API基础
1 package javaAPI; 2 3 public class SystemTest 4 { 5 public static void main(String[] args) 6 { 7 System.out.println("java.version: " + System.getProperty("java.version")); 8 9 System.out.println("java.vm.version: " + System.getProperty("java.vm.version")); 10 11 System.out.println("java.class.path: " + System.getProperty("java.class.path")); 12 13 System.out.println("os.version: " + System.getProperty("os.version")); 14 15 System.out.println("user.name: " + System.getProperty("user.name")); 16 17 System.out.println("user.dir: " + System.getProperty("user.dir")); 18 19 System.out.println("user.home: " + System.getProperty("user.home")); 20 } 21 22 }
1 package javaAPI; 2 3 public class RuntimeTest 4 { 5 public static void main(String[] args) 6 { 7 Runtime rtime = Runtime.getRuntime(); 8 long totalMemory = rtime.totalMemory() / 1024; 9 long freeMemory = rtime.freeMemory() / 1024; 10 11 System.out.println("totalMemory: " + totalMemory + "KB"); 12 System.out.println("freeMemory: " + freeMemory + "KB"); 13 } 14 }
1 package javaAPI; 2 3 import java.util.*; 4 5 public class MyDate 6 { 7 public static void main(String[] args) 8 { 9 Date date = new Date(); 10 11 System.out.println(date); 12 13 Calendar now = Calendar.getInstance(); 14 int year = now.get(Calendar.YEAR); 15 int month = now.get(Calendar.MONTH) + 1; 16 int day = now.get(Calendar.DATE); 17 18 System.out.println(year + "-" + month + "-" + day); 19 20 int hour = now.get(Calendar.HOUR); 21 int minute = now.get(Calendar.MINUTE); 22 int second = now.get(Calendar.SECOND); 23 24 System.out.println(hour + ":" + minute + ":" + second); 25 26 int week = now.get(Calendar.DAY_OF_WEEK); 27 String str = "日一二三四五六"; 28 int i = week - 1; 29 30 System.out.println("星期" + str.substring(i, i + 1)); 31 } 32 }
1 package javaAPI; 2 3 import java.util.*; 4 5 public class Exam5_7 6 { 7 public static void main(String[] args) 8 { 9 final int SIZE = 10; 10 int i; 11 int[] a = new int[SIZE]; 12 13 for (i = 0; i < a.length; i ++) 14 { 15 a[i] = (int)(Math.random() * 100); 16 } 17 18 System.out.println("Before sort: "); 19 for (i = 0; i < a.length; i ++) 20 { 21 System.out.println(a[i] + " "); 22 } 23 24 Arrays.sort(a); 25 26 System.out.println("\nAfter sort: "); 27 for (i = 0; i < a.length; i ++) 28 { 29 System.out.println(a[i] + " "); 30 } 31 } 32 }
1 package javaAPI; 2 3 import java.util.*; 4 5 public class Josephus 6 { 7 public static void main(String[] args) 8 { 9 int n = 5, s = 0, d = 2; 10 Vector v = new Vector(n); 11 12 for (int i = 0; i < n; i ++) 13 v.add(new Integer(i + 1)); 14 15 Enumeration e = v.elements(); 16 while (e.hasMoreElements()) 17 System.out.println(e.nextElement()); 18 19 int current = s - 1; 20 while (v.size() > 1) 21 { 22 System.out.println("Vector: "); 23 for (int i = 0; i < v.size(); i ++) 24 System.out.println(((Integer)v.get(i)).intValue() + " "); 25 26 int j = 0; 27 while (j < d) 28 { 29 current = (current + 1) % v.size(); 30 j ++; 31 } 32 33 System.out.println("\tcurrent = " + current + " "); 34 v.remove(current); 35 } 36 37 System.out.println("The survivor is " + ((Integer)v.get(0)).intValue()); 38 } 39 }
第六章 图形用户界面
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestFrame extends Frame 6 { 7 public static void main(String[] args) 8 { 9 TestFrame f = new TestFrame(); 10 f.setTitle("My First Frame"); 11 f.setSize(260, 160); 12 f.setLayout(new FlowLayout()); 13 f.setLocation(0, 0); 14 f.setResizable(false); 15 16 Label lb1 = new Label(); 17 lb1.setAlignment(Label.LEFT); 18 lb1.setText("My First Label"); 19 f.add(lb1); 20 21 Button b1 = new Button("My First Button"); 22 f.add(b1); 23 24 TextField t = new TextField(); 25 t.setText("My First TextField"); 26 f.add(t); 27 28 TextArea t1 = new TextArea("My First TextArea", 3, 20); 29 t1.setEditable(false); 30 f.add(t1); 31 f.setVisible(true); 32 } 33 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestCheckbox extends Frame 6 { 7 public static void main(String[] args) 8 { 9 TestCheckbox f = new TestCheckbox(); 10 f.setTitle("Test Checkbox"); 11 f.setSize(300, 200); 12 f.setLayout(null); 13 14 Checkbox checkbox1 = new Checkbox("Computer", true); 15 Checkbox checkbox2 = new Checkbox("Finance", true); 16 Checkbox checkbox3 = new Checkbox("Management", false); 17 18 checkbox1.setBounds(10, 30, 60, 20); 19 checkbox2.setBounds(10, 50, 60, 20); 20 checkbox3.setBounds(10, 70, 60, 20); 21 f.add(checkbox1); 22 f.add(checkbox2); 23 f.add(checkbox3); 24 25 CheckboxGroup zct = new CheckboxGroup(); 26 Checkbox c1 = new Checkbox("Maths", zct, false); 27 Checkbox c2 = new Checkbox("English", zct, true); 28 Checkbox c3 = new Checkbox("Arts", zct, false); 29 Checkbox c4 = new Checkbox("Physics", zct, false); 30 c4.setCheckboxGroup(zct); 31 c1.setBounds(150, 30, 60, 20); 32 c2.setBounds(150, 50, 60, 20); 33 c3.setBounds(150, 70, 60, 20); 34 c4.setBounds(150, 90, 60, 20); 35 f.add(c1); 36 f.add(c2); 37 f.add(c3); 38 f.add(c4); 39 f.setVisible(true); 40 } 41 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestChoiceList extends Frame 6 { 7 public static void main(String[] args) 8 { 9 TestChoiceList f = new TestChoiceList(); 10 f.setTitle("Test ChoiceList"); 11 f.setSize(300, 200); 12 f.setLayout(new FlowLayout()); 13 14 List list1 = new List(); 15 list1.setMultipleMode(true); 16 list1.add("Liberty"); 17 list1.add("Maths"); 18 list1.add("Arts"); 19 list1.add("Physics"); 20 list1.add("KongFu"); 21 list1.select(2); 22 list1.select(4); 23 f.add(list1); 24 25 Choice choice1 = new Choice(); 26 choice1.add("University"); 27 choice1.add("Middle School"); 28 choice1.add("Primary School"); 29 choice1.add("Kindergarden"); 30 choice1.select(2); 31 f.add(choice1); 32 f.setVisible(true); 33 } 34 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestMenu extends Frame 6 { 7 public TestMenu() {} 8 9 public static void main(String[] args) 10 { 11 TestMenu frame1 = new TestMenu(); 12 frame1.setTitle("My Menu"); 13 frame1.setSize(200, 120); 14 15 MenuBar menubar1 = new MenuBar(); 16 Menu menu1 = new Menu("School"); 17 Menu menu2 = new Menu("Childcare"); 18 MenuItem menuitem1 = new MenuItem("University"); 19 MenuItem menuitem2 = new MenuItem("Middle School"); 20 MenuItem menuitem3 = new MenuItem("Primary School"); 21 MenuItem menuitem4 = new MenuItem("Kindergarden"); 22 MenuItem menuitem5 = new MenuItem("Childcare"); 23 menubar1.add(menu1); 24 menubar1.add(menu2); 25 menu1.add(menuitem1); 26 menu1.add(menuitem2); 27 menu1.addSeparator(); 28 menu1.add(menuitem3); 29 menu2.add(menuitem4); 30 menu2.add(menuitem5); 31 frame1.setMenuBar(menubar1); 32 frame1.setVisible(true); 33 } 34 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestGraphics extends Frame 6 { 7 public void paint(Graphics g) 8 { 9 int x, y, i = 0; 10 Font font = new Font("Serif", Font.ITALIC|Font.BOLD, 40); 11 12 g.setFont(font); 13 g.drawOval(60, 50, 80, 80); 14 g.setColor(Color.red); 15 g.fillOval(150, 50, 80, 80); 16 g.setColor(Color.pink); 17 g.drawOval(240, 50, 80, 80); 18 g.setColor(Color.green); 19 g.drawString("I love Java", 80, 260); 20 g.setColor(Color.blue); 21 for (i = 0; i < 20; i ++) 22 { 23 x = (int)(Math.random() * 300) + 30; 24 y = (int)(Math.random() * 200) + 130; 25 g.fillOval(x, y, 10, 10); 26 } 27 g.setColor(Color.orange); 28 y = 100; 29 for (i = 0; i < 40; i ++) 30 { 31 y += 5; 32 g.drawRect(30, 30, 320, y); 33 } 34 } 35 36 public static void main(String[] args) 37 { 38 TestGraphics f = new TestGraphics(); 39 f.setTitle("My First Graphics"); 40 f.setSize(400, 345); 41 f.setLocation(0, 0); 42 f.setVisible(true); 43 } 44 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestFlowLayout extends Frame 6 { 7 public static void main(String[] args) 8 { 9 TestFlowLayout f = new TestFlowLayout(); 10 11 f.setTitle("My FlowLayout Manager"); 12 f.setSize(200, 120); 13 14 FlowLayout fl = new FlowLayout(); 15 fl.setHgap(10); 16 f.setLayout(fl); 17 f.add(new Button("OK")); 18 f.add(new Button("Cancel")); 19 f.add(new Button("Password")); 20 f.add(new Button("Reset")); 21 f.setVisible(true); 22 } 23 }
1 package gUI; 2 3 import java.awt.*; 4 5 public class TestBorderLayout extends Frame 6 { 7 public static void main(String[] args) 8 { 9 TestBorderLayout frame1 = new TestBorderLayout(); 10 11 frame1.setTitle("My BorderLayout"); 12 frame1.setSize(200, 200); 13 14 BorderLayout border = new BorderLayout(5, 10); 15 16 frame1.setLayout(border); 17 frame1.add(new Button("South"), BorderLayout.SOUTH); 18 frame1.add(new Button("West"), BorderLayout.WEST); 19 frame1.add(new Button("North"), BorderLayout.NORTH); 20 frame1.add(new Button("East"), BorderLayout.EAST); 21 frame1.add(new Button("Center"), BorderLayout.CENTER); 22 frame1.setVisible(true); 23 } 24 }
1 package gUI; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 public class PhoneBook extends WindowAdapter implements ActionListener, ItemListener 7 { 8 Frame f; 9 TextField tf1, tf2; 10 List l; 11 Button b1, b2; 12 13 public PhoneBook() 14 { 15 f = new Frame("PhoneBook"); 16 f.setSize(640, 480); 17 18 Panel p = new Panel(); 19 p.add(new Label("Name")); 20 tf1 = new TextField(10); 21 p.add(tf1); 22 p.add(new Label("PhoneNumber")); 23 tf2 = new TextField(20); 24 p.add(tf2); 25 b1 = new Button("Add"); 26 b2 = new Button("Delete"); 27 b1.addActionListener(this); 28 b2.addActionListener(this); 29 p.add(b1); 30 p.add(b2); 31 f.add(p, "North"); 32 33 l = new List(); 34 l.add("Name PhoneNumber"); 35 l.addItemListener(this); 36 f.add(l); 37 38 f.setVisible(true); 39 f.addWindowListener(this); 40 } 41 42 public void actionPerformed(ActionEvent e) 43 { 44 if (e.getSource() == b1) 45 l.add(tf1.getText() + " " + tf2.getText()); 46 47 if (e.getSource() == b2) 48 l.remove(l.getSelectedIndex()); 49 } 50 51 public void itemStateChanged(ItemEvent e) 52 { 53 String str = l.getSelectedItem(); 54 int i = str.indexOf(' '); 55 tf1.setText(str.substring(0, i)); 56 str = str.substring(i); 57 str = str.trim(); 58 tf2.setText(str); 59 } 60 61 public void windowClosing(WindowEvent e) 62 { 63 System.exit(0); 64 } 65 66 public static void main(String[] args) 67 { 68 new PhoneBook(); 69 } 70 }
1 package gUI; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 public class RGBColor extends WindowAdapter implements TextListener 7 { 8 Frame f; 9 TextField tf1, tf2, tf3; 10 Panel p2; 11 12 public RGBColor() 13 { 14 f = new Frame("Mixed color"); 15 f.setSize(500, 200); 16 17 Panel p1 = new Panel(); 18 p2 = new Panel(); 19 f.add(p1, "North"); 20 f.add(p2); 21 p1.add(new Label("Red")); 22 tf1 = new TextField("255", 10); 23 p1.add(tf1); 24 25 p1.add(new Label("Green")); 26 tf2 = new TextField("0", 10); 27 p1.add(tf2); 28 29 p1.add(new Label("Blue")); 30 tf3 = new TextField("0", 10); 31 p1.add(tf3); 32 33 tf1.addTextListener(this); 34 tf2.addTextListener(this); 35 tf3.addTextListener(this); 36 37 p2.setBackground(new Color(255, 0, 0)); 38 39 f.setVisible(true); 40 f.addWindowListener(this); 41 } 42 43 public void textValueChanged(TextEvent e) 44 { 45 int r = (new Integer(tf1.getText()).intValue()); 46 int g = (new Integer(tf2.getText()).intValue()); 47 int b = (new Integer(tf3.getText()).intValue()); 48 49 if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) 50 p2.setBackground(new Color(r, g, b)); 51 } 52 53 public void windownClosing(WindowEvent e) 54 { 55 System.exit(0); 56 } 57 58 public static void main(String[] args) 59 { 60 new RGBColor(); 61 } 62 }