JavaSWT 应用程序在 Eclipse 中工作,但在终端中不工作

标签 java eclipse swt

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class clientWindow {

    static Text chatWindow;

    public static void sendMessage(Socket socket, String message) throws IOException {
        PrintWriter pr = new PrintWriter(socket.getOutputStream());
        pr.println("Client: " + message);
        pr.flush();
    }

    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s = new Socket("10.0.1.8", 4500);
        Display display = new Display();
        Shell clientWindow = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        clientWindow.setLayout(layout);

        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        GridData data1 = new GridData(GridData.FILL_BOTH);

        chatWindow = new Text(clientWindow, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);
        chatWindow.setLayoutData(data1);
        Text messageBox = new Text(clientWindow, SWT.SINGLE);
        messageBox.setLayoutData(data);
        Button send = new Button(clientWindow, 0);
        send.setText("Send");
        send.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent event) {
                try {
                    sendMessage(s, messageBox.getText());
                    messageBox.setText("");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });

        clientWindow.open();

        while (!clientWindow.isDisposed()) {
            if (!display.readAndDispatch()) {
                          display.sleep();
                }

    }   
    }

}

这是我完成的一个小型消息应用程序。这里的一切在 Eclipse 中都运行良好。然而,当我尝试在终端中运行它时,我得到了这个。

Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4711)
    at org.eclipse.swt.SWT.error(SWT.java:4626)
    at org.eclipse.swt.SWT.error(SWT.java:4597)
    at org.eclipse.swt.widgets.Display.error(Display.java:1112)
    at org.eclipse.swt.widgets.Display.createDisplay(Display.java:853)
    at org.eclipse.swt.widgets.Display.create(Display.java:837)
    at org.eclipse.swt.graphics.Device.<init>(Device.java:132)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:736)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:727)
    at clientWindow.main(clientWindow.java:28)

我很确定当尝试从“main”之外的内容访问显示时会发生此错误,这不是我想要做的。那么为什么它会给我这个错误?

最佳答案

根据 Display 代码中的行号判断,您正在 macOS 上运行此代码。

在 macOS 上,当您在终端中使用 java 命令运行代码时,必须指定 -XstartOnFirstThread 选项。

该程序可以在 Eclipse 中运行,因为 Eclipse 在运行配置中自动为您进行了设置。

关于JavaSWT 应用程序在 Eclipse 中工作,但在终端中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59829378/

相关文章:

java - NullPointerException @一对一映射

类似于c++ map的java库

java - 写入行时 eclipse(ADT) 崩溃包括 "R."

java - SWt : what is the color of a Control's border?

java - docx4j模板生成:can not take the placeholder containing string as a separate entity

Java OutputStream 仅在关闭时刷新数据

eclipse - 在 Eclipse 启动时禁用插件

java - Eclipse Java : “The import com.jidesoft cannot be resolved”

java - 从 SWT 浏览器销毁 Cookie

java - 检查 SWT/Jface 中数据绑定(bind)的值