JInternalFrame 与 scala.swing

发布于 2024-11-10 03:53:53 字数 1776 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-11-17 03:53:54

问题是你不能把一个内部框架作为MainFrame的全部内容

,所以你需要设置一个JDesktopPane作为MainFrame的内容,然后使用add方法
要将内部框架添加到 JDesktopPane,

请在 MainFrame 中添加这些行

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

,将方法 createFrame 的最后一行修改为:

desktop.peer.add(newFrame.peer) 

这是一个丑陋的解决方案。需要做的是编写 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

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.

神经大条 2024-11-17 03:53:53

以下是在 Component 的同伴上定义的:

def wrap (c: JComponent): Component 

因此您编写 Component.wrap(new JInternalFrame)

The following is defined on the companion of Component:

def wrap (c: JComponent): Component 

So you write Component.wrap(new JInternalFrame).

夏天碎花小短裙 2024-11-17 03:53:53

我猜你是这样做的:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文