}
}
public static void main(String[] args) { }
new Information();
rb2.addActionListener(listener2);
// 将JPanel面板和JScrollPane面板添加到JFrame容器中 Container container = this.getContentPane(); container.add(panel, BorderLayout.NORTH); container.add(pane, BorderLayout.CENTER); this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);
3、参考答案
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class MyMenu extends JFrame implements ActionListener {
JLabel label = new JLabel(\请选择菜单\JMenuItem aaMenuItem, baMenuItem; MyMenu() { }
public void actionPerformed(ActionEvent e) { }
JMenuItem source = (JMenuItem) (e.getSource()); label.setText(\选择了菜单:\label.setHorizontalAlignment(JLabel.CENTER); JMenuBar menuBar = new JMenuBar(); JMenu aMenu = new JMenu(\菜单A\JMenu bMenu = new JMenu(\菜单B\
JMenuItem aaMenuItem = new JMenuItem(\菜单项 AA\JMenuItem abMenuItem = new JMenuItem(\菜单项AB\JMenuItem baMenuItem = new JMenuItem(\菜单项 BA\menuBar.add(aMenu); menuBar.add(bMenu); aMenu.add(aaMenuItem); aMenu.addSeparator(); aMenu.add(abMenuItem); bMenu.add(baMenuItem);
aaMenuItem.addActionListener(this); abMenuItem.addActionListener(this); baMenuItem.addActionListener(this); setJMenuBar(menuBar);
getContentPane().add(label, BorderLayout.CENTER);
}
public static void main(String args[]) { }
JFrame frame = new MyMenu();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true);
第10章 网络编程
一、填空题
1、面向连接、客户端、服务器端 2、2、0-65535
3、链路层、网络层、运输层、应用层 4、InetAddress
5、DatagramPacket、DatagramSocket 二、判断题
1、错 2、对 3、对 4、错 5、对 三、选择题
1、C 2.A 3、ABD 4、B 5.、A 6、D 7、B 8、C 四、简答题
1、在Internet中传输数据都需要遵守一定的规则,这种规则通常被称作网络通信协议。网络通信协议对数据传输格式、传输速率、传输步骤等作了统一规定,通信双方必须共同遵守这个规定才能完成数据的交互。到目前为止,网络通信协议已经有很多种,其中TCP/IP协议在世界范围内应用最为广泛。
2、UDP协议是无连接通信协议,所谓的无连接就是指数据的发送端和接收端不建立逻辑连接。由于UDP协议消耗资源小,通信效率高,通常都会用于音频、视频和普通数据的传输。UDP协议在传输数据时不能保证数据的完整性,因此在传输重要数据时不建议使用UDP协议。
TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输。在TCP连接中必须要明确客户端与服务器端,由客户端向服务端发出连接请求,每次连接的创建都需要经过“三次握手”。
3、ServerSocket类用于创建服务端程序,通过调用ServerSocket对象的accept()方法,接收来自客户端的请求。
Socket类用于创建客户端程序,当客户端和服务端的两个Socket建立了专线连接后,连接的一端既能向另一端连续写入字节,也能从另一端读取字节。Socket类中定义了getInputStream()方法返回Socket的输入流对象,定义了getOutputStream()方法返回Socket的输出流对象。只要连接的一端向该输出流对象写入了数据,连接的另一端就能从其输入流对象中读取到。
五、编程题 1、参考答案
import java.net.InetAddress; public class Test01 {
public static void main(String[] args) throws Exception {
InetAddress localAddress = InetAddress.getLocalHost();
InetAddress remoteAddress = InetAddress.getByName(\
}
}
System.out.println(\本机的IP地址:\System.out.println(\甲骨文的IP地址:\
System.out.println(\本地的主机名:\
2、 参考答案
接收端:
import java.net.*; public class Test02 { }
public static void main(String[] args) throws Exception { }
byte[] buf = new byte[1024];
DatagramSocket ds = new DatagramSocket(8001); DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp);
String str = new String(dp.getData(),0, dp.getLength()); System.out.println(str); ds.close();
发送端
import java.net.*; public class Test03 { }
public static void main(String[] args) throws Exception { }
DatagramSocket ds = new DatagramSocket(3000); String str = \
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
InetAddress.getByName(\
ds.send(dp); ds.close();
3、参考答案 服务端
import java.io.*; import java.net.*; public class Test04 { }
class TCPServer {
public void listen() throws Exception {
ServerSocket serverSocket = new ServerSocket(8002); public static void main(String[] args) throws Exception { }
new TCPServer().listen();
}
}
Socket client = serverSocket.accept(); OutputStream os = client.getOutputStream(); os.write(\Thread.sleep(5000); os.close(); client.close();
客户端
import java.io.*; import java.net.*; public class Test05 { }
class TCPClient { }
public void connect() throws Exception { }
Socket client = new Socket(InetAddress.getLocalHost(), 8002); InputStream is = client.getInputStream(); byte[] buf = new byte[1024]; int len = is.read(buf);
System.out.println(new String(buf, 0, len)); client.close();
public static void main(String[] args) throws Exception { }
new TCPClient().connect();