在 java swing GUI 中显示文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15602104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display file in java swing GUI
提问by Dobby
Having problems displaying a ".txt" file in a java swing Gui.
在 java swing Gui 中显示“.txt”文件时出现问题。
I have the code to read the .txt file and display it in the cmd window but cant seem to link it so it will display in the Gui.
我有读取 .txt 文件并将其显示在 cmd 窗口中的代码,但似乎无法链接它,因此它将显示在 Gui 中。
here is the code that reads the file:
这是读取文件的代码:
import java.io.*;
public class ReadFile {
public static void main (String [] args) throws IOException{
String inputLine = "";
FileReader inputFile = new FileReader( "player.txt");
BufferedReader br = new BufferedReader(inputFile);
System.out.println( "\nThe contents of the file player.txt are ");
// Read lines of text from the file, looping until the
// null character (ctrl-z) is endountered
while( ( inputLine = br.readLine()) != null){
System.out.println(inputLine); // Write to the screen
}
br.close(); // Close the stream
} // end of main method
} // end of class
here is the code for the Swing Gui
这是 Swing Gui 的代码
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class GUI extends JFrame{
private JButton button;
//Set Hight and Width of Interface
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
//Method to Create Inteface
public GUI(){
createComponents();
createDisplay();
setSize(WIDTH, HEIGHT);
}
//An Action Listener To Contorl Action Preformed
class ClickListener implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
//Method to Create Button
private void createComponents(){
button = new JButton("Click Me");
ActionListener listener = new ClickListener();
button.addActionListener(listener);
JPanel panel = new JPanel();
panel.add(button);
add(panel);
}
//Method To Create Display
private void createDisplay(){
JTextArea myArea = new JTextArea();
JPanel panel1 = new JPanel();
panel1.add(myArea);
add(panel1);
}
}
and here is the code to run the Swing GUI
这是运行 Swing GUI 的代码
import javax.swing.*;
public class GUITest{
public static void main (String [] agrs){
JFrame frame = new GUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
All files are in the same folder on my laptop along with the .txt file. And i am using TextPad to code and compile.
所有文件和 .txt 文件都在我的笔记本电脑上的同一个文件夹中。我正在使用 TextPad 进行编码和编译。
all help would be grate
所有的帮助都会被炉排
thanks Derek
谢谢德里克
采纳答案by camickr
here is the code that reads the file:
这是读取文件的代码:
Get rid of that code, it is not needed.
摆脱那个代码,它是不需要的。
JTextArea has a read(...)
method that you can use to read the text file directly into the text area.
JTextArea 有一个read(...)
方法可以用来将文本文件直接读入文本区域。
回答by Infusion of Wormwood n Asfodel
You could create an object of the ReadFile
class in the Actionlistener
class and call ReadFile
's main method.
您可以在ReadFile
类中创建类的对象Actionlistener
并调用ReadFile
的 main 方法。