简体   繁体   English

如何通过单击按钮显示此文件的内容?

[英]How can I display contents of this file through the click of a button?

right now what I am trying to do is load the contents of a file (readfile.txt) into the data viewer window through the click of a button.现在我要做的是通过单击按钮将文件(readfile.txt)的内容加载到数据查看器 window 中。 I created my window in 1 class and read the contents of the file in another (that is what I was asked to do).我在 1 class 中创建了我的 window 并在另一个中读取文件的内容(这是我被要求做的)。 What I was asked to do is make the button called 'load' insert the contents of a file called 'readfile.txt' into the window.我被要求做的是让名为“加载”的按钮将名为“readfile.txt”的文件的内容插入 window。 The file contains simple strings.该文件包含简单的字符串。 So far iv put the contents of the file in the window without the load button by using a method and append as shown below.至此,通过使用方法和append,将文件内容放入window中,没有加载按钮,如下图所示。

public class Challenge                                              
{        
  private static DataViewer dataViewer = new DataViewer();
    
  
  public static String readFile()                                               
  {
     String file = "Tasks.csv";                                                                                         
     String i = "";         
    try(BufferedReader in = new BufferedReader(new FileReader(file)))                                           
   {     
            
     while(in.ready())                                              
     {                                              
       i = i + in.readLine() + System.lineSeparator();
     }                                              
    }                                               
    catch(Exception a)                                              
    {     
        
    }
    return i;
  }
  
  public static void displayFile()
  {
      dataViewer.setFileDisplay(readFile());
// add readFile() text into the text area of the viewer 
  }
}
public class DataViewer extends JFrame{

    JTextArea fileDisplay;
     public Button load;
     public Button quitViewer;
     
     public DataViewer() throws HeadlessException {
         
         load = new Button("Load File");
         quitViewer = new Button("Quit");
         fileDisplay = new JTextArea(10,10);
         
         JScrollPane scroll = new JScrollPane(fileDisplay);
         
         setTitle("Data Viewer");
         setBounds(400,0,400,400);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
         
         var layout = new JPanel(new BorderLayout());
         layout.add(scroll, BorderLayout.CENTER);
         layout.add(load, BorderLayout.NORTH);
         layout.add(quitViewer, BorderLayout.SOUTH);

         
         handler action = new handler();
         
         load.addActionListener(action); // this button is meant to load the contents of the file into the window(so what setFileDisplay() method does).
         quitViewer.addActionListener(action); //this button closes the window
                  
         add(layout);
         setVisible(true);

     }
     
     private class handler implements ActionListener
             {
           @Override
           public void actionPerformed(ActionEvent e)
           {
               dispose(); // deals with the quit button.
           };
     };
     
   public void setFileDisplay(String i) {
         fileDisplay.append(i); //adds contents of the parameter(readfile.txt) into the jtextarea.
     }
    
 }

This is how you can create an action listener:这是创建动作侦听器的方法:

load.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //Assuming that your static methods are working correctly
        Challenge.readFile();
        Challenge.displayFile();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM