swing程序设计 1.编写一个程序实现鼠标事件、窗口事件。2.socket编程,要求:创建服务端,往客户端发送信息,创建客户端
时间: 2024-01-21 12:03:56 浏览: 75
基于socket编程的客户端和服务器端程序
好的,我可以回答你的问题。
1. 编写一个程序实现鼠标事件、窗口事件:
以下是一个简单的Swing程序,可以实现鼠标事件和窗口事件。当用户单击窗口时,会弹出一个对话框显示“Hello World!”;当用户单击按钮时,会在控制台输出“Button clicked!”。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends JFrame implements MouseListener {
private JButton button;
public MyWindow() {
super("My Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加按钮
button = new JButton("Click me");
button.addMouseListener(this);
getContentPane().add(button, BorderLayout.CENTER);
// 添加窗口事件
addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
System.out.println("Window activated");
}
public void windowClosed(WindowEvent e) {
System.out.println("Window closed");
}
public void windowClosing(WindowEvent e) {
System.out.println("Window closing");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("Window deactivated");
}
public void windowDeiconified(WindowEvent e) {
System.out.println("Window deiconified");
}
public void windowIconified(WindowEvent e) {
System.out.println("Window iconified");
}
public void windowOpened(WindowEvent e) {
System.out.println("Window opened");
}
});
pack();
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == button) {
System.out.println("Button clicked!");
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new MyWindow();
}
}
```
2. Socket编程,要求:创建服务端,往客户端发送信息,创建客户端:
以下是一个简单的Socket编程程序,可以创建服务端和客户端,并实现信息发送和接收。服务端启动后,等待客户端连接;客户端连接后,可以向服务端发送消息,服务端接收到消息后会在控制台输出,并回复客户端“Message received”。
服务端程序:
```java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Server started");
Socket socket = serverSocket.accept();
System.out.println("Client connected");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message: " + inputLine);
out.println("Message received");
}
in.close();
out.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
客户端程序:
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
System.out.println("Connected to server");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while ((inputLine = userIn.readLine()) != null) {
out.println(inputLine);
System.out.println(in.readLine());
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
以上是我的回答,希望能对你有所帮助。
阅读全文