初始化 GUI 字段的最佳方法是什么 [英] What is best way to initializing GUI fields

查看:22
本文介绍了初始化 GUI 字段的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

public class MyWindow extends Window {

    private final Panel panel = new Panel(new Layout());
    private final TextField textField = new TextField();

    private TextArea textArea;
    private String value;

    public MyWindow(String value) {
         this.value = value;
         setHeading("Example");
         textArea = createTextArea();
         panel.add(textArea);
         panel.add(textField);
         add(panel);
    }

    private TextArea createTextArea() {
         TextArea textArea = new TextArea();
         textArea.setValue(value);
         textArea.setToolTip("tooltip");
    }

}

密切关注 TextArea - 初始化此字段的最佳方法是什么?内联字段初始化或默认构造函数?

Keep eye on TextArea - What is bestway to initialize this field? Inline field initialization or default constructor?

我应该像上面那样做还是像这样:

Should I do as above or maybe like this:

   public class MyWindow extends Window {

    ...
    private TextArea textArea = new TextArea();
    ...

    public MyWindow(String value) {
         ...
         setupTextArea();
         ...  
    }

    private TextArea setupTextArea() {
         textArea.setValue(value);
         textArea.setToolTip("tooltip");
    }

}

我有更多的字段要初始化,我不能总是在一行中声明该字段.有时在 GUI 中创建单个项目需要创建两个其他对象.因此,代码变得难以阅读——一些字段在构造函数中初始化,而所有字段都在声明中.在我看来,它看起来不太好.

I have many more fields to initializing and I can't always declare the field in one line. Sometimes the creation of a single item in the GUI needs to create two other objects. Therefore, the code becomes difficult to read - some fields are initialized in the constructor, and all rest in the declaration. It doesn't look too good in my opinion.

你是怎么做到的?

也许我必须在构造函数中创建所有字段并将所有信息作为参数传递给方法?

Maybe I have to create all fields in the constructor and pass all informations to methods as arguments?

推荐答案

请参阅以下示例,说明如何完成.
使用 initialize 方法在它们自己的方法中创建特定组件(或者类也是可选的,例如,如果面板是在框架内创建的)

顺便说一句不要让你的类从Window扩展,这被认为是不好的做法.而是在您的类中创建一个窗口对象.例如,在我的示例中,我创建了一个 Jframe 而不是从 Jframe 类扩展.

See the following example how it could be done.
Use a initialize method to create the specific components in their own method (or class is also optional if for example panels are created within frames)

By the way don't let your class extend from Window, this is considered as bad practice. Rather create a window object within your class. For example in my example I create a Jframe and not extend from Jframe class.

public class Program {

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() { new MainFrame();}
    });
    }
}



public class MainFrame {      
    private JFrame frame;
    private JTextArea textArea_input;
    private JTextArea textArea_output;
    private JButton button_parse;
    private JButton button_createQuestionnaire;
    private Form form;

    public MainFrame() {
    initialize();
    }

    private void initialize() {
    final Context context = new Context();
    createFrame();      
    createTextFieldInput();
    createButtonParse(context);
    createButtonQuestionnaire(context);
    createTextFieldOutput();
    }

    private void createButtonQuestionnaire(final Context context) {
    button_createQuestionnaire = new JButton("Create Questionnaire");
    button_createQuestionnaire.setEnabled(false);
    button_createQuestionnaire.setSize(TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT);
    frame.getContentPane().add(button_createQuestionnaire);     
    button_createQuestionnaire.addMouseListener(buttonClickCreateQuestionnaire(context));
    }

这篇关于初始化 GUI 字段的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆