如何使用swingbuilder在groovy中使用jxdatepicker?

时间:2018-02-07 20:23:26

标签: java swing groovy

我尝试使用此代码创建一个使用swingx和groovy

的日期选择器

但我不知道,因为不工作

我试图在第一个标签中显示日期选择器,但没有显示任何内容

我不知道出了什么问题

请帮我解决方案

这是我使用swingx和javax的最终代码IM

并使用swingbuilder请查看此代码

并帮助我

这是不一样的问题我试图使用wingx jxdatepicker而我的代码不起作用 请查看此部分

picker.setFormats(new SimpleDateFormat(“dd.MM.yyyy”)); tableLayout {    picker.show()

import groovy.swing.SwingBuilder 
import groovy.beans.Bindable 
 import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import static javax.swing.JFrame.EXIT_ON_CLOSE 
@Grapes(
    @Grab(group='org.swinglabs', module='swingx', version='1.6')
)

import org.jdesktop.swingx.JXDatePicker;
import java.awt.*

@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 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: [350, 230],
            show: true, locationRelativeTo: null,
            defaultCloseOperation: EXIT_ON_CLOSE) {
        borderLayout(vgap: 5)

        panel(constraints: BorderLayout.CENTER,

                border: compoundBorder([emptyBorder(10), titledBorder('Enter your address:')])) {
        JXDatePicker picker = new JXDatePicker();
        picker.setDate(Calendar.getInstance().getTime());
        picker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));

            tableLayout {
       picker.show()
                tr {
                    td {
                             picker
                    }
                    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
                    }
                }
            }

        }

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

        // Binding of textfield's to address object.
        bean address,
            street: bind { streetField.text },
            number: bind { numberField.text },
            city: bind { cityField.text }
    } 
}

1 个答案:

答案 0 :(得分:0)

如果您正确注册JXDatePicker,您的示例应该有效,缺失的行可能如下所示:     registerBeanFactory(“datePicker”,JXDatePicker) 而且你不需要实例化JXDatePicker小部件,你也不需要显式地显示它,SwingBuilder应该照顾它,这个例子对我有用:

import groovy.swing.SwingBuilder 
import groovy.beans.Bindable 
import java.text.SimpleDateFormat
import java.util.Calendar
import javax.swing.JFrame
import javax.swing.JPanel
import static javax.swing.JFrame.EXIT_ON_CLOSE 
@Grapes(
    @Grab(group='org.swinglabs', module='swingx', version='1.6')
)

import org.jdesktop.swingx.JXDatePicker
import java.awt.*

@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 swingBuilder = new SwingBuilder()
swingBuilder.edt {  // edt method makes sure UI is build on Event Dispatch Thread.
    registerBeanFactory("datePicker", JXDatePicker)
    lookAndFeel 'nimbus'  // Simple change in look and feel.
    frame(title: 'Address', size: [350, 230],
            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 'Birth Date:'
                    }
                    td {
                        datePicker(date: new Date(), formats: new SimpleDateFormat("dd.MM.yyyy"))
                    }
                }
                tr {                
                    td {
                        label 'Street:'
                    }
                    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
                    }
                }
            }

        }

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

        // Binding of textfield's to address object.
        bean address,
            street: bind { streetField.text },
            number: bind { numberField.text },
            city: bind { cityField.text }
    } 
}

然后您应该能够将日期选择器的值绑定到bean地址,有关详细信息,请查看:Building Rich Applications with Groovy's SwingBuilder, TS-5098, JavaOne 2008

希望这有帮助。