[英]Opening Java file in a JTextArea
I'm fairly new to Java so I was wondering if someone could help me figure this out. 我对Java很新,所以我想知道是否有人可以帮我解决这个问题。
I've managed to create a program which allows the user to input a file name which then shows the contents of the file in the system output but I want to be able to show the output in a JTextArea instead of the current place of output. 我设法创建了一个程序,允许用户输入一个文件名,然后在系统输出中显示文件的内容,但我希望能够在JTextArea中显示输出而不是当前的输出位置。 Can some help me figure out how this could be possible?
有人可以帮我弄清楚这是怎么可能的吗?
As one of the comments suggested that I should add a JFrame before doing anything else and I have managed to create a JFrame with a JTextArea within it but I am still unable to display the text file in the TextArea. 正如其中一条评论所说,我应该在做其他任何事情之前添加一个JFrame,并且我已经设法在其中创建一个带有JTextArea的JFrame但我仍然无法在TextArea中显示文本文件。
import java.awt.GraphicsConfiguration;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class OpenFile {
static GraphicsConfiguration gc;
public static void main(String[] args) {
String selectModule = "";
selectModule = JOptionPane.showInputDialog("Module Input: ");
Path path = Paths.get(selectModule);
String fileOpener = selectModule;
if(selectModule.equalsIgnoreCase(fileOpener)){
//Makes the user input case insensitive
}
BufferedReader br = null;
FileReader fr = null;
try {
//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(fileOpener + ".txt");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
JTextArea TextArea22 = new JTextArea(selectModule);
JFrame frame= new JFrame(gc);
frame.setTitle("Opening TextFile");
frame.setSize(600, 400);
frame.setLocation(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(TextArea22);
}
}
The code to read in the entire contents of a file can be simplified using Files.readAllBytes . 使用Files.readAllBytes可以简化读取文件全部内容的代码 。
Once that is done, set the JTextArea's text to the captured file content. 完成后,将JTextArea的文本设置为捕获的文件内容。 If an exception occurs while trying to read the file, we can output that as a helpful message to the JTextArea instead.
如果在尝试读取文件时发生异常,我们可以将其作为有用的消息输出到JTextArea。
Last, remember that any code that manipulates a Swing component must be executed on the event dispatch thread . 最后,请记住,任何操作Swing组件的代码都必须在事件派发线程上执行。 So, put that code in a Runnable and pass it to SwingUtilities.invokeLater .
因此,将该代码放在Runnable中并将其传递给SwingUtilities.invokeLater 。
Give this a try: 尝试一下:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class OpenFile {
public static void main(String[] args) {
String selectModule = JOptionPane.showInputDialog("Module Input: ");
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get(selectModule)));
} catch (IOException e) {
content = e.toString();
}
final String fileContent = content;
Runnable r = new Runnable() {
public void run() {
JTextArea textArea22 = new JTextArea(fileContent);
JFrame frame= new JFrame();
frame.setTitle("Opening TextFile");
frame.setSize(600, 400);
frame.setLocation(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(textArea22);
}
};
SwingUtilities.invokeLater(r);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.