我正在尝试使用JRuby来显示带有SWT的托盘项目。我尝试使用Windows 7,Ubuntu Unity / gnome 3 /和gnome classic。
我发现托盘物品没有出现。菜单也会显示在鼠标光标位置。
我在这里做错了什么?
=begin
# references
* usage of swt gem -- https://github.com/danlucraft/swt/blob/master/examples/menu_and_toolbar.rb
* SWT tray example -- http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java
=end
require 'java'
require_relative '../dependencies/swt/lib/swt'
class Doro
include Swt::Widgets
IMAGE = File.expand_path('../../assets/doro.jpg', __FILE__)
def initialize
ui_start
setup_tray
ui_end
end
def ui_start
display = Display.get_current
@shell = Shell.new
#@shell.text = self.class.to_s
end
def ui_end
#@shell.set_bounds(50, 50, 300, 200)
#@shell.visible = false
@shell.pack
@shell.open
end
def setup_tray
display = @shell.display
tray = display.get_system_tray
tray_item = TrayItem.new(tray, Swt::SWT::NONE)
tray_item.tool_tip_text = 'Doro'
tray_item.image = Swt::Graphics::Image.new(display, IMAGE)
tray_item.add_selection_listener { }
=begin
tray_item.add_show_listener { |event| puts 'show' }
tray_item.add_listener(Swt::SWT::Show, Swt::SWT::Listener.new { |event| puts 'show' })
tray_item.add_hide_listener { }
tray_item.add_default_selection_listener { }
=end
setup_menu tray_item
tray_item.visible = true
end
def setup_menu(parent)
menu = Swt::Widgets::Menu.new(@shell, Swt::SWT::POP_UP)
fileItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
fileItem.setText("File")
editItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
editItem.setText("Edit")
menu.visible = true
parent.add_menu_detect_listener do
menu.each { |m| m.visible = true }
end
end
def start
display = @shell.display
while !@shell.isDisposed
display.sleep unless display.read_and_dispatch
end
display.dispose
end
end
app = Doro.new
Swt::Widgets::Display.set_app_name app.class.to_s
app.start
更新:
我在Windows 7 m / c上工作时看到此代码没有任何更改。我正确地看到了图标。我想知道为什么它在家里的窗户上不起作用。 (当然永远不会在ubuntu上)。我使用相同版本的SWT jar和swt gem。
答案 0 :(得分:1)
我发现你引用TrayItem类的方式存在问题:
def setup_tray
...
# tray_item = TrayItem.new(tray, Swt::SWT::NONE) # here is the problem, replace with above
tray_item = org.eclipse.swt.widgets.TrayItem.new(tray, Swt::SWT::NONE)
此更改会使图标显示在托盘中。
要在右键单击图标时显示菜单,请修复setup_menu:
def setup_menu(parent)
menu = Swt::Widgets::Menu.new(@shell, Swt::SWT::POP_UP)
fileItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
fileItem.setText("File")
editItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
editItem.setText("Edit")
menu.visible = false # FIXED
parent.add_menu_detect_listener do
=begin # fixed debug code
items = menu.getItems
puts "this code block run every time you rightclick the icon in the systray,"
puts "your menu consists of #{items.count} items"
items.each { |m| p m.text }
=end
# items.each { |m| m.visible = true } #original code
menu.visible = true # FIXED
end
end
有关引用swt类的方法,请参阅SébastienLeCallonnec
的文章:using swt with jruby否则,按照Martin Sadler的文章:An Introduction to Desktop Apps with Ruby我找到了一个基于AWT(而不是SWT)的解决方案
进行一些编辑:
1)在jruby类中包含Java和导入awt类
class Doro
include Java
import java.awt.TrayIcon
import java.awt.Toolkit
...
2)设置菜单并在setup_tray
中设置托盘图标 def setup_tray
# Setup our menu items
file_item = java.awt.MenuItem.new("File")
edit_item = java.awt.MenuItem.new("Edit")
# Add the items to the popup menu itself
menu = java.awt.PopupMenu.new
menu.add(file_item)
menu.add(edit_item)
# Give the tray an icon and attach the popup menu to it
image = java.awt.Toolkit::default_toolkit.get_image(IMAGE)
tray_icon = TrayIcon.new(image, "Screenshot!", menu)
tray_icon.image_auto_size = true
# Finally add the tray icon to the tray
tray = java.awt.SystemTray::system_tray
tray.add(tray_icon)
end
测试:
操作系统:Microsoft Windows [版本6.1.7601]
ruby interpreter:jruby 1.6.7(ruby-1.9.2-p312)(2012-02-22 3e82bc8)(IBM J9 VM 1.6.0)[Windows Vista-amd64-java]
gem:swt(0.13)