Jruby Swing覆盖JFrame关闭事件

Jruby Swing覆盖JFrame关闭事件,swing,jruby,Swing,Jruby,使用Jruby,在JFrame中覆盖默认关闭操作的正确方法是什么? 我尝试了以下方法,但是只有单击“窗口装饰关闭”按钮才能关闭窗口 JFrame初始化方法 WindowAdapter类 class CloseHelper

使用Jruby,在JFrame中覆盖默认关闭操作的正确方法是什么? 我尝试了以下方法,但是只有单击“窗口装饰关闭”按钮才能关闭窗口

JFrame初始化方法 WindowAdapter类
class CloseHelper
我不确定该方法是否正确,但使用JRuby 1.7.4+
JDK 1.7.0\u 40
对我有效:

include Java
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.WindowConstants

def makeUI
  p = JPanel.new
  return p
end
def run
  f = JFrame.new "Attendance"
  f.default_close_operation = WindowConstants::DO_NOTHING_ON_CLOSE
  f.add_window_listener {|e|
    if e.getID == WindowEvent::WINDOW_CLOSING
      puts e
      cdate = false; #correct_date(signouttime,nil)
      if cdate
        #AttendanceModel.fetch("...",signouttime,signouttime).update
        #showerror("Message","Signed out all visitors",nil,nil)
        #XXX continue?
      else
        f.dispose
      end
    end
  }
  f.add makeUI
  f.set_size 320, 240
  f.location_relative_to = nil
  f.visible = true
end
java.awt.EventQueue.invokeLater self
class CloseHelper < WindowAdapter
   def window_closing event
            signouttime=DateTime.now
            cdate=correct_date(signouttime,nil)
      if cdate
        AttendanceModel.fetch("update visitors set signedout=? where datediff(day,signedin,?)=0 and signedout is null",signouttime,signouttime).update
        showerror("Message","Signed out all visitors",nil,nil)
      end
       java.lang.System.exit(0)
      end
end
include Java
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.WindowConstants

def makeUI
  p = JPanel.new
  return p
end
def run
  f = JFrame.new "Attendance"
  f.default_close_operation = WindowConstants::DO_NOTHING_ON_CLOSE
  f.add_window_listener {|e|
    if e.getID == WindowEvent::WINDOW_CLOSING
      puts e
      cdate = false; #correct_date(signouttime,nil)
      if cdate
        #AttendanceModel.fetch("...",signouttime,signouttime).update
        #showerror("Message","Signed out all visitors",nil,nil)
        #XXX continue?
      else
        f.dispose
      end
    end
  }
  f.add makeUI
  f.set_size 320, 240
  f.location_relative_to = nil
  f.visible = true
end
java.awt.EventQueue.invokeLater self