简体   繁体   English

如何将文本区域(JTextArea)放入窗口(JFrame)中?

[英]How to put a text area (JTextArea) into a window (JFrame)?

I have a working code which creates a window with a text area. 我有一个工作代码,该代码创建一个带有文本区域的窗口。 The code is here . 代码在这里 I try to figure out how this code works. 我试图弄清楚这段代码是如何工作的。 A lot of things are clear: 很多事情很清楚:

  1. The main-method creates an instance of the TextAreaDeom class (which is subclass of the JFrame). 主方法创建TextAreaDeom类(它是JFrame的子类)的实例。 In other words, the main-method creates a window. 换句话说,主方法创建一个窗口。
  2. In the main-method we set some "parameters" of the window and make it visible. 在主要方法中,我们设置了窗口的一些“参数”并使其可见。

It is not clear to me, in which place we put the text area in the window. 我不清楚,我们将文本区域放在窗口的哪个位置。 I see that the text area is created right before the constructor. 我看到该文本区域是在构造函数之前创建的。 I also see that in the constructor we set some "parameters" of the text area (setText). 我还看到,在构造函数中,我们设置了文本区域(setText)的一些“参数”。 I also see that in the constructor we create aa scrolling area and set some parameters for it. 我还看到在构造函数中,我们创建了一个滚动区域并为其设置了一些参数。 I see that the scrolling area is "connected" to the text area (since we use the instance of the text area to create the scrolling area). 我看到滚动区域已“连接”到文本区域(因为我们使用文本区域的实例来创建滚动区域)。 I also see that we create an object called "content" (using the current window) and we "add" the scrolling area to the "content". 我还看到,我们创建了一个名为“内容”的对象(使用当前窗口),并将滚动区域“添加”到“内容”中。

But at which place the text area is added to the window? 但是,在哪个位置将文本区域添加到窗口? May be I can say that the text area is added to the scrolling area and the scrolling area is added to the "content" and the content is a part of the window-object? 可以说文本区域被添加到滚动区域中,而滚动区域被添加到“内容”中,并且内容是窗口对象的一部分吗?

in line 16 you create a JScrollPane which wraps around your JTextArea object. 在第16行中,您将创建一个JScrollPane,该JScrollPane包裹了您的JTextArea对象。 On line 21 you add this JScrollPane, which contains your TextArea to the ContentPane of the JFrame.As you call getContentPane() instead of creating a new one the ContentPane is already part of the JFrame. 在第21行上,将此JScrollPane添加到JFrame的ContentPane中,该JScrollPane包含TextArea。当您调用getContentPane()而不是创建新的JScrollPane时,ContentPane已成为JFrame的一部分。
All elements of the ContentPane will be displayed as part of the JFrame. ContentPane的所有元素都将显示为JFrame的一部分。 The add method of JFrame is only for convenience and forwards the call to the JFrames ContentPane. JFrame的add方法只是为了方便起见,并将调用转发到JFrames ContentPane。

The scroll pane scrollingArea is created with the text area inside. 滚动窗格scrollingArea是使用内部文本区域创建的。 scrollPane , was constructed with text area m_resultArea (see the documentation for JScrollPane 's constructor ). scrollPane是使用文本区域m_resultArea构造的 (请参见JScrollPane构造函数的文档)。 is then added to the frame's content pane. 然后将其添加到框架的内容窗格。

The GUI elements should be constructed on the EDT . GUI元素应在EDT上构建。 Here is a more reliable main() method for the program cited above. 这是上面引用的程序的更可靠的main()方法。

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame win = new TextAreaDemo();
            win.setTitle("TextAreaDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.pack();
            win.setVisible(true);
        }
    });
}

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

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