如何在groovy swingbuilder面板中获取所有组件名称?

时间:2018-03-02 15:26:33

标签: java swing groovy swingbuilder

我在单击groovy面板中的按钮

时尝试获取所有组件名称

但我的命令没有得到所有组件;我如何在

中更正此代码

常规

我不知道如何得到所有

请帮我解决这个问题;

我使用getcomponent()命令但没有获得我面板的所有组件

我需要获得compoentn date和dat3e

import groovy.swing.SwingBuilder 
import groovy.beans.Bindable 
import static javax.swing.JFrame.EXIT_ON_CLOSE 
import java.awt.*
import groovy.transform.Field


    def swingBuilder = new SwingBuilder()
    swingBuilder.edt {  
        frame(title: 'Address',   id:"allx" ,size: [350, 230],
                show: true, locationRelativeTo: null,
                defaultCloseOperation: EXIT_ON_CLOSE) {
            borderLayout(vgap: 5)

            panel(constraints: BorderLayout.CENTER,   id:"allx2" ,
                    border: compoundBorder([emptyBorder(10), titledBorder('Enter your address:')])) {
                tableLayout {
    tr{ textField  id:"allx3"

      td {       textField  id:"date",text="01-01-18", columns: 10     } 

    }

    tr{  

      td {       textField  id:"dat3e",text="01-01-18", columns: 10     } 

    }
    }

     }

   panel(constraints: BorderLayout.SOUTH) {
            button text: 'Save', actionPerformed: {


    Component[] components = allx2.getComponents();
println  components
    for (int i = 0; i < components.length; i++) {

        if(components[i].getClass().getName().toString().equals("javax.swing.JTextField")){
            components[i].setEnabled(false);
println    components[i]
        }
    }



            }
        }



       } 
    }

1 个答案:

答案 0 :(得分:0)

我用这个解决了

import groovy.swing.SwingBuilder 
import groovy.beans.Bindable 
import static javax.swing.JFrame.EXIT_ON_CLOSE 
import java.awt.*
import groovy.transform.Field


    def swingBuilder = new SwingBuilder()
    swingBuilder.edt {  
        frame(title: 'Address',   id:"allx" ,size: [350, 230],
                show: true, locationRelativeTo: null,
                defaultCloseOperation: EXIT_ON_CLOSE) {
            borderLayout(vgap: 5)

            panel(constraints: BorderLayout.CENTER,   id:"allx2" ,
                    border: compoundBorder([emptyBorder(10), titledBorder('Enter your address:')])) {
                tableLayout( id:"allxgg") {
    tr{  

      td {       textField  id:"date",text="01-01-18", columns: 10     } 

    }

    tr{  

      td {       textField  id:"dat3e",text="01-01-18", columns: 10     } 

    }


  tr{ 

      td {       textField  id:"dateg",text="01-01-18", columns: 10     } 

    }

    tr{  

      td {       textField  id:"dat3eg",text="01-01-18", columns: 10     } 

    }

    }

     }

   panel(constraints: BorderLayout.SOUTH) {
            button text: 'Save', actionPerformed: {


    Component[] components = allxgg.getComponents();
println components.size()
println  components[3].getText()





            }
        }



       } 
    }