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

后端 未结 2 779
梦谈多话
梦谈多话 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条回答
  • 2020-12-22 07:11

    To read a file into a JTextArea you can simply use JTextArea#read, this will, however, discard the current contents of the JTextArea

    Updated

    After adding the code to an IDE, I've noted that you are not adding rules (the JTextArea) to anything so it will never be visible...

    The general structure of how you create your UI is also a little skewed, try something more like...

    public class Rules extends JFrame {
    
        public Rules() throws IOException {
            super();
            // Initial setu[
            setTitle("Rules Of Santorini Board Game");
    
            // Create the basic UI content
            JTextArea textArea = new JTextArea(40, 20);
            JScrollPane scrollPane = new JScrollPane(textArea);
    
            // Read the file
            try (BufferedReader reader = new BufferedReader(new FileReader(new File("resources/New Text Document.txt")))) {
                textArea.read(reader, "File");
            } catch (IOException exp) {
                exp.printStackTrace();
            }
    
            getContentPane().setBackground(Color.ORANGE);
    
            JButton ok = new JButton("Got It");
            add(textArea, BorderLayout.SOUTH);
            add(ok, BorderLayout.SOUTH);
    
            ok.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //dispose();
                    //?? No idea what this is for, but it won't do much
                    setVisible(false);
    
                }
            });
    
            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
    
        }
    
    }
    

    Don't use MouseListeners with buttons, instead you should be using an ActionListener

    Don't use null layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen.

    Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work...

    0 讨论(0)
  • 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.
    0 讨论(0)
提交回复
热议问题