简体   繁体   English

Swing Hello World应用程序如何工作?

[英]How does the Swing Hello World application works?

I am trying to figure out how the Swing based Hello World application works. 我试图弄清楚基于Swing的Hello World应用程序是如何工作的。 This is the code I have: 这是我的代码:

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


public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);

        //====================================================== constructor
        public HelloWorldSwing() {
            //... Set initial text, scrolling, and border.
            m_resultArea.setText("Enter more text to see scrollbars");
            JScrollPane scrollingArea = new JScrollPane(m_resultArea);
            scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));

            // Get the content pane, set layout, add to center
            Container content = this.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(scrollingArea, BorderLayout.CENTER);
            this.pack();
        }

        //============================================================= main
        public static void main(String[] args) {
            JFrame win = new HelloWorldSwing();
            win.setTitle("TextAreaDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.setVisible(true);
        }


}

The code was taken from here (I slightly modified it). 该代码是从这里获取的 (我稍加修改了)。 I have two main questions about the example but if you answer at leas one of them would appreciate it a lot. 关于这个例子,我有两个主要问题,但是如果您回答的话,其中之一将非常感激。 Here are my questions: 这是我的问题:

1. As far as I understand the "main" method is run automatically. 1.据我了解,“ main”方法是自动运行的。 In this method we instantiate a "win" object and, as a consequence the constructor of the class will be executed. 在这种方法中,我们实例化了一个“ win”对象,因此将执行该类的构造函数。 But when the first line of the class is executed (JTextArea m_resultArea = new JTextArea(6, 30));. 但是当执行该类的第一行时(JTextArea m_resultArea = new JTextArea(6,30));。

2. Is there a good reason to instantiate the text area (m_resultArea) outside the constructor and then set its parameters (setText) within the constructor. 2.是否有充分的理由在构造函数外部实例化文本区域(m_resultArea),然后在构造函数内设置其参数(setText)。 Why we cannot instantiate the text area in the constructor? 为什么我们不能在构造函数中实例化文本区域? Why we cannot set the parameters of the text area beyond the constructor? 为什么我们不能在构造函数之外设置文本区域的参数? (Just for the sake of consistency). (仅出于一致性考虑)。

The code you posted violates Swing threading rules. 您发布的代码违反了Swing线程规则。 The code in main method has to run on Event Dispatch Thread and should look like: main方法中的代码必须在Event Dispatch Thread上运行,并且应类似于:

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

Side note: 边注:

This is a typical example of where using a debugger will greatly help you understand the flow of execution. 这是一个使用调试器将极大地帮助您了解执行流程的典型示例。 If you don't know how to use a debugger, I recommend these great free video tutorials for the Eclipse debugger. 如果您不知道如何使用调试器,那么我建议为Eclipse调试器提供这些很棒的免费视频教程


Now, to answer your questions: 现在,回答您的问题:

As far as I understand the "main" method is run automatically. 据我了解,“ main”方法是自动运行的。 In this method we instantiate a "win" object and, as a consequence the constructor of the class will be executed. 在这种方法中,我们实例化了一个“ win”对象,因此将执行该类的构造函数。

The object being instantiated is of type HelloWorldSwing , not win . 实例化的对象是HelloWorldSwing类型,而不是win win is just the name of the variable. win只是变量的名称。

Is there a good reason to instantiate the text area (m_resultArea) outside the constructor and then set its parameters (setText) within the constructor. 是否有充分的理由在构造函数外部实例化文本区域(m_resultArea),然后在构造函数内设置其参数(setText)。

Instantiation can occur outside of the constructor, but regular method calls like setText() must occur within another method. 实例化可以在构造函数之外进行,但常规方法调用(如setText()必须在另一个方法内进行。

But when the first line of the class is executed (JTextArea m_resultArea = new JTextArea(6, 30));. 但是当执行该类的第一行时(JTextArea m_resultArea = new JTextArea(6,30));。

The first line of the class is executed before the constructor. 类的第一行在构造函数之前执行。

Why we cannot instantiate the text area in the constructor? 为什么我们不能在构造函数中实例化文本区域?

You can as well, up to you. 您也可以,取决于您自己。

Why we cannot set the parameters of the text area beyond the constructor? 为什么我们不能在构造函数之外设置文本区域的参数? (Just for the sake of consistency). (仅出于一致性考虑)。

As I said before, loops, method calls, etc. must occur within a method (constructor, or main, or any other method). 正如我之前所说,循环,方法调用等必须在一个方法(构造函数,main,或任何其他方法)内发生。

For more info on field initialization, you can refer to the Sun tutorial here . 有关字段初始化的更多信息,您可以在此处参考Sun教程

1- The instantiation of the JTextArea is the first thing that happens when you create an object of HelloWorldSwing. 1-创建您的HelloWorldSwing对象时,首先发生JTextArea的实例化。 The constructor is executed right after that. 此后立即执行构造函数。 Since it's not static, you cannot access it without and object (in this case, win). 由于它不是静态的,因此如果没有和对象,您将无法访问它(在这种情况下为win)。

2- There is a good reason, but it could be instantiated inside the constructor. 2-有充分的理由,但可以在构造函数中实例化。 It's like that because that way you make sure it is not null when you want to use it. 之所以这样,是因为您可以确保要使用的时候它不为null。 If you used it in the constructor before you instantiated, you would get an Exception. 如果在实例化之前在构造函数中使用它,则将获得Exception。 It's just a way to make sure it's already created before the class object is contructed. 这只是确保在构造类对象之前已创建它的一种方法。

  1. the line is executed automatically when the HelloWorldSwing object is created. 创建HelloWorldSwing对象时,将自动执行该行。 All fields of the class are initialized before the constructor is actually called. 实际调用构造函数之前 ,将初始化类的所有字段。

  2. you can do it either way. 您可以选择任何一种方式。 In this case, as resultArea is modified inside the constructor, I would put the instantiation inside the constructor as well, but it is IMHO a style issue. 在这种情况下,由于在构造函数内部修改了resultArea ,我也将实例化放入了构造函数内部,但是恕我直言,这是样式问题。 And you are also free to modify the properties of resultArea anytime after the constructor has finished. 而且,在构造函数完成后,您还可以随时随时修改resultArea的属性。

1) No, you do not instantiate a "win" object; 1)不,您不实例化“ win”对象; you instantiate a HelloWorldSwing object. 您实例化一个HelloWorldSwing对象。 Instance variables are initialized before the body of the constructor runs . 实例变量在构造函数的主体运行之前被初始化。

2) There's no reason why you can't instantiate the text area in the constructor. 2)没有理由不能在构造函数中实例化文本区域。 You can set various properties of the TextArea any time you like. 您可以随时设置TextArea的各种属性。

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

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