单击按钮时如何更新面板中的groovy图像?

问题描述 投票:0回答:1

我正在尝试使用此代码在面板中显示图像,然后在单击时更新图像但不起作用(不更新图像)

得到此错误:

线程“AWT-EventQueue-0”中的异常groovy.lang.MissingPropertyException:没有这样的属性:label

我不知道如何更新。

这是我的代码:

@Bindable
class Address { 
    String street, number, city
    String toString() { "address[street=$street,number=$number,city=$city]" }
} 



def address = new Address(street: 'Evergreen Terrace', number: '742', city: 'Springfield')
   def BL = new BorderLayout()

def swing = new SwingBuilder()
def swingBuilder = new SwingBuilder()
swingBuilder.edt {  // edt method makes sure UI is build on Event Dispatch Thread.


    lookAndFeel 'nimbus'  // Simple change in look and feel.

    frame(title: 'Address', size: [1000, 800],
            show: true, locationRelativeTo: null,

            defaultCloseOperation: EXIT_ON_CLOSE) {

        borderLayout(vgap: 5)

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

                tr {
                    td {
                        label 'Street:'  // text property is default, so it is implicit.
                    }
                    td {
                        textField address.street, id: 'streetField', columns: 20
                    }
                }
                tr {
                    td {
                        label 'Number:'
                    }
                    td {
                        textField id: 'numberField', columns: 5, text: address.number
                    }
                }
                tr {
                    td {
                        label 'City:'
                    }
                    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
                }
            }


      scrollPane(id:'scroll',preferredSize: [200,200], constraints: context.CENTER) {



    panel(layout: new FlowLayout()) {


        label(icon: imageIcon(new URL('pokemon.png')))
    }


      }





      }







        panel(constraints: BorderLayout.SOUTH) {
            button text: 'Save', actionPerformed: {
                println address
label.setIcon('pokemon.png');
            }
        }

        // Binding of textfield's to address object.
        bean address,
            street: bind { streetField.text },
            number: bind { numberField.text },
            city: bind { cityField.text }
    } 
}
java swing groovy swingbuilder
1个回答
0
投票

我解决了这个问题

@Bindable
class Address { 
    String street, number, city
    String toString() { "address[street=$street,number=$number,city=$city]" }
} 



def address = new Address(street: 'Evergreen Terrace', number: '742', city: 'Springfield')
   def BL = new BorderLayout()

def swing = new SwingBuilder()
def swingBuilder = new SwingBuilder()
swingBuilder.edt {  // edt method makes sure UI is build on Event Dispatch Thread.


    lookAndFeel 'nimbus'  // Simple change in look and feel.

    frame(title: 'Address', size: [1000, 800],
            show: true, locationRelativeTo: null,

            defaultCloseOperation: EXIT_ON_CLOSE) {

        borderLayout(vgap: 5)

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

                tr {
                    td {
                        label 'Street:'  // text property is default, so it is implicit.
                    }
                    td {
                        textField address.street, id: 'streetField', columns: 20
                    }
                }
                tr {
                    td {
                        label 'Number:'
                    }
                    td {
                        textField id: 'numberField', columns: 5, text: address.number
                    }
                }
                tr {
                    td {
                        label 'City:'
                    }
                    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
                }
            }


          scrollPane(id:'scroll',preferredSize: [200,200], constraints: context.CENTER) {



        panel(layout: new FlowLayout()) {


    imagelabelx=        label(icon: imageIcon(new URL('pokemon.png')))
        }


          }





          }







            panel(constraints: BorderLayout.SOUTH) {
                button text: 'Save', actionPerformed: {
                    println address
    imagelabelx.icon= imageIcon(new URL('pokemon2.png'))
                }
            }

            // Binding of textfield's to address object.
            bean address,
                street: bind { streetField.text },
                number: bind { numberField.text },
                city: bind { cityField.text }
        } 
    }
© www.soinside.com 2019 - 2024. All rights reserved.