在 GUI 中显示从文件中读取的任何内容 [英] Displaying whatever has been read from a file in a GUI
问题描述
所以,我的数据存储在一个名为 log.txt 的文件中,我想在 GUI 中查看它的内容.所以我有我正在使用的这两个类,一个是 Engine,我在其中读取 log.txt 文件,另一个是 GUI,用于应用 Engine 中使用的方法.
So, my data are stored in a file, called log.txt, and I want to view the content of it, in GUI. So i've got these two classes which i'm using, one is Engine, where I read the log.txt file, and the other one is GUI, where is used to apply the methods used in Engine.
所以,在我的引擎中,我有这些代码:
So, in my engine, I've got these codes:
public void loadLog()
{
try
{
java.io.File cpFile = new java.io.File( "log.txt" );
if ( cpFile.exists() == true )
{
File file = cpFile;
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String strLine="";
String logPrint="";
fis = new FileInputStream ( file );
// Here is BufferedInputStream added for fast reading
bis = new BufferedInputStream ( fis );
dis = new DataInputStream ( bis );
// New Buffer Reader
BufferedReader br = new BufferedReader( new InputStreamReader( fis ) );
while ( ( strLine = br.readLine() ) != null )
{
StringTokenizer st = new StringTokenizer ( strLine, ";" );
while ( st.hasMoreTokens() )
{
logPrint = st.nextToken();
System.out.println(logPrint);
}
log = new Log();
//regis.addLog( log );
}
}
}
catch ( Exception e ){
}
}
然后,在我的 GUI 中,我会尝试应用我的引擎中使用的代码:
and then, in my GUI, I'd try to apply the codes used in my engine:
// create exit menu
Logout = new JMenu("Exit");
// create JMenuItem for about menu
reportItem = new JMenuItem ( "Report" );
// add about menu to menuBar
menuBar.add ( Logout );
menuBar.setBorder ( new BevelBorder(BevelBorder.RAISED) );
Logout.add ( reportItem );
/* --------------------------------- ACTION LISTENER FOR ABOUT MENU ------------------------------------------ */
reportItem.addActionListener ( new ActionListener()
{
public void actionPerformed ( ActionEvent e )
{
engine.loadLog();
mainPanel.setVisible (false);
mainPanel = home();
toolBar.setVisible(false);
vasToolBar.setVisible(false);
cpToolBar.setVisible(false);
add ( mainPanel, BorderLayout.CENTER );
add ( toolBar, BorderLayout.NORTH );
toolBar.setVisible(false);
mainPanel.setVisible ( true );
pack();
setSize(500, 500);
}
});
现在,
我的问题是,如何在 GUI 部分内打印出在我的引擎方法中读取的任何内容?我希望它们位于 JLabel 或 JTextArea 内.我该怎么做?
my question is, how can I print out whatever is read in my Engine's method, inside the GUI part? I want them to be either inside a JLabel or JTextArea. How am I supposed to do that?
推荐答案
文件 IO 操作被视为阻塞/耗时.
File IO operations are considered blocking/time consuming.
您应该避免在事件调度线程中运行它们,因为这会阻止 UI 开始更新,使您的应用程序看起来像是挂起/崩溃
You should avoid running them in the Event Dispatching Thread, as this will prevent the UI from begin updated, making your application look like its hung/crashed
您可以使用 SwingWorker
执行文件加载部分,将每一行传递给 publish
方法并通过 process
方法将这些行添加到文本区域...
You could use a SwingWorker
to perform the file loading part, passing each line to the publish
method and adding the lines to the text area via the process
method...
public class FileReaderWorker extends SwingWorker<List<String>, String> {
private final File inFile;
private final JTextArea output;
public FileReaderWorker(File file, JTextArea output) {
inFile = file;
this.output = output;
}
public File getInFile() {
return inFile;
}
public JTextArea getOutput() {
return output;
}
@Override
protected void process(List<String> chunks) {
for (String line : chunks) {
output.append(line);
}
}
@Override
protected List<String> doInBackground() throws Exception {
List<String> lines = new ArrayList<String>(25);
java.io.File cpFile = getInFile();
if (cpFile != null && cpFile.exists() == true) {
File file = cpFile;
BufferedReader br = null;
String strLine = "";
String logPrint = "";
try {
// New Buffer Reader
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while ((strLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(strLine, ";");
while (st.hasMoreTokens()) {
logPrint = st.nextToken();
publish(logPrint);
}
}
} catch (Exception e) {
publish("Failed read in file: " + e);
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (Exception e) {
}
}
} else {
publish("Input file does not exist/hasn't not begin specified");
}
return lines;
}
}
查看课程:Swing 中的并发更多信息
这篇关于在 GUI 中显示从文件中读取的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!