如何将.txt文件添加到JTextArea以及放置.txt文件的位置? [英] how to add .txt file to a JTextArea and where to place my .txt file?
问题描述
我正在处理我的第一个GUI程序,几乎完成了最后一个类是一个jFrame,它有一个.txt文件和一个关闭窗口的按钮,我不知道如何将我的文件追加到窗口? ?
package eg.edu.guc.santorini.gui;
i'm working on my first GUI program and almost finished the last class is a jFrame that has a .txt file and a button to close the window and i don't know how to append my file into the window ??? package eg.edu.guc.santorini.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Rules extends JFrame implements ActionListener, MouseListener{
JPanel Rules;
JTextArea rules;
public Rules() throws IOException
{
super();
setTitle("Rules Of Santorini Board Game");
setSize(1000, 700);
setLocation(200, 100);
Container content = getContentPane();
content.setBackground(new Color(220,20,60));
content.setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
WindowDestroyer wd = new WindowDestroyer();
addWindowListener(wd);
JTextArea rules=new JTextArea();
rules.append("");
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
StringBuilder builder = new StringBuilder();
// read a text file from resources folder that is parallel to src folder
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/New Text Document.txt")));
String line = null;
while ((line = reader.readLine()) != null) {
// read the file line by line
builder.append(line).append(System.lineSeparator());
}
reader.close();
// set the content of file in text area
textArea.setText(builder.toString());
/* FileReader fileReader = new FileReader("New Text Document.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputFile = "";
String textFieldReadable = bufferedReader.readLine();
while (textFieldReadable != null){
inputFile += textFieldReadable;
textFieldReadable = bufferedReader.readLine();
rules.setText(inputFile);*/
Rules=new JPanel();
Rules.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Rules.setVisible(true);
Rules.setBackground(Color.ORANGE);
add(Rules, BorderLayout.CENTER);
Rules.setSize(1000, 700);
this.getContentPane().add(Rules);
JButton ok=new JButton("Got It");
ok.setSize(100, 50);
ok.setLocation(800, 570);
ok.addMouseListener(this);
Rules.add(ok);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//dispose();
setVisible(false);
}
});
//JFrame f = new JFrame();
//f.setSize(320, 200);
//f.getContentPane().add(rules);
//f.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
推荐答案
将文件读入 JTextArea
你可以简单地使用 JTextArea #read
,但是,这将丢弃 JTextArea的当前内容
To read a file into a JTextArea
you can simply use JTextArea#read
, this will, however, discard the current contents of the JTextArea
更新
将代码添加到IDE后,我注意到您没有添加规则
( JTextArea
)任何东西,所以它永远不可见......
After adding the code to an IDE, I've noted that you are not adding rules
(the JTextArea
) to anything so it will never be visible...
你如何创建你的一般结构UI也有点偏斜,尝试更像... ...
The general structure of how you create your UI is also a little skewed, try something more like...
public class Rules extends JFrame {
public Rules() throws IOException {
super();
// Initial setu[
setTitle("Rules Of Santorini Board Game");
// Create the basic UI content
JTextArea textArea = new JTextArea(40, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
// Read the file
try (BufferedReader reader = new BufferedReader(new FileReader(new File("resources/New Text Document.txt")))) {
textArea.read(reader, "File");
} catch (IOException exp) {
exp.printStackTrace();
}
getContentPane().setBackground(Color.ORANGE);
JButton ok = new JButton("Got It");
add(textArea, BorderLayout.SOUTH);
add(ok, BorderLayout.SOUTH);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispose();
//?? No idea what this is for, but it won't do much
setVisible(false);
}
});
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
不要使用带按钮的 MouseListener
s,而不是你应该使用 ActionListener
Don't use MouseListener
s with buttons, instead you should be using an ActionListener
不要使用 null
布局。像素完美布局是现代UI设计中的一种幻觉,您无法控制字体,DPI,渲染管道或其他因素,这些因素将改变组件在屏幕上呈现的方式。
Don't use null
layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen.
Swing旨在与布局管理器一起解决这些问题。如果你坚持忽略这些功能并违背API设计,那就要做好准备应对很多令人头疼的事情,永远不要过时努力......
Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work...
这篇关于如何将.txt文件添加到JTextArea以及放置.txt文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!