请为此设置默认文字。我正在尝试创建一个显示文件内容的文本区域。我打算在java库中使用文件打开器来选择文件。我将把文件内容存储在一个字符串中,然后将它打印在JtextArea中。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Parser implements ActionListener {
protected JTextArea textArea;
Parser() {
JFrame f = new JFrame("DECA Test Parser");
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);
f.setJMenuBar(jmb);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String comStr = ae.getActionCommand();
if (comStr == "Exit");
System.exit(0);
}
public static void main(String args[]) {
new Parser();
}
}
答案 0 :(得分:1)
您可以通过添加以下几行来添加textArea:
textArea = new JTextArea();
f.add(textArea);
然后,你可以像这样添加文字:
textArea.setText("your file content here");
答案 1 :(得分:1)
// Go through this code it reads a file named read.txt in c drive and prints its content to text area
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test implements ActionListener {
protected JTextArea textArea;
Test() throws IOException {
JFrame f = new JFrame("DECA Test Parser");
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar jmb = new JMenuBar();
JMenu jmFile = new JMenu("File");
JMenuItem jmiOpen = new JMenuItem("Open");
JMenuItem jmiSave = new JMenuItem("Save");
JMenuItem jmiExit = new JMenuItem("Exit");
jmFile.add(jmiOpen);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
JMenu jmHelp = new JMenu("Help");
JMenuItem jmiAbout = new JMenuItem("About");
jmHelp.add(jmiAbout);
jmb.add(jmHelp);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);
textArea = new JTextArea();
FileInputStream fstream = new FileInputStream("C:\\read.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
textArea.append(strLine);
}
f.getContentPane().add(textArea);
f.setJMenuBar(jmb);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String comStr = ae.getActionCommand();
if (comStr == "Exit");
System.exit(0);
}
public static void main(String args[]) {
try {
new Test();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 2 :(得分:1)
JTextArea扩展了JTextComponent,它有两个方法
public void read(Reader in, Object desc)
public void write(Writer out)
只需在那里传递FileReader / FileWriter。