简体   繁体   English

使用Swing在JPanel中创建JTextArea

[英]Create a JTextArea in a JPanel using Swing

I want to create a Jtextarea in the first half of the below panel under tab2. 我想在tab2下的下面面板的上半部分中创建一个Jtextarea。 I want to iterate and write the contents present in the test.txt file into this newly created Jtext area. 我想迭代并将test.txt文件中存在的内容写入此新创建的Jtext区域。 The text.txt file is present in the location (C:\\test.txt) . text.txt文件位于(C:\\ test.txt)位置 Can someone suggest me how to achieve this for the below code. 有人可以建议我如何通过以下代码实现这一目标。 (Note: The below code is a JPane with three tabs tab1,tab2,tab3) and the tab 2 has been splited into two halves.) (注意:下面的代码是带有三个选项卡tab1,tab2,tab3的JPane,并且选项卡2已分为两半。)

I am new to the Jtextarea concept so it will be nice from understanding viewpoint if anyone can provide some suggestion code for my below code: 我是Jtextarea概念的新手,所以如果有人可以为我的以下代码提供一些建议代码,从理解的角度来看这将是很好的:

text.txt contents in my local disk is as below 我本地磁盘中的text.txt内容如下

username:test1  
Password:test1  
DataBasename: testDB

CODE: 码:

import javax.swing.*;
import java.awt.*;

public class SplitPaneExp {

    public static void main(String[] args){
        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("WELCOME");
                // A better close operation..
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JTabbedPane tab = new JTabbedPane();
                frame.add(tab, BorderLayout.CENTER);
                JButton button = new JButton("1");
                tab.add("tab1", button);

                // this GridLayout will create a single row of components,
                // with equal space for each component
                JPanel tab2Panel = new JPanel(new GridLayout(0,1));
                button = new JButton("2");
                tab2Panel.add(button);
                tab2Panel.add(new JButton("Second window"));
                // add the panel containing two buttons to the tab
                tab.add("tab2", tab2Panel);

                button = new JButton("3");
                tab.add("tab3", button);
                // a better sizing method..
                //frame.setSize(400,400);
                frame.pack();
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class SplitPaneExp {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("WELCOME");
                // A better close operation..
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JTabbedPane tab = new JTabbedPane();
                frame.add(tab, BorderLayout.CENTER);
                JButton button = new JButton("1");
                tab.add("tab1", button);

                // this GridLayout will create a single row of components,
                // with equal space for each component
                JPanel tab2Panel = new JPanel(new GridLayout(0, 1));
//                button = new JButton("2");

                //Hi, I added a few things here!
                String output = "";
                try {
                    BufferedReader br = new BufferedReader(new FileReader(new File("C:\test.txt")));
                    String line = br.readLine();
                    while(line != null) {
                        if(output.length() > 0) {
                            output = output + "\n";
                        }
                        output = output + line;
                        line = br.readLine();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JTextArea jta = new JTextArea(output);
                tab2Panel.add(jta);
                // Done.

//                tab2Panel.add(button);
                tab2Panel.add(new JButton("Second window"));
                // add the panel containing two buttons to the tab
                tab.add("tab2", tab2Panel);

                button = new JButton("3");
                tab.add("tab3", button);
                // a better sizing method..
                //frame.setSize(400,400);
                frame.pack();
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Hope it helps. 希望能帮助到你。

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

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