gpt4 book ai didi

repl.it 上的 Java java.awt.HeadlessException : No X11 DISPLAY variable was set, 但该程序执行了需要它的操作

转载 作者:行者123 更新时间:2023-12-02 09:07:51 29 4
gpt4 key购买 nike

在解决这个问题之前,因为我没有使用 Linux 机器或类似的东西,并且我使用的是 repl.it,export DISPLAY=:0.0 的通用解决方案setenv 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();
}
}

}

这就是我使用该方法的方式:

新窗口(file.getName());

是否可以设置变量或其他内容来解决此问题?

最佳答案

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

您不能使用swing GUI类:headless java repl.it 中的 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);
}
}

关于repl.it 上的 Java java.awt.HeadlessException : No X11 DISPLAY variable was set, 但该程序执行了需要它的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59666969/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com