简体   繁体   English

Jython 使用 swing JComboBoxes 如何创建 actionPerformed 处理程序

[英]Jython using swing JComboBoxes how to create an actionPerformed handler

I am not that proficient in Java.我对 Java 不是很精通。 I need to build a UI from a runtime script in Jython.我需要从 Jython 中的运行时脚本构建 UI。 The UI is a date picker. UI 是一个日期选择器。 For backwards hardware compatibility I am forced to use JDK 1.4 as Source/Binary format, while having JDK 1.8 as platform.为了向后硬件兼容性,我被迫使用 JDK 1.4 作为源代码/二进制格式,同时使用 JDK 1.8 作为平台。 The rest of the GUI wirks fine. GUI 的 rest 运行良好。

the date picker looks like this:日期选择器如下所示:

在此处输入图像描述

all the controls, including the panel container, are built at runtime.所有控件,包括面板容器,都是在运行时构建的。 And they are all wrapped in classes like:它们都包含在以下类中:

class DP_Combo(JComboBox):

    def __init__(self, kwargv={}):
        
        self.setModel(jaxswing.DefaultComboBoxModel(kwargv['items']))
        self.setBounds(kwargv['bounds'][0], kwargv['bounds'][1], \
                        kwargv['bounds'][2], kwargv['bounds'][3])
        self.setFont(Font("Dialog", 1, 18))

for the ComboBoxes.对于组合框。

also the DatePicker itself is a class called from another script: DatePicker 本身也是从另一个脚本调用的 class :

class DatePicker:

    def __init__(self, hiddenTabbedPane, argDate=time.strftime('%d.%m.%Y'), argSplitter="."):
       # blah bla

in the DatePicker class the ComboBoxes instances are created like this:在 DatePicker class 中,组合框实例的创建方式如下:

def __build_combos(self):
    _months = {'items': MONTHS, 'bounds': MONTHS_BOUNDS}
    _years = {'items': YEARS, 'bounds': YEARS_BOUNDS}
    self.months_list = DP_Combo(_months)
    self.years_list = DP_Combo(_years)
    # initialize combos:
    self.months_list.setSelectedIndex(self.curMonth - 1)
    self.years_list.setSelectedIndex(indexOf(self.curYear, YEARS))
    self.newPane.add(self.months_list)
    self.newPane.add(self.years_list)

Note: the items in all caps are list of months and years obv.注意:所有大写的项目是月份和年份的列表。

All good so far.到目前为止一切都很好。 Now I need to create the handlers to capture the events from the controls and assign them to the controls themselves.现在我需要创建处理程序来捕获控件中的事件并将它们分配给控件本身。

for the ComboBoxes as example, I know from java:以 ComboBoxes 为例,我从 java 知道:

// initializing the frame
private void initComponents() {
    // itin vars and create components - widgets       
   jComboBoxMonths = new javax.swing.JComboBox();
   // setup jComboBoxMonths 
   // ...
   jComboBoxMonths.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBoxMonths_ActionPerformed(evt);
        }
    });
}

then at frame class level the event handler:然后在帧 class 级别的事件处理程序:

private void jComboBox3_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // do stuff
}                                           

I am struggling to translate the above into jython, and also not sure whether the handlers can be placed inside the DatePicker class or need to be one level up.我正在努力将上述内容转换为 jython,并且不确定处理程序是否可以放置在 DatePicker class 内或需要上一级。

Help please and thanx in advance for the patience请提前帮助并感谢您的耐心

I don't know if this is the cleaner way, but it works.我不知道这是否是更清洁的方式,但它有效。

first I had to create inner (nested) classes to grab the events, in this example the actionPerfomed event is captured and passed to the handler by myActionListener :首先我必须创建内部(嵌套)类来获取事件,在本例中,actionPerfomed 事件被捕获并由myActionListener传递给处理程序:

from java.awt import event

class DatePicker:

    def __init__(self, kwargv):
    # blah blah
    # initialize controls


    class myActionListener(event.ActionListener):

        def __init__(self, handler):
            self.handler = handler

        def actionPerformed(self, evt):
            self.handler(evt)

and when creating the combo box (DatePicker level) add an instance of the listener:并在创建组合框(DatePicker 级别)时添加侦听器的实例:

class DatePicker:
    # ...
    # init and stuff

    def crate_years_combo(self):
        self.years_combo = DP_Combo(_years)
        listener = self.myActionListener(self.years_change_evt)
        self.years_combo.addActionListener(listener)

finally (again at DatePicker level) define the method to handle the event actionPerformed (the years_change_evt ):最后(再次在 DatePicker 级别)定义处理事件actionPerformed的方法( years_change_evt ):

class DatePicker:
    # ...
    # init and stuff

    def years_change_evt(self, evt):
        # do stuff

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

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