java基础学习笔记7

    科技2022-07-10  160

    java基础学习笔记7

    网络

    Ip地址

    四个字节 每个字节最大到255

    提供独一无二的IP地址

    TCP

    可靠,传输慢

    UDP

    不可靠,传输慢。QQ聊天

    Socket

    网络编程也叫Socket编程

    端口号

    计算机每个进程都有一个或多个端口

    TCP Socket模型

    代码实现:

    半双工聊天:

    talkserver

    package com.ufo.nine; import java.io.*; import java.net.*; import java.applet.Applet; public class talkserver { public static void main(String args[]) { try { ServerSocket server = null; try { server = new ServerSocket(4700); }catch(Exception e) { System.out.println("can not listen to:" + e); } Socket socket = null; try { socket = server.accept(); }catch(Exception e) { System.out.println("Error:" + e); } String line; BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Client:" + is.readLine()); line = sin.readLine(); while (!line.equals("bye")) { os.println(line); os.flush(); System.out.println("Server:" + line); System.out.println("Client:" + is.readLine()); line = sin.readLine(); } is.close(); os.close(); socket.close(); server.close(); }catch(Exception e) { System.out.println("Error" + e); } } }

    talkclient

    package com.ufo.nine; import java.io.*; import java.net.*; public class talkclient { public static void main(String args[]) { try { Socket socket = new Socket("127.0.0.1",4700); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader( socket.getInputStream())); String readline; readline = sin.readLine(); while (!readline.equals("bye")) { os.println(readline); os.flush(); System.out.println("Client:" + readline); System.out.println("Server:" + is.readLine()); readline = sin.readLine(); } os.close(); is.close(); socket.close(); }catch(Exception e) { System.out.println("Error" + e); } } }

    全双工聊天:参考https://blog.csdn.net/jiahao1186/article/details/82715859

    亲测可用!

    单工、半双工、全双工定义

    单工: 在单一的通路上端与端进行信息交流,若规定A为数据发送方,则B为数据接收方; 数据的传输是单向的,只能沿着一个方向。

    半双工: 在单一的通路上端与端进行信息交流,规定在同一时间段只能一方发送一方接收, 例如:A方发送数据时,B方只能接收数据;或者B方发送数据时A方只能接收数据; 双方不能同时发送数据。 数据可以双向传递,但是同一个时段只能进行一个方向的传递。

    双工: 在A方发送数据时,B方可以同时发送数据,一般需要两条通道。 通信的双方可以同时发送和接收数据。

    UDP socket

    关键:用DatagramPacket这个类打包数据

    用DatagramSocket传输数据

    TestUDPClient

    package com.ufo.nine.testudp; import java.net.*; import java.io.*; public class TestUDPClient { public static void main(String args[]) throws Exception { long n = 10000L; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeLong(n); //把需要发送的数据转成基本数据流 byte[] buf = baos.toByteArray(); //字节数组流转为字节数组 System.out.println(buf.length); //打印一下长度 DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678) );//通过DatagramPacket,把需要发送的数据,InetSocketAddress目标地址和端口号,装进去) DatagramSocket ds = new DatagramSocket(9999); //DatagramSocket设定自己的端口号 ds.send(dp); ds.close(); } }

    TestUDPServer

    package com.ufo.nine.testudp; import java.net.*; import java.io.*; public class TestUDPServer { public static void main(String args[]) throws Exception { byte buf[] = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); //通过DatagramPacket初始化接受对象 DatagramSocket ds = new DatagramSocket(5678); //设定自己的端口号 while(true) { ds.receive(dp); //一直接受 ByteArrayInputStream bais = new ByteArrayInputStream(buf);//各种转换 DataInputStream dis = new DataInputStream(bais);//各种转换 System.out.println(dis.readLong());//打印内容 } } }

    没多大用处。

    全双工udp聊天:https://blog.csdn.net/qq_35341203/article/details/90115898

    亲测可用!

    GUI

    Awt在不同机器上展示效果不同

    Swing相同

    核心:

    container比较特殊,container里面还可以放component,也可也放container。

    Frame就是窗口,可直接展示出来。

    Penle需要加入在Frame重才能展示出来。

    布局管理器:

    Awt提供了5种布局管理器类: FlowLayagt,BorderLayout,GridLayou,CardLayout,GridBagLayout


    FlowLayagt

    package com.ufo.nine.gui; import java.awt.*; public class test2 { public static void main(String[] args) { Frame f=new Frame("此处是标题"); Button b1=new Button("这个是button的lable"); Button b2=new Button("这个是button的lable"); Button b3=new Button("这个是button的lable"); f.setLayout(new FlowLayout()); f.add(b1); f.add(b2); f.add(b3); f.setSize(100,100); f.setVisible(true); } }

    BorderLayout

    GridLayout

    GridLayout型布局管理器将空间划分成规则的矩形网格,每个单元格区 域大小相等。组件被添加到每个单元格中,先从左到右添满一行后换 行,再从上到下。 在GridLayout构造方法中指定分割的行数和列数:GridLayout(4,4)

    Panel默认布局FloatLayout

    Frame默认布局BordertLayout

    事件模型

    重点:实现Actionlistener接口,重写actionPerformed方法.

    把这个类的对象放到button.addActionListener这个函数里面。

    在重写的actionPerformed方法里面,会自动传入当前对象的相关信息。

    ActionEvent e就是自动传入的,可以通过这个e来区分不同的元件。

    例子1:基础点击事件,点击一下,打印台打印一下

    package com.ufo.nine.gui; import java.awt.*; import java.awt.event.*; public class TestActionEvent { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me!"); Monitor bh = new Monitor(); b.addActionListener(bh); f.add(b,BorderLayout.CENTER); f.pack(); f.setVisible(true); } } class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed"); } }

    例子2:弄一个输入框,输入什么东西,回车,打印什么东西出来

    package com.ufo.nine.gui; import java.awt.*; import java.awt.event.*; public class TFActionEvent { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TFFrame(); } } class TFFrame extends Frame { TFFrame() { TextField tf = new TextField(); add(tf); tf.addActionListener(new TFActionListener()); pack(); setVisible(true); } } class TFActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf = (TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }

    TextFiled可以通过setEchoChar("*")设置回显字符,把密码变成✳展示出来。

    问题:如果点击其中一个按钮,需要取获取别的元件的相关信息,怎么办?

    使用内部类。内部类可以访问包装他的那个类的成员。

    内部类编译后产生的class文件由包装类名$内部类名构成。

    例子3:模拟计算机的加法:使用内部类。

    package com.ufo.nine.gui2; import java.awt.*; import java.awt.event.*; public class TFMath { public static void main(String[] args) { new TFFrame().launchFrame(); } } class TFFrame extends Frame { TextField num1, num2, num3; public void launchFrame() { num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); btnEqual.addActionListener(new MyMonitor()); setLayout(new FlowLayout()); add(num1); add(lblPlus); add(num2); add(btnEqual); add(num3); pack(); setVisible(true); } private class MyMonitor implements ActionListener { public void actionPerformed(ActionEvent e) { int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText("" + (n1 + n2)); } } }

    Graphics画图

    关键:定义一直只Graphics画笔对象,然后调用它的函数。具体用法参考api文档。

    pack(); setVisible(true); } private class MyMonitor implements ActionListener { public void actionPerformed(ActionEvent e) { int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText("" + (n1 + n2)); } }

    }

    Graphics画图

    关键:定义一直只Graphics画笔对象,然后调用它的函数。具体用法参考api文档。

    Processed: 0.028, SQL: 8