public class Scan implements Runnable{ private String path; private JTextArea res; private int setting=0; File af; public Scan(String Path, JTextArea Res, int Set) { path = Path; res = Res; setting = Set; res.setText(""); } @Override public void run() { ScanDisk(); }
时间: 2023-12-30 22:05:15 浏览: 77
这是一个实现了 Runnable 接口的 Scan 类,它的主要功能是扫描指定路径下的文件,并将扫描结果显示在 JTextArea 组件上。
构造方法 Scan(String Path, JTextArea Res, int Set) 接收三个参数:扫描路径 path、显示结果的 JTextArea 组件 res 和设置参数 setting。在构造方法中,首先将 JTextArea 组件的文本内容清空。
run() 方法是 Runnable 接口的方法,用于启动线程。在 run() 方法中,调用了 ScanDisk() 方法来执行扫描操作。
由于代码片段不完整,无法确定 ScanDisk() 方法的具体实现,但可以推断出它是用于扫描指定路径下的文件的方法。
相关问题
package Socker; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server extends JFrame { private JTextArea chatArea; private JTextField inputField; private final int port = 8000; private Socket clientSocket; private BufferedReader reader; private PrintWriter writer; public Server() { setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("这里是服务器!"); setBounds(750, 100, 800, 600); setAlwaysOnTop(true); JPanel chatPanel = new JPanel(new BorderLayout()); chatArea = new JTextArea(); chatArea.setEditable(false); JScrollPane jScrollPane = new JScrollPane(chatArea); chatPanel.add(jScrollPane, BorderLayout.CENTER); JPanel inputPanel = new JPanel(new FlowLayout()); inputField = new JTextField(50); inputField.setPreferredSize(new Dimension(100, 30)); JButton sendButton = new JButton("发送"); sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sendMessage(inputField.getText()); } }); inputPanel.add(inputField); inputPanel.add(sendButton); Container container = getContentPane(); container.add(chatPanel, BorderLayout.CENTER); container.add(inputPanel, BorderLayout.SOUTH); setVisible(true); startServer(); } private void sendMessage(String text) { if (!text.isEmpty()) { String message = "【服务器】: " + text + "\n"; chatArea.append(message); inputField.setText(""); sendToClient(message); } } private void sendToClient(String message) { writer.println(message); } private void startServer() { try { ServerSocket serverSocket = new ServerSocket(port); System.out.println("服务器已启动,等待客户端连接"); clientSocket = serverSocket.accept(); System.out.println("客户端连接成功:" + clientSocket); reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); writer = new PrintWriter(clientSocket.getOutputStream(), true); new Thread(new ClientListener()).start(); } catch (IOException e) { e.printStackTrace(); } } private class ClientListener implements Runnable { @Override public void run() { try { String message; while ((message = reader.readLine()) != null) { chatArea.append(message + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Server().startServer(); } }); } }这个代码为何无界面显示
这个代码可能无界面显示的原因是因为在主函数中创建了一个新的线程来启动服务器,但是在这个新线程中没有再次调用`setVisible(true)`方法来显示窗口。
可以将主函数的代码修改为如下所示,即在SwingUtilities.invokeLater中再次调用setVisible(true)方法,来确保界面显示正常:
```java
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Server server = new Server();
server.setVisible(true);
server.startServer();
}
});
}
```
这样,就可以保证在启动服务器的同时,也会正确显示界面。
将下列数据包通讯程序代码完善,使得可以实现两个客户端之间互相传递信息package ChattingApp; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; @SuppressWarnings("serial") public class ChattingApp extends JFrame implements ActionListener { private JTextArea chatHistory; private JTextField chatInput; private JButton sendBtn, clearBtn, exitBtn; // 发送,清空,退出按钮 private DatagramSocket socket; private InetAddress address; private int port; public ChattingApp() { setTitle("Chat Application"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chatHistory = new JTextArea(20, 40); chatHistory.setEditable(false); JScrollPane scrollPane = new JScrollPane(chatHistory); chatInput = new JTextField(30); sendBtn = new JButton("发送"); sendBtn.addActionListener(this); clearBtn = new JButton("清空"); clearBtn.addActionListener(this); exitBtn = new JButton("退出"); exitBtn.addActionListener(this); JPanel panel = new JPanel(); panel.add(chatInput); panel.add(sendBtn); panel.add(clearBtn); panel.add(exitBtn); add(scrollPane, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); setVisible(true); try { socket = new DatagramSocket(); address = InetAddress.getByName("localhost"); port = 9999; } catch (Exception e) { e.printStackTrace(); } new Thread(new ReceiveThread()).start(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == sendBtn) { String message = chatInput.getText(); if (!message.isEmpty()) { byte[] data = message.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address, port); try { socket.send(packet); } catch (Exception ex) { ex.printStackTrace(); } chatInput.setText(""); } } if (e.getSource() == clearBtn) { chatInput.setText(""); } if (e.getSource() == exitBtn) { System.exit(0); } } private class ReceiveThread implements Runnable { public void run() { try { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); while (true) { socket.receive(packet); String message = new String(packet.getData(), 0, packet.getLength()); chatHistory.append(message + "\n"); } } catch (Exception e) { e.printStackTrace(); } } } }
在该代码基础上,需要新建一个类Client并实现客户端之间的通讯。具体代码实现如下:
package ChattingApp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
@SuppressWarnings("serial")
public class ChattingApp extends JFrame implements ActionListener {
private JTextArea chatHistory;
private JTextField chatInput;
private JButton sendBtn, clearBtn, exitBtn;// 发送,清空,退出按钮
private DatagramSocket socket;
private InetAddress address;
private int port;
public ChattingApp() {
setTitle("Chat Application");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatHistory = new JTextArea(20, 40);
chatHistory.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatHistory);
chatInput = new JTextField(30);
sendBtn = new JButton("发送");
sendBtn.addActionListener(this);
clearBtn = new JButton("清空");
clearBtn.addActionListener(this);
exitBtn = new JButton("退出");
exitBtn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(chatInput);
panel.add(sendBtn);
panel.add(clearBtn);
panel.add(exitBtn);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
socket = new DatagramSocket();
address = InetAddress.getByName("localhost");
port = 9999;
} catch (Exception e) {
e.printStackTrace();
}
new Thread(new ReceiveThread()).start();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendBtn) {
String message = chatInput.getText();
if (!message.isEmpty()) {
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
try {
socket.send(packet);
} catch (Exception ex) {
ex.printStackTrace();
}
chatInput.setText("");
}
}
if (e.getSource() == clearBtn) {
chatInput.setText("");
}
if (e.getSource() == exitBtn) {
System.exit(0);
}
}
private class ReceiveThread implements Runnable {
public void run() {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
chatHistory.append(message + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ChattingApp serverApp = new ChattingApp();
Client client1 = new Client("Client 1", "localhost", 9999);
Client client2 = new Client("Client 2", "localhost", 9999);
}
}
class Client extends JFrame implements ActionListener {
private JTextArea chatHistory;
private JTextField chatInput;
private JButton sendBtn, clearBtn, exitBtn;
private DatagramSocket socket;
private InetAddress address;
private int port;
private String name;
public Client(String name, String address, int port) {
this.name = name;
setTitle(name);
setSize(500, 500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
chatHistory = new JTextArea(20, 40);
chatHistory.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatHistory);
chatInput = new JTextField(30);
sendBtn = new JButton("发送");
sendBtn.addActionListener(this);
clearBtn = new JButton("清空");
clearBtn.addActionListener(this);
exitBtn = new JButton("退出");
exitBtn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(chatInput);
panel.add(sendBtn);
panel.add(clearBtn);
panel.add(exitBtn);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
socket = new DatagramSocket();
this.address = InetAddress.getByName(address);
this.port = port;
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendBtn) {
String message = chatInput.getText();
if (!message.isEmpty()) {
message = "[" + name + "]: " + message;
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
try {
socket.send(packet);
} catch (Exception ex) {
ex.printStackTrace();
}
chatInput.setText("");
chatHistory.append(message + "\n");
}
}
if (e.getSource() == clearBtn) {
chatInput.setText("");
}
if (e.getSource() == exitBtn) {
dispose();
}
}
}
阅读全文