0

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

JFrame初始化方法

def initialize *args
    super("Attendance")
    self.setDefaultCloseOperation(javax.swing.JFrame::DO_NOTHING_ON_CLOSE)
    self.add_window_listener(CloseHelper.new)
    self.minimum_width  = FRAME_WIDTH
    self.minimum_height = FRAME_HEIGHT
    base_font = Font.new("Lucida Grande", 0, 14)
    set_up_components
    self.set_visible true
    self.pack
end

WindowAdapter 类

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
4

1 回答 1

0

我不确定是否正确,但使用JRuby 1.7.4+对我有用JDK 1.7.0_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
于 2013-09-16T06:47:39.343 回答