简体   繁体   English

向initComponents添加参数的其他方法[java-netbeans]

[英]Other ways to add a parameter to initComponents [java-netbeans]

I have a huge problem with my GUI java project in netbeans. 我在netbeans中的GUI Java项目遇到很大的问题。 It is well-known that the code compiled by netbeans is read-only and I need an other way to add a parameter to the initComponents method, besides calling a myInitComponents method, identical to initComponents, and calling it in the constructor. 众所周知,netbeans编译的代码是只读的,除了调用与initComponents相同的myInitComponents方法并在构造函数中调用之外,我还需要其他方法将参数添加到initComponents方法中。 Now I have this: 现在我有这个:

public class MainFrame {

    public MainFrame() {
       DefaultStyledDocument doc = new DefaultStyledDocument();
       myInitComponents(doc);
    }

    myInitComponents (DefaultStyledDocument doc) {
       //components
       textModel = new javax.swing.JTextPane(doc);
       //components
    }

    initComponents () {
      //components
    }

In this way it works, but every time I change something within the frame, I have to copy and pase all the new code of initComponents inside myInitComponent. 通过这种方式,它可以工作,但是每次我在框架中进行更改时,都必须复制并插入myInitComponent中所有initComponents的新代码。 Moreover, this is a very awful way to do that. 此外,这是一种非常糟糕的方法。 Is there any other way to add that parameter? 还有其他方法可以添加该参数吗? Any help is appreciated! 任何帮助表示赞赏!

You can add a parameter to the MainFrame constructor, place it in a field, use custom creation code in the properties table of the GUI builder. 您可以向MainFrame构造函数添加参数,将其放置在字段中,并在GUI构建器的属性表中使用自定义创建代码。

There a couple of free code places to insert in the code of initComponents. 在initComponents的代码中可以插入一些免费的代码。 Create custom code is such a place; 创建自定义代码就是这样一个地方;

private final DefaultStyledDocument doc = new DefaultStyledDocument();

And in "custom creation code:" 并在“自定义创建代码”中:

new JTextPane(doc)

Which can also be used for custom panels etcetera. 也可以用于自定义面板等。

I have a huge problem with my GUI java project in netbeans. 我在netbeans中的GUI Java项目遇到很大的问题。 It is well-known that the code compiled by netbeans is read-only and I need an other way to add a parameter to the initComponents method, besides calling a myInitComponents method, identical to initComponents, and calling it in the constructor 众所周知,netbeans编译的代码是只读的,除了调用与initComponents相同的myInitComponents方法并在构造函数中调用之外,我还需要其他方法将参数添加到initComponents方法中。

I think you are probably confused between using constructors for initializations and setters for accessing values. 我认为您可能在使用构造函数进行初始化和使用setter访问值之间感到困惑。

This is as good as asking: if you have a class with attributes like a, b & c, how to create a setter which set all attributes. 这就像问一个问题一样好:如果您有一个具有诸如a,b和c之类的属性的类,那么如何创建一个设置所有属性的设置器。 This is something you should avoid. 这是您应避免的事情。 You could just create an individual setter and getter for each property instead of trying to use an init to set all attributes. 您可以为每个属性创建一个单独的setter和getter,而不是尝试使用init来设置所有属性。

You should be doing this: 您应该这样做:

class MyClass
{
    private int a;
    private int b;
    private int c;

    public MyClass(){
        init();
    }

    private void init(){
        a = 100;
        b = 200;
        c = 300;
    }

    public int getA(){return a;}  
    public int getB(){return b;}
    public int getC(){return c;}

    public void setA(int a){this.a = a;}
    public void setB(int b){this.a = b;}
    public void setC(int c){this.a = c;}
}

instead of this: 代替这个:

class MyClass
{
    private int a;
    private int b;
    private int c;

    public MyClass(){
        init();
    }

    private void init(){
        a = 100;
        b = 200;
        c = 300;
    }

    public void myInit(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

this is a very awful way to do that. 这是一种非常糟糕的方法。 Is there any other way to add that parameter? 还有其他方法可以添加该参数吗?

So you asked, if you have one more attribute, say int d . 因此,您问,如果还有一个属性,请说int d How should I add it to the parameter list of myInit() . 我应该如何将其添加到myInit()的参数列表中。 So you already start to see the problem with this approach for your class design. 因此,您已经开始在您的班级设计中看到这种方法的问题。

If possible, we try to achieve low coupling and high cohesion in our design. 如果可能,我们尝试在设计中实现低耦合高内聚 When you dump various unrelated attributes within a single method, you are steering towards low cohesion (a method which is not performing a very specific task). 当在单个方法中转储各种不相关的属性时,您将趋向于低内聚性(这种方法无法执行非常具体的任务)。

If you try to use a single method like myInit() and use it as a setter to set multiple fields, it can cause a number of problems. 如果尝试使用像myInit()这样的单个方法并将其用作设置多个字段的设置器,则可能会导致许多问题。

  • What if the user only wants to set a specific attribute, and not the rest? 如果用户只想设置一个特定的属性,而不是其余的,该怎么办?

So to answer your question, use individual setters for each attribute, unless the attributes are closely related for example: 因此,要回答您的问题,请对每个属性使用单独的设置器,除非这些属性紧密相关,例如:

setLocation(int x, int y);
setBounds(int x, int y, int width, int height);

At last I fixed this in a very simple way. 最后,我以非常简单的方式解决了这个问题。 I inserted all the code needed for the DefaultStyleDocument in the initComponents() method by clicking on customize code and adding it as pre-creation code. 通过单击定制代码并将其添加为预创建代码,我将DefaultStyleDocument所需的所有代码插入initComponents()方法中。

public class MainFrame {

    public MainFrame() {

       myInitComponents();
    }

  //delete the myInitComponents() method

    initComponents () {
   //code useful for the DefaultStyledDocument..
    DefaultStyledDocument doc = new DefaultStyledDocument();
      //components
      textModel = new javax.swing.JTextPane(doc);
    }

Hope this could be useful to somebody. 希望这对某人有用。

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

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