JInternalFrame 与 scala.swing
scala swing 看起来很有趣,但不知何故它不完整,有时我仍然需要使用旧的 java 类,但我不知道如何正确包装它们。
那么如何正确包装 javax.swing.JInternalFrame 以便我可以将其用作 MainFrame 中的组件?
我尝试让 this 示例与 scala 和 scala 一起使用swing 库,我终于设法获得了一个内部框架,但是我的 MainFrame 扭曲了所有内部框架并拉伸它们,直到它们的宽度和高度与 MainFrame 内部的空间完全相同。
这是我当前的实现:
import swing._
import event._
object InternalFrameDemo extends SimpleSwingApplication{
val top = new MainFrame{
title = "InternalFrameDemo"
preferredSize = new Dimension(640,480)
val menuNew = new MenuItem("New"){
mnemonic = Key.N
action = new Action("new"){
def apply(){
createFrame
}
}
}
val menuQuit = new MenuItem("Quit"){
mnemonic = Key.Q
action = new Action("quit"){
def apply(){
quit()
}
}
}
menuBar = new MenuBar{
contents += new Menu("Document"){
mnemonic = Key.D
contents ++= Seq(menuNew,menuQuit)
}
}
def createFrame{
val newFrame = MyInternalFrame()
newFrame.visible = true
contents = newFrame
}
}
}
object MyInternalFrame{
var openFrameCount = 0;
val xOffset, yOffset = 30;
def apply() = {
openFrameCount += 1
val jframe = new javax.swing.JInternalFrame("Document #" + openFrameCount,true,true,true,true)
jframe.setSize(300,300)
jframe.setLocation(xOffset*openFrameCount,yOffset*openFrameCount)
Component.wrap(jframe)
}
}
scala swing looks interesting, but somehow it is incomplete, and sometimes I still need to use the old java classes, but I have no clue how I have to wrap them correctly.
So how do I wrap javax.swing.JInternalFrame correctly so that I can use it as Component in my MainFrame?
I try to get this example to work with scala and the scala swing library, and I finally managed to get an Internal Frame, but my MainFrame distorts all internal Frames and stretches them until they have exactly the same width and height as the space inside the MainFrame.
This is my current implementation:
import swing._
import event._
object InternalFrameDemo extends SimpleSwingApplication{
val top = new MainFrame{
title = "InternalFrameDemo"
preferredSize = new Dimension(640,480)
val menuNew = new MenuItem("New"){
mnemonic = Key.N
action = new Action("new"){
def apply(){
createFrame
}
}
}
val menuQuit = new MenuItem("Quit"){
mnemonic = Key.Q
action = new Action("quit"){
def apply(){
quit()
}
}
}
menuBar = new MenuBar{
contents += new Menu("Document"){
mnemonic = Key.D
contents ++= Seq(menuNew,menuQuit)
}
}
def createFrame{
val newFrame = MyInternalFrame()
newFrame.visible = true
contents = newFrame
}
}
}
object MyInternalFrame{
var openFrameCount = 0;
val xOffset, yOffset = 30;
def apply() = {
openFrameCount += 1
val jframe = new javax.swing.JInternalFrame("Document #" + openFrameCount,true,true,true,true)
jframe.setSize(300,300)
jframe.setLocation(xOffset*openFrameCount,yOffset*openFrameCount)
Component.wrap(jframe)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是你不能把一个内部框架作为MainFrame的全部内容
,所以你需要设置一个JDesktopPane作为MainFrame的内容,然后使用add方法
要将内部框架添加到 JDesktopPane,
请在 MainFrame 中添加这些行
,将方法 createFrame 的最后一行修改为:
这是一个丑陋的解决方案。需要做的是编写 JDesktopPane 和 JInternalFrame 的简单包装器
,或者在 InteractiveMesh.org 上找到更好的解决方案,检查他们的 ImScalaSwing API,其中包含 JInternalFrame 包装器。在使用之前请务必阅读他们的一些示例代码。
the problem is that you can't put an internal frame as the entire contents of MainFrame
so you need to set a JDesktopPane as the contents of MainFrame, then use the add method
to add internal frame to the JDesktopPane
add these lines in your MainFrame
amend your last line of method createFrame to this:
this is an ugly solution. what need to be done is write a simple wrapper of JDesktopPane and JInternalFrame
or, a better solution at InteractiveMesh.org, check their ImScalaSwing API, which contain the JInternalFrame wrapper. Be sure to read some of their example code before you use it.
以下是在
Component
的同伴上定义的:因此您编写
Component.wrap(new JInternalFrame)
。The following is defined on the companion of
Component
:So you write
Component.wrap(new JInternalFrame)
.我猜你是这样做的:
I guess you do it like this: