【发布时间】:2020-04-27 05:39:10
【问题描述】:
在我解决这个问题之前,因为我没有使用 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();
}
}
}
这是我使用方法的方式:
new Window(file.getName());
我可以设置一个变量或其他东西来解决这个问题吗?
【问题讨论】:
-
我无法重现您的问题,我刚刚测试并 repl.it 按预期显示窗口。
-
这很奇怪。它会根据您使用的计算机而改变吗?
-
他们似乎有一种专门用于 java + swing 的语言。 repl.it/languages/java_swing 如果你愿意的话,也许你应该分享你的 repl.it。