开发者

JInternalFrame with scala.swing

开发者 https://www.devze.com 2023-03-08 13:08 出处:网络
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.

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.setLocati开发者_Python百科on(xOffset*openFrameCount,yOffset*openFrameCount)

        Component.wrap(jframe)
    }
}


The following is defined on the companion of Component:

def wrap (c: JComponent): Component 

So you write Component.wrap(new JInternalFrame).


I guess you do it like this:

scala> import swing.Component
import swing.Component

scala> import javax.swing.JInternalFrame
import javax.swing.JInternalFrame

scala> class InternalFrame extends Component {
     | override lazy val peer = new JInternalFrame
     | }
defined class InternalFrame


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

val desktop = Component.wrap(new javax.swing.JDesktopPane())
contents = desktop

amend your last line of method createFrame to this:

desktop.peer.add(newFrame.peer) 

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消