Specifying a frame icon on Scala’s Swing Frame

六月ゝ 毕业季﹏ 提交于 2019-12-13 05:53:04

问题


How can you set a new frame icon on Scala’s scala.swing.Frame class? What are the intentions behind Frame.iconImage: Image and Frame.iconify()? I can’t figure out, what they’re doing.

Here’s my last attempt

import scala.swing.Frame

class MyFrame extends Frame {
  iconImage = toolkit.getImage("src/main/resources/icon.png")
  visible = true
}

I also tried several other methods, but nothing worked.


回答1:


I'm guessing you are on OS X. Sadly, the icon decoration does not work for the OS X look and feel, neither does it work for Nimbus look and feel which seems to not come with a specific window decoration (uses title bar from OS X).

So you will need a look and feel that does paint its own window title bar:

import scala.swing._
import javax.swing._
UIManager.setLookAndFeel(new plaf.metal.MetalLookAndFeel)
JFrame.setDefaultLookAndFeelDecorated(true)

val f = new Frame {
   iconImage = toolkit.getImage(new java.net.URL(
      "http://www.scala-lang.org/sites/default/files/favicon.gif"))
   size = new Dimension(200, 200)
   visible = true
}

The only chance with OS X window title bars is if you want to decorate with the default icon used for a particular file.

Look for Window.documentFile here: http://developer.apple.com/library/mac/#technotes/tn2007/tn2196.html#//apple_ref/doc/uid/DTS10004439




回答2:


What you have there should work, but if the getImage can't find the file, it fails silently. Since you have a relative path, it's probably because you code isn't executing in the directory you intended.

On Ubuntu this should highlight the difference. I get one window with a smiley icon and one with the standard java icon.

new Frame() {
  iconImage = toolkit.getImage("/usr/share/icons/gnome/16x16/emotes/stock_smiley-10.png")
  size = new Dimension(200, 200)
  visible = true
}
new Frame() {
  iconImage = toolkit.getImage("xxx")
  size = new Dimension(200, 200)
  visible = true
}


来源:https://stackoverflow.com/questions/11308277/specifying-a-frame-icon-on-scala-s-swing-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!