【问题标题】:Java on repl.it java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires itrepl.it java.awt.HeadlessException 上的 Java:未设置 X11 DISPLAY 变量,但该程序执行了需要它的操作
【发布时间】:2020-04-27 05:39:10
【问题描述】:

在我解决这个问题之前,因为我没有使用 Linux 机器或类似的东西,而且我使用的是 repl.it,所以 export DISPLAY=:0.0setenv DISPLAY :0.0 的通用解决方案将不起作用。这只是 repl.it 上的一个 java 文件。

所以基本上,我有一个程序可以将自定义的用户定义信息写入文本文件,然后读取它。阅读算法和一切正常。这是我的窗口代码:

这是我的 Window 类,其中包含显示代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.File;
import java.util.Scanner;

public class Window extends JFrame {

  //Typing Area:
  private JTextField txtEnter = new JTextField();

  //Chat Area:
  private static JTextArea txtChat = new JTextArea();

  public Window(String name) {
    //Frame Attributes:
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 600);
    this.setVisible(true);
    this.setResizable(false);
    this.setLayout(null);
    this.setTitle("name");

    //txtChat Attributes:
    txtChat.setLocation(15, 5);
    txtChat.setSize(560, 510);

    this.add(txtChat);

    try {
      File f = new File(name = ".txt");
      Scanner scan = new Scanner(f);
      txtChat.append("File name: " + f.getName() + "\n");
      txtChat.append("File Size in Bytes: " + f.length() + " bytes\n");
      txtChat.append("\nFile Contents:\n\n");

      while(scan.hasNext()) {
        txtChat.append(scan.nextLine() + "\n");
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

}

这是我使用方法的方式:

new Window(file.getName());

我可以设置一个变量或其他东西来解决这个问题吗?

【问题讨论】:

  • 我无法重现您的问题,我刚刚测试并 repl.it 按预期显示窗口。
  • 这很奇怪。它会根据您使用的计算机而改变吗?
  • 他们似乎有一种专门用于 java + swing 的语言。 repl.it/languages/java_swing 如果你愿意的话,也许你应该分享你的 repl.it。

标签: java repl.it


【解决方案1】:

小错误:

  • 最后忘记了:scan.close();
  • txtChat 不应该是静态的,而是 Window 的一个字段。

可能在某个地方:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> new Window("x.txt").setVisible(true));
}

错误?也许 repl 是无头的(=仅控制台)?

也许您在 Docker 容器或类似容器中运行。这是我的第一个猜测。

【讨论】:

    【解决方案2】:

    使用System.in 作为输入,使用System.out 作为输出,如下例所示:

    您不能在 headless java repl.it 中使用 swing GUI 类:JTextFieldJTextArea

    正如马特所说,您可以使用特殊的https://repl.it/languages/java_swing

    这至少会编译但当然会错过文件:

    import java.io.File;
    import java.util.Scanner;
    
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Main extends JFrame {
    
        // Typing Area:
        private JTextField txtEnter = new JTextField();
    
        // Chat Area:
        private JTextArea txtChat = new JTextArea();
    
        public Main(String name) {
            // Frame Attributes:
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(600, 600);
            this.setVisible(true);
            this.setResizable(false);
            this.setLayout(null);
            this.setTitle("name");
    
            // txtChat Attributes:
            txtChat.setLocation(15, 5);
            txtChat.setSize(560, 510);
    
            this.add(txtChat);
    
            try {
                File f = new File(name = ".txt");
                Scanner scan = new Scanner(f);
                txtChat.append("File name: " + f.getName() + "\n");
                txtChat.append("File Size in Bytes: " + f.length() + " bytes\n");
                txtChat.append("\nFile Contents:\n\n");
    
                while (scan.hasNext()) {
                    txtChat.append(scan.nextLine() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            // creating instance of JFrame
            Main f = new Main("test");
    
            f.setSize(400, 500);
            f.setLayout(null);
            // make the frame visible
            f.setVisible(true);
        }
    }
    

    【讨论】:

    • 我还可以使用哪些其他文本字段类?
    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多