Use System.in for your input and System.out for the output like in this example:
- https://repl.it/@KarimBonnaa/Java-Calculator-App
You can't use the swing GUI classes: JTextField or JTextArea in headless java repl.it.
As Matt mentioned you can use the special https://repl.it/languages/java_swing
This will at least compile but misses the file of course:
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);
}
}
Small errors:
- You forgot:
scan.close();at the end. txtChatshould not be static but a field of Window.
Somewhere there probably is:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Window("x.txt").setVisible(true));
}
The error? Maybe the repl is headless (=only console)?
Maybe you run in a Docker container or such. Which would be my first guess.
Due to the limitation recently imposed by repl.it, I had to find another alternative for a web interface developing Java Swing applications in my class. Many of my students only have Chromebooks. I made a tutorial to get a working Java Swing development environment for my students using GitHub CodeSpaces. Thought, I'll share this. I hope this helps! https://apps.mvhs.io/resources/codespaces/