【问题标题】:Other ways to add a parameter to initComponents [java-netbeans]向 initComponents 添加参数的其他方法 [java-netbeans]
【发布时间】:2016-03-19 16:27:53
【问题描述】:

我在 netbeans 中的 GUI java 项目有一个大问题。 众所周知,netbeans编译出来的代码是只读的,除了调用一个myInitComponents方法,和initComponents一样,在构造函数中调用,我还需要另外一种方式给initComponents方法加参数。 现在我有了这个:

public class MainFrame {

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

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

    initComponents () {
      //components
    }

以这种方式它可以工作,但是每次我在框架内更改某些内容时,我都必须将 initComponents 的所有新代码复制并粘贴到 myInitComponent 中。 此外,这是一种非常糟糕的方法。 有没有其他方法可以添加该参数? 任何帮助表示赞赏!

【问题讨论】:

  • 你可以看看下面我的解决方案。

标签: java swing netbeans jframe parameter-passing


【解决方案1】:

您可以向 MainFrame 构造函数添加一个参数,将其放置在一个字段中,使用 GUI 构建器的属性表中的自定义创建代码。

在 initComponents 的代码中插入了几个免费的代码位置。创建自定义代码就是这样的地方;

private final DefaultStyledDocument doc = new DefaultStyledDocument();

而在“自定义创建代码:”

new JTextPane(doc)

也可用于自定义面板等。

【讨论】:

    【解决方案2】:

    我在 netbeans 中的 GUI java 项目有一个大问题。众所周知,netbeans编译出来的代码是只读的,除了调用一个myInitComponents方法,和initComponents一样,在构造函数中调用,我还需要另外一种方式给initComponents方法加参数

    我认为您可能对使用 constructors 进行初始化和 setter 访问值感到困惑。

    这就像问:如果您有一个具有 a、b 和 c 等属性的类,如何创建一个设置所有属性的 setter。这是你应该避免的。您可以只为每个属性创建一个单独的 setter 和 getter,而不是尝试使用 init 来设置 all 属性。

    你应该这样做:

    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;}
    }
    

    而不是这个:

    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;
        }
    }
    

    这是一种非常糟糕的方法。有没有其他方法可以添加该参数?

    所以你问,如果你还有一个属性,说int d。我应该如何将它添加到myInit()的参数列表中。所以你已经开始看到这种方法在你的类设计中存在的问题。

    如果可能,我们会尝试在我们的设计中实现低耦合高内聚。当您在单个方法中转储各种不相关的属性时,您正在转向低内聚(一种不执行非常具体任务的方法)。

    如果您尝试使用像 myInit() 这样的单一方法并将其用作设置器来设置多个字段,则会导致许多问题。

    • 如果用户只想设置特定属性,而不是其他属性怎么办?

    所以要回答您的问题,请为每个属性使用单独的 setter,除非属性密切相关,例如:

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

    【讨论】:

      【解决方案3】:

      最后我以一种非常简单的方式解决了这个问题。我在 initComponents() 方法中插入了 DefaultStyleDocument 所需的所有代码,方法是单击 customize code 并将其添加为 pre-创建代码。

      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);
          }
      

      希望这对某人有用。

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 2011-02-27
        • 2011-08-01
        • 2013-10-14
        相关资源
        最近更新 更多