how to add .txt file to a JTextArea and where to place my .txt file?

后端 未结 2 797
梦谈多话
梦谈多话 2020-12-22 06:27

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

2条回答
  •  梦毁少年i
    2020-12-22 07:20

    where to place my .txt file?

    You can try any one

    // Read from same package 
    InputStream in = getClass().getResourceAsStream("abc.txt");
    
    // Read from resources folder parallel to src in your project
    File file = new File("resources/abc.txt");
    
    // Read from src/resources folder
    InputStream in = getClass().getResourceAsStream("/resources/abc.txt");
    

    enter image description here


    --EDIT--

    Must read A Visual Guide to Layout Managers.

    Here is some points from your code:

    • Don't use null layout Rules.setLayout(null);
    • Call JFrame#setVisible(true); in the end when all the components are added
    • Always use SwingUtilities.invokeLater() to initialize the GUI
    • Follow Java Naming convention.

提交回复
热议问题