我如何将 JTextArea 添加到此代码中?

发布于 2024-12-18 05:54:28 字数 1468 浏览 2 评论 0原文

请为此设置默认文本。我正在尝试创建一个显示文件内容的文本区域。我计划使用java库中的文件打开器来选择文件。我会将文件内容存储在字符串中,然后将其打印在 JtextArea 中。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Parser implements ActionListener {
protected JTextArea textArea;
Parser() {
    JFrame f = new JFrame("DECA Test Parser");
    f.setSize(400, 400);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();
    JMenu jmFile = new JMenu("File");
    JMenuItem jmiOpen = new JMenuItem("Open");
    JMenuItem jmiSave = new JMenuItem("Save");
    JMenuItem jmiExit = new JMenuItem("Exit");
    jmFile.add(jmiOpen);
    jmFile.add(jmiSave);
    jmFile.addSeparator();
    jmFile.add(jmiExit);
    jmb.add(jmFile);

    JMenu jmHelp = new JMenu("Help");
    JMenuItem jmiAbout = new JMenuItem("About");
    jmHelp.add(jmiAbout);
    jmb.add(jmHelp);

    jmiOpen.addActionListener(this);
    jmiSave.addActionListener(this);
    jmiExit.addActionListener(this);
    jmiAbout.addActionListener(this);


    f.setJMenuBar(jmb);

    f.setVisible(true);


}
public void actionPerformed(ActionEvent ae) {
    String comStr = ae.getActionCommand();
    if (comStr == "Exit");
        System.exit(0);
}
public static void main(String args[]) {
    new Parser();
}
}

Please set default text for this. I am trying to make a text area that displays the contents of a file. I plan to use a file opener in the java library to select the file. I wills store the files contents in a string and then print it in the JtextArea.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Parser implements ActionListener {
protected JTextArea textArea;
Parser() {
    JFrame f = new JFrame("DECA Test Parser");
    f.setSize(400, 400);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();
    JMenu jmFile = new JMenu("File");
    JMenuItem jmiOpen = new JMenuItem("Open");
    JMenuItem jmiSave = new JMenuItem("Save");
    JMenuItem jmiExit = new JMenuItem("Exit");
    jmFile.add(jmiOpen);
    jmFile.add(jmiSave);
    jmFile.addSeparator();
    jmFile.add(jmiExit);
    jmb.add(jmFile);

    JMenu jmHelp = new JMenu("Help");
    JMenuItem jmiAbout = new JMenuItem("About");
    jmHelp.add(jmiAbout);
    jmb.add(jmHelp);

    jmiOpen.addActionListener(this);
    jmiSave.addActionListener(this);
    jmiExit.addActionListener(this);
    jmiAbout.addActionListener(this);


    f.setJMenuBar(jmb);

    f.setVisible(true);


}
public void actionPerformed(ActionEvent ae) {
    String comStr = ae.getActionCommand();
    if (comStr == "Exit");
        System.exit(0);
}
public static void main(String args[]) {
    new Parser();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-12-25 05:54:28

您可以通过添加以下几行来添加文本区域:

textArea = new JTextArea(); 
f.add(textArea);

然后,您可以向其中添加文本,如下所示:

textArea.setText("your file content here");

You can add a textArea by adding these few lines:

textArea = new JTextArea(); 
f.add(textArea);

Then, you can add text to it like this:

textArea.setText("your file content here");
孤星 2024-12-25 05:54:28
//  Go through this code it reads a file named read.txt in c drive and prints its content to text area  


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    public class Test implements ActionListener {
    protected JTextArea textArea;
    Test() throws IOException {
        JFrame f = new JFrame("DECA Test Parser");
        f.setSize(400, 400);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar jmb = new JMenuBar();
        JMenu jmFile = new JMenu("File");
        JMenuItem jmiOpen = new JMenuItem("Open");
        JMenuItem jmiSave = new JMenuItem("Save");
        JMenuItem jmiExit = new JMenuItem("Exit");
        jmFile.add(jmiOpen);
        jmFile.add(jmiSave);
        jmFile.addSeparator();
        jmFile.add(jmiExit);
        jmb.add(jmFile);

        JMenu jmHelp = new JMenu("Help");
        JMenuItem jmiAbout = new JMenuItem("About");
        jmHelp.add(jmiAbout);
        jmb.add(jmHelp);

        jmiOpen.addActionListener(this);
        jmiSave.addActionListener(this);
        jmiExit.addActionListener(this);
        jmiAbout.addActionListener(this);

        textArea = new JTextArea();

        FileInputStream fstream = new FileInputStream("C:\\read.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
        // Print the content on the console
          textArea.append(strLine);
        } 

        f.getContentPane().add(textArea);
        f.setJMenuBar(jmb);

        f.setVisible(true);


    }
    public void actionPerformed(ActionEvent ae) {
        String comStr = ae.getActionCommand();
        if (comStr == "Exit");
            System.exit(0);
    }
    public static void main(String args[]) {
        try {
            new Test();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    }
//  Go through this code it reads a file named read.txt in c drive and prints its content to text area  


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    public class Test implements ActionListener {
    protected JTextArea textArea;
    Test() throws IOException {
        JFrame f = new JFrame("DECA Test Parser");
        f.setSize(400, 400);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar jmb = new JMenuBar();
        JMenu jmFile = new JMenu("File");
        JMenuItem jmiOpen = new JMenuItem("Open");
        JMenuItem jmiSave = new JMenuItem("Save");
        JMenuItem jmiExit = new JMenuItem("Exit");
        jmFile.add(jmiOpen);
        jmFile.add(jmiSave);
        jmFile.addSeparator();
        jmFile.add(jmiExit);
        jmb.add(jmFile);

        JMenu jmHelp = new JMenu("Help");
        JMenuItem jmiAbout = new JMenuItem("About");
        jmHelp.add(jmiAbout);
        jmb.add(jmHelp);

        jmiOpen.addActionListener(this);
        jmiSave.addActionListener(this);
        jmiExit.addActionListener(this);
        jmiAbout.addActionListener(this);

        textArea = new JTextArea();

        FileInputStream fstream = new FileInputStream("C:\\read.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
        // Print the content on the console
          textArea.append(strLine);
        } 

        f.getContentPane().add(textArea);
        f.setJMenuBar(jmb);

        f.setVisible(true);


    }
    public void actionPerformed(ActionEvent ae) {
        String comStr = ae.getActionCommand();
        if (comStr == "Exit");
            System.exit(0);
    }
    public static void main(String args[]) {
        try {
            new Test();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    }
新雨望断虹 2024-12-25 05:54:28

JTextArea 扩展了 JTextComponent,它有 2 个方法,

public void read(Reader in, Object desc)
public void write(Writer out)

只需将 FileReader/FileWriter 传递到那里即可。

JTextArea extends JTextComponent which has 2 methods

public void read(Reader in, Object desc)
public void write(Writer out)

Just pass FileReader/FileWriter there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文