前言
代码仅供复习查看,只收集整理了重要知识点
仅个人能看懂,不保证能正常运行
具体怎么实现还得看基本功了
Comparable接口实现排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class TCountry implements Comparable{ int GoldNum; int SilverNum; int BronzeNum; public int compareTo(Object o) { TCountry obj = (TCountry)o; if(this.GoldNum != obj.GoldNum) { return -this.GoldNum + obj.GoldNum; } if(this.GoldNum == obj.GoldNum && this.SilverNum != obj.SilverNum) { return -this.SilverNum + obj.SilverNum; } else { return -this.BronzeNum + obj.BronzeNum; } }
}
|
日历类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package work2;
import java.util.Calendar;
public class TMyCalendar {
void ShowMyCalendar(int Difference) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, Difference); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); cal.set(Calendar.DAY_OF_MONTH,1); int day = cal.get(Calendar.DAY_OF_MONTH); int day_w = cal.get(Calendar.DAY_OF_WEEK); cal.roll(Calendar.DAY_OF_MONTH,false); int daynum = cal.get(Calendar.DAY_OF_MONTH); System.out.println(year + "年" + (month+1) + "月"); System.out.println("\t日\t一\t二\t三\t四\t五\t六\t"); this.PrintSpace(day_w); for(int i=0;i<daynum;i++) { if(day_w != 8) { System.out.print(day+"\t"); } else { day_w = 1; System.out.println(); System.out.print("\t"+day+"\t"); } day++; day_w++; } } void PrintSpace(int day_w) { System.out.print("\t"); if(day_w == 2) { for(int i=0;i<1;i++) { System.out.print("\t"); } } if(day_w == 3) { for(int i=0;i<2;i++) { System.out.print("\t"); } } if(day_w == 4) { for(int i=0;i<3;i++) { System.out.print("\t"); } } if(day_w == 5) { for(int i=0;i<4;i++) { System.out.print("\t"); } } if(day_w == 6) { for(int i=0;i<5;i++) { System.out.print("\t"); } } if(day_w == 7) { for(int i=0;i<6;i++) { System.out.print("\t"); } } if(day_w == 1) { } } }
|
自定义异常类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class TMyException {
}
|
txt文件复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public class TTXTCopy { File Poem1; File Poem2; File newPoem;
TTXTCopy(){ Poem1 = new File(".\\files\\poem\\Poem1.txt"); Poem2 = new File(".\\files\\poem\\Poem2.txt"); newPoem = new File(".\\files\\poem\\李白诗集.txt"); if(!newPoem.exists()) { try { newPoem.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } int n=0; char buf[] = new char[10]; try { FileReader FR = new FileReader(Poem1); FileWriter FW = new FileWriter(newPoem); while((n = FR.read(buf)) != -1) { FW.write(buf,0,n); } FW.write("\n\n"); FR.close(); FR = new FileReader(Poem2); while((n = FR.read(buf)) != -1) { FW.write(buf,0,n); } FW.write("\n\t\t\t\t————李白诗集"); FR.close(); FW.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("创建完成"); }
public static void main(String[] args) {
}
}
|
文件复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| public class TFileCopy { File Source; File Dest; TFileCopy() { Source = new File(".\\source"); Dest = new File(".\\dest"); } void ShowList() { String[] lst = Source.list(); for(int i=0;i<lst.length;i++) { System.out.println(lst[i]); } } void Copy_FileInOutputStream(String name) { File obj = new File(Source, name); if(!obj.exists()) { System.out.println("File doesn't exist: " + name); return; } else { try { File result = new File(Dest, name); if(!result.exists()) { result.createNewFile(); } FileInputStream FIS = new FileInputStream(obj); FileOutputStream FOS = new FileOutputStream(result); int n=0; byte buf[] = new byte[10]; while((n = FIS.read(buf)) != -1) { FOS.write(buf,0,n); } System.out.println(name+" copied successfully"); FIS.close(); FOS.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } void CopyFolder(String FileName, String DestName, String ParentFileName) { File obj = new File(ParentFileName + "\\" + FileName); File result = new File(DestName + "\\" + FileName); if(obj.isDirectory()) { result.mkdir(); System.out.println("Folder: " + FileName + " copied successfully"); String lst[] = obj.list(); for(int i=0;i<lst.length;i++) { CopyFolder(lst[i], DestName+"\\"+FileName, ParentFileName + "\\" +FileName); } return; } else { try { if(!result.exists()) { result.createNewFile(); } FileInputStream FIS = new FileInputStream(obj); FileOutputStream FOS = new FileOutputStream(result); int n=0; byte buf[] = new byte[10]; while((n = FIS.read(buf)) != -1) { FOS.write(buf,0,n); } System.out.println("File: " + FileName+" copied successfully"); FIS.close(); FOS.close(); } catch (IOException e) { e.printStackTrace(); } return; } } void CopyFolderTo(String FileName, String DestName) { File obj = new File(FileName); String lst[] = obj.list(); for(int i=0;i<lst.length;i++) { CopyFolder(lst[i], ".\\filescopy", ".\\files"); } System.out.println("All files copied successfully"); } void Operation() { while(true) { System.out.print("Please input a command: "); Scanner reader = new Scanner(System.in); String operation = reader.nextLine(); if(operation.equals("list")) { ShowList(); } else if(operation.equals("exit")) { System.out.println("System exits."); break; } else { String name = operation.substring(5); operation = operation.substring(0, 4); if(operation.equals("copy")) { Copy_FileInOutputStream(name); } else { System.out.println("Wrong command"); } } System.out.println(""); } } }
|
数据库操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| public class TDBOperation { Connection con; Statement sql; public TDBOperation() { this.con = null; this.sql = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("加载驱动成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("加载驱动失败"); } String url = "jdbc:mysql://localhost:3306/test?" + "&useSSL=false" + "&characterEncoding=UTF-8" + "&serverTimezone=UTC"; String user = "root"; String password = "123"; try { con = java.sql.DriverManager.getConnection(url,user,password); System.out.println("连接成功"); } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); } } void InsertData() { try { if(this.sql == null) { sql = con.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } String InsertStatement1 = "insert into employee (id,name,sex,salary) values (1002,'Tom','male',2600)"; String InsertStatement2 = "insert into employee (id,name,sex,salary) values (1003,'Mary','female',3200)"; String InsertStatement3 = "insert into employee (id,name,sex,salary) values (1004,'Peter','male',3000)"; String InsertStatement4 = "insert into employee (id,name,sex,salary) values (1005,'John','male',7000)"; String InsertStatement5 = "insert into employee (id,name,sex,salary) values (1006,'Paul','male',4000)"; try { sql.executeUpdate(InsertStatement1); sql.executeUpdate(InsertStatement2); sql.executeUpdate(InsertStatement3); sql.executeUpdate(InsertStatement4); sql.executeUpdate(InsertStatement5); System.out.println("插入成功"); } catch (SQLException e) { e.printStackTrace(); } } void UpdataAllSalary(int add) { try { if(this.sql == null) { sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); } } catch (SQLException e) { e.printStackTrace(); } String AddStatement = "update employee set salary=salary+" + add; try { sql.executeUpdate(AddStatement); System.out.println("所有人工资加" + add); } catch (SQLException e) { e.printStackTrace(); } } void DeletePeter() { try { if(this.sql == null) { sql = con.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } try { String DeleteStatement = "delete from employee where name='Peter'"; sql.executeUpdate(DeleteStatement); System.out.println("所有Peter已被开除"); } catch (SQLException e) { e.printStackTrace(); } } void Search() { try { if(this.sql == null) { sql = con.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } try { ResultSet rs = null; String SearchStatement = "select * from employee where salary>=4000"; rs = sql.executeQuery(SearchStatement); while(rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String sex = rs.getString("sex"); int salary = rs.getInt("salary"); System.out.println("ID: " + id + " Name: " + name + " Sex: " + sex + " Salary: " + salary); } System.out.println("成功输出所有工资大于等于4000"); } catch (SQLException e) { e.printStackTrace(); } } void DeleteAll() { try { if(this.sql == null) { sql = con.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } try { String DeleteAllStatement = "delete from employee"; sql.executeUpdate(DeleteAllStatement); System.out.println("全部删除"); } catch (SQLException e) { e.printStackTrace(); } } void CloseConnection() { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { TDBOperation test = new TDBOperation(); test.InsertData(); test.Search(); test.UpdataAllSalary(1500); test.DeletePeter(); test.CloseConnection(); }
}
|
数据库查找界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| public class TDBFrame extends JFrame{
JButton B1; JButton B2; JTextField T1; JTextField T2; JTextField T3; JTextField T4; DB_Check Checker = new DB_Check(); ActionListener ButtonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == B1) { String ID = T1.getText(); String name; int price; String date; try { String SearchStatement = "select * from product where productID='" + ID +"'"; ResultSet rs = Checker.sql.executeQuery(SearchStatement); rs.next(); name = rs.getString("name"); price = rs.getInt("price"); date = rs.getString("date"); T2.setText(name); T3.setText("" + price); T4.setText(date); } catch (SQLException e1) { e1.printStackTrace(); } } if(e.getSource() == B2) { T1.setText(""); T2.setText(""); T3.setText(""); T4.setText(""); } } }; TDBFrame(){ Init(); setSize(550,450); setLocationRelativeTo(null); setTitle("汽车信息查询"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void Init() { setLayout(null); } public class DB_Check { Connection con; Statement sql;
public DB_Check() { this.con = null; this.sql = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("加载驱动成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("加载驱动失败"); } String url = "jdbc:mysql://localhost:3306/test?" + "&useSSL=false" + "&characterEncoding=UTF-8" + "&serverTimezone=UTC"; String user = "root"; String password = "123"; try { con = java.sql.DriverManager.getConnection(url,user,password); System.out.println("连接成功"); } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); } try { sql = con.createStatement(); System.out.println("已创建statement"); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TDBFrame(); }
}
|
多线程文件复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class TThreadCopy extends Thread{ String name; TThreadCopy(String name){ super("复制"); this.name = name; } TThreadCopy(){ super("复制"); this.name = ""; } public void run() { File source = new File(".\\source"); File obj = new File(source,name); if(!obj.exists()) { System.out.println("文件"+name+"不存在"); return; } File dest = new File(".\\dest"); File result = new File(dest,name); if(!result.exists()) { try { result.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { FileInputStream FIS = new FileInputStream(obj); FileOutputStream FOS = new FileOutputStream(result); int n=0; byte buf[] = new byte[10]; while((n = FIS.read(buf)) != -1) { FOS.write(buf,0,n); } System.out.println(name+" copied successfully"); FIS.close(); FOS.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { File source = new File(".\\source"); String[] lst = source.list(); TThreadCopy[] ThreadList = new TThreadCopy[lst.length]; for(int i=0;i<lst.length;i++) { ThreadList[i] = new TThreadCopy(lst[i]); } for(int i=0;i<lst.length;i++) { ThreadList[i].start(); } } }
|
多线程卖票
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class TThreadWorkShop implements Runnable{
int total = 800; @Override public void run() { int num=0; while(total>0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this) { if(total>0) { System.out.println(Thread.currentThread().getName() + "卖出第" + total + "张票"); total--; num++; } } } System.out.println(Thread.currentThread().getName()+"总共卖出"+num+"张票"); } public static void main(String[] args) { TThreadWorkShop Target = new TThreadWorkShop(); Thread WorkThread1 = new Thread(Target,"WorkShop1"); Thread WorkThread2 = new Thread(Target,"WorkShop2"); Thread WorkThread3 = new Thread(Target,"WorkShop3"); Thread WorkThread4 = new Thread(Target,"WorkShop4"); WorkThread1.start(); WorkThread2.start(); WorkThread3.start(); WorkThread4.start(); } }
|
多线程经典生产者消费者问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| package work7;
public class TThreadProducer_Consumer {
public static void main(String[] args) { GoodsShelf Shelf = new GoodsShelf(); Producer p1 = new Producer("生产者1"); Producer p2 = new Producer("生产者2"); Consumer c1 = new Consumer("消费者1"); Consumer c2 = new Consumer("消费者2"); Consumer c3 = new Consumer("消费者3"); p1.setShelf(Shelf); p2.setShelf(Shelf); c1.setShelf(Shelf); c2.setShelf(Shelf); c3.setShelf(Shelf); p1.start(); p2.start(); c1.start(); c2.start(); c3.start(); } }
public class Consumer extends Thread{
GoodsShelf Shelf; public Consumer(String name) { super(name); } void setShelf(GoodsShelf Shelf) { this.Shelf = Shelf; } void Consume() { Shelf.remove(); try { Thread.sleep(7000); } catch (InterruptedException e) { e.printStackTrace(); } } public void run() { while(true) { Consume(); } } } public class Producer extends Thread{ GoodsShelf Shelf; public Producer(String name) { super(name); } void setShelf(GoodsShelf Shelf) { this.Shelf = Shelf; } void Produce() { Shelf.add(new Goods()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } public void run() { while(true) { Produce(); } } } public class GoodsShelf { Goods Shelf[]; int index; int DefaultSize; public GoodsShelf() { DefaultSize = 10; Shelf = new Goods[DefaultSize]; index = -1; } boolean IsEmpty() { return index == -1; } boolean IsFull() { return index == DefaultSize-1; } synchronized void add(Goods goods) { while(IsFull()) { try { System.out.println("货架已满,"+Thread.currentThread().getName()+"无法继续生产"); this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } index++; Shelf[index] = goods; System.out.println(Thread.currentThread().getName()+"添加了新商品"); notifyAll(); } synchronized void remove() { while(IsEmpty()) { try { System.out.println("货架已空,"+Thread.currentThread().getName()+"无法购买"); this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } index--; System.out.println(Thread.currentThread().getName()+"购买了商品"); notifyAll(); } }
|
文件复制客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| public class TFileCopyClient { Socket socket = null; DataInputStream DIS = null; DataOutputStream DOS = null; FileOutputStream FOS = null; public TFileCopyClient() { try { socket = new Socket("127.0.0.1",2018); System.out.println("连接服务器成功"); DIS = new DataInputStream(socket.getInputStream()); DOS = new DataOutputStream(socket.getOutputStream()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("连接服务器失败"); } } void getFileNames() { try { int len = DIS.readInt(); System.out.println("文件名如下: "); for(int i=0;i<len;i++) { System.out.println(DIS.readUTF()); } } catch (IOException e) { e.printStackTrace(); } } void Interact() { getFileNames(); Scanner scanner = new Scanner(System.in); File DestFolder = new File(".\\destination"); File result = null; while(true) { try { System.out.println("请输入一个文件名"); String name = scanner.nextLine(); DOS.writeUTF(name); if(DIS.readBoolean() == false) { System.out.println("文件不存在,无法复制"); continue; } result = new File(DestFolder,name); result.createNewFile(); FOS = new FileOutputStream(result); int n = 0; byte buf[] = new byte[10000]; while((n = DIS.readInt()) != -1) { DIS.read(buf); FOS.write(buf,0,n); DOS.writeInt(1); } System.out.println(name + " copied successfully"); FOS.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TFileCopyClient().Interact(); } }
|
文件复制服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| public class TFileCopyServer { ServerSocket sSocket; File SourceFolder = null; String lst[] = null; TFileCopyServer() { try { sSocket = new ServerSocket(2018); SourceFolder = new File(".\\source"); lst = SourceFolder.list(); } catch (IOException e) { e.printStackTrace(); } } class ClientThread extends Thread { Socket socket = null; DataInputStream DIS = null; DataOutputStream DOS = null; FileInputStream FIS = null; File obj = null; ClientThread(Socket socket){ super(); this.socket = socket; try { DIS = new DataInputStream(socket.getInputStream()); DOS = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { System.out.println(socket + "断开连接"); } } void SendFileNames() { try { DOS.writeInt(lst.length); for(int i=0;i<lst.length;i++) { DOS.writeUTF(lst[i]); } } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { SendFileNames(); while(true) { try { String name = null; name = DIS.readUTF(); obj = new File(SourceFolder,name); if(!obj.exists()) { System.out.println("文件不存在"); DOS.writeBoolean(false); continue; } else { DOS.writeBoolean(true); } FIS = new FileInputStream(obj); int n = 0; byte buf[] = new byte[10000]; while((n = FIS.read(buf)) != -1) { DOS.writeInt(n); DOS.write(buf,0,n); DIS.readInt(); } DOS.writeInt(n); FIS.close(); System.out.println("已完成端口为"+socket.getPort()+"客户端的一次复制操作"); } catch (IOException e) { e.printStackTrace(); } } } } void AcceptClient() { while(true) { try { Socket socket = null; socket = sSocket.accept(); System.out.println("客户端成功连接,端口为: " + socket.getPort()); new ClientThread(socket).start(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TFileCopyServer().AcceptClient(); }
}
|
三角形计算客户端界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| public class TTriangleClientFrame extends JFrame{ JTextArea Area; Socket socket = null; DataInputStream DIS = null; DataOutputStream DOS = null; int Connectable = 0; public TTriangleClientFrame() { Init(); AddFunction(); setSize(550,450); setLocationRelativeTo(null); setTitle("客户端"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void Init() { setLayout(new BorderLayout()); JPanel North = new JPanel(); North.setLayout(new BorderLayout()); JPanel CenterPanel = new JPanel(); CenterPanel.setLayout(new FlowLayout()); L1 = new JLabel("SideA"); T1 = new JTextField(10); L2 = new JLabel("SideB"); T2 = new JTextField(10); L3 = new JLabel("SideC"); T3 = new JTextField(10); B2 = new JButton("Send"); CenterPanel.add(L1); CenterPanel.add(T1); CenterPanel.add(L2); CenterPanel.add(T2); CenterPanel.add(L3); CenterPanel.add(T3); CenterPanel.add(B2); B1 = new JButton("连接到服务器"); North.add(B1,BorderLayout.NORTH); North.add(CenterPanel,BorderLayout.CENTER); Area = new JTextArea(); JScrollPane Center = new JScrollPane(Area); add(North,BorderLayout.NORTH); add(Center,BorderLayout.CENTER); B2.setEnabled(false); }
void AddFunction() { B1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Connectable == 0) { try { InetSocketAddress ISA = new InetSocketAddress("127.0.0.1",2018); socket = new Socket(); socket.connect(ISA); Area.append("服务器连接成功,当前端口地址: " + socket.getLocalSocketAddress() + '\n'); try { DIS = new DataInputStream(socket.getInputStream()); DOS = new DataOutputStream(socket.getOutputStream()); } catch (IOException e1) { e1.printStackTrace(); } } catch (IOException e1) { e1.printStackTrace(); } Connectable = 1; B1.setEnabled(false); B2.setEnabled(true); } } }); B2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Double a = Double.parseDouble(T1.getText()); Double b = Double.parseDouble(T2.getText()); Double c = Double.parseDouble(T3.getText()); try { DOS.writeDouble(a); DOS.writeDouble(b); DOS.writeDouble(c); Area.append(DIS.readUTF() + '\n'); } catch (IOException e1) { e1.printStackTrace(); } } }); }
public static void main(String[] args) { new TTriangleClientFrame(); } }
|
UDP聊天软件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public class TUDPAorB {
public static void main(String[] args) { SendThread_A ST = new SendThread_A(); ReceiveThread_A RT = new ReceiveThread_A(); ST.start(); RT.start(); System.out.println("A已启动"); }
} class SendThread_A extends Thread{ DatagramSocket sendS = null; InetSocketAddress sa = null; public SendThread_A(){ super(); try { sendS = new DatagramSocket(); sa = new InetSocketAddress("127.0.0.1",8888); } catch (SocketException e) { e.printStackTrace(); } } public void run() { while(true) { try { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); byte []data = str.getBytes(); DatagramPacket outP = new DatagramPacket(data,data.length,sa); sendS.send(outP); } catch (IOException e) { e.printStackTrace(); } } } } class ReceiveThread_A extends Thread{ DatagramSocket receiveS = null; public ReceiveThread_A() { super(); try { receiveS = new DatagramSocket(8889); } catch (SocketException e) { e.printStackTrace(); } } public void run() { while(true) { try { byte []buf = new byte[100]; DatagramPacket inP = new DatagramPacket(buf, 100); receiveS.receive(inP); String data = new String(buf,0,inP.getLength()); if(data.equals("TIME")) { System.out.println(new Date().toString()); continue; } else { System.out.println("B: " + data); } } catch (IOException e) { e.printStackTrace(); } } } }
|
考后总结
本来想“得救之道,就在其中”的,结果失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了失败了
令人感叹,多线程服务器客户端交互没能完全实现,考前没有着重复习
不过就花了一晚上看Java,平时学得挺好来着,没花多久时间复习Java,算是给考试周节约了不少时间
不过幸亏没能“得救之道,就在其中”,书上对象流输入输出讲的比自己写的更详细,因祸得福了