public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { RegisterReaders registerReaders = new RegisterReaders(); registerReaders.setVisible(true); } }); } }
时间: 2024-02-10 19:19:46 浏览: 30
这段代码是整个程序的入口,也是 Java 应用程序的入口。在 main 方法中,首先使用 SwingUtilities.invokeLater 方法启动一个新线程,并在其中创建一个 RegisterReaders 对象,并将其显示出来。这样就会在程序启动时显示出读者注册界面。由于 GUI 通常需要在事件分发线程中运行,因此使用了 invokeLater 方法来确保程序运行在正确的线程上。
相关问题
Fifth, create a GUI class with a main method. In this main method, create an anonymous class that implements the Runnable interface with a run method and use the javax.swing.SwingUtilities.invokeLater method to run that code on the event dispatch thread.
Sure, here's an example of how to create a GUI class with a main method and use an anonymous class to implement the Runnable interface and run the code on the event dispatch thread:
```java
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MyGUI {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("My GUI");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add components to the frame
frame.setVisible(true);
}
});
}
}
```
In this example, we create a `JFrame` object and set its properties, such as size and title. We then add components to the frame (not shown in the code snippet). Finally, we make the frame visible.
Note that we use the `SwingUtilities.invokeLater()` method to run the code on the event dispatch thread, which is necessary for updating the user interface components safely.
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();
}
});
}
```
这样,就可以保证在启动服务器的同时,也会正确显示界面。