java 窗体 removeall
时间: 2023-08-09 15:11:34 浏览: 134
`removeAll()` 是 Java AWT 和 Swing 中的一个方法,用于从容器中删除所有组件。如果你想在窗体中删除所有的组件,可以使用以下代码:
```java
myFrame.getContentPane().removeAll();
myFrame.revalidate();
myFrame.repaint();
```
其中,`myFrame` 是你的窗体对象。在调用 `removeAll()` 方法后,需要调用 `revalidate()` 方法来重新布局,然后再调用 `repaint()` 方法来重绘窗体。
相关问题
java怎么清空窗体
在Java中,清空窗体可以通过以下两种方式实现:
1. 使用容器的removeAll()方法
可以通过容器对象(如JFrame、JPanel等)的removeAll()方法来移除容器中的所有组件,从而清空窗体。
示例代码:
```java
JFrame frame = new JFrame("Clear Frame");
frame.setSize(400, 300);
JPanel panel = new JPanel();
frame.add(panel);
// 添加组件
// 清空窗体
panel.removeAll();
frame.setVisible(true);
```
2. 使用组件的setVisible()方法
可以通过设置组件的setVisible()方法为false来隐藏组件,从而清空窗体。
示例代码:
```java
JFrame frame = new JFrame("Clear Frame");
frame.setSize(400, 300);
JPanel panel = new JPanel();
frame.add(panel);
// 添加组件
// 清空窗体
Component[] components = panel.getComponents();
for (Component component : components) {
component.setVisible(false);
}
frame.setVisible(true);
```
以上两种方式都可以实现清空窗体的效果,具体使用哪种方式取决于实际需求。
写一个客户端和服务端聊天带窗体的JAVA程序
好的,以下是一个简单的基于 Java Socket 编写的客户端和服务端聊天程序。客户端和服务端都有一个 GUI 界面,可以通过界面输入和接收消息。
客户端界面:
![客户端界面](https://i.imgur.com/2VBKkLj.png)
服务端界面:
![服务端界面](https://i.imgur.com/ArQ8D6C.png)
客户端代码:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatClient {
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("Chat Client");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 60);
public ChatClient() {
// Layout GUI
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, BorderLayout.SOUTH);
frame.getContentPane().add(new JScrollPane(messageArea), BorderLayout.CENTER);
frame.pack();
// Add Listeners
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}
private String getServerAddress() {
return "localhost"; // Change this to your server's IP address
}
private int getServerPort() {
return 9001; // Change this to your server's port number
}
public void run() throws IOException {
// Make connection and initialize streams
Socket socket = new Socket(getServerAddress(), getServerPort());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName());
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "\n");
}
}
}
private String getName() {
return "Client"; // Change this to your desired name
}
public static void main(String[] args) throws Exception {
ChatClient client = new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}
```
服务端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
public class ChatServer {
private static final int PORT = 9001;
private static Set<String> names = new HashSet<String>();
private static Set<PrintWriter> writers = new HashSet<PrintWriter>();
public static void main(String[] args) throws Exception {
System.out.println("The chat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Handler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Request a name from this client. Keep requesting until
// a name is submitted that is not already used. Note that
// checking for the existence of a name and adding the name
// must be done while locking the set of names.
while (true) {
out.println("SUBMITNAME");
name = in.readLine();
if (name == null) {
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}
// Now that a successful name has been chosen, add the
// socket's print writer to the set of all writers so
// this client can receive broadcast messages.
out.println("NAMEACCEPTED");
writers.add(out);
// Accept messages from this client and broadcast them.
// Ignore other clients that cannot be broadcasted to.
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
```
希望这个程序能够帮到您!
阅读全文