【问题标题】:How to interrupt the window closing mechanism in scala swing如何中断scala swing中的窗口关闭机制
【发布时间】:2011-09-28 04:33:18
【问题描述】:

我正在使用 scala swing 中的 SimpleSwingApplication 特征构建一个 GUI。 我想要做的是提供一种关闭机制,询问用户(是,否,取消)他是否还没有保存文档。如果用户点击取消,Application 不应关闭。但到目前为止,我用MainFrame.closecloseOperation 尝试的所有方法都不起作用。

那么这在 Scala Swing 中是如何完成的呢?

我使用的是 Scala 2.9。

提前致谢。

【问题讨论】:

    标签: swing scala window savefiledialog


    【解决方案1】:

    我对 scala swing 不是很熟悉,但我在一些旧的测试程序中发现了这段代码:

    object GUI extends SimpleGUIApplication {
      def top = new Frame {
        title="Test"
        peer.setDefaultCloseOperation(0)
    
        reactions += {
          case WindowClosing(_) => {
            println("Closing it?")
            val r = JOptionPane.showConfirmDialog(null, "Exit?")
            if (r == 0) sys.exit(0)
          }
        }
      }
    }
    

    【讨论】:

    • 我不确定:sys.exit(0) 是否正常用于此目的?乍一看似乎太重了,但我可能错了。
    【解决方案2】:

    与霍华德建议的略有不同

    import scala.swing._
    
    object GUI extends SimpleGUIApplication {
      def top = new Frame {
        title="Test"
    
        import javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE
        peer.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
    
        override def closeOperation() { showCloseDialog() }
    
        private def showCloseDialog() {
          Dialog.showConfirmation(parent = null,
            title = "Exit",
            message = "Are you sure you want to quit?"
          ) match {
            case Dialog.Result.Ok => exit(0)
            case _ => ()
          }
        }
      }
    }
    

    通过使用DO_NOTHING_ON_CLOSE,您有机会定义当scala 框架接收到WindowEvent.WINDOW_CLOSING 事件时应该做什么。当 Scala 框架接收到 WINDOW_CLOSING 事件时,它通过调用 closeOperation 做出反应。因此,要在用户尝试关闭框架时显示对话框,只需覆盖 closeOperation 并实现所需的行为。

    【讨论】:

    • 谢谢。删除弃用后,这就成功了。
    【解决方案3】:

    这个呢:

    import swing._
    import Dialog._
    
    object Test extends SimpleSwingApplication { 
      def top = new MainFrame { 
        contents = new Button("Hi")
    
        override def closeOperation { 
          visible = true
          if(showConfirmation(message = "exit?") == Result.Ok) super.closeOperation  
        }
      }
    }
    

    【讨论】:

    • 不幸的是,这不起作用(至少在它是 MainFrame 时不起作用),因为确认对话框仅在主窗口已经消失后出现。
    【解决方案4】:

    这就是我想做的;调用 super.closeOperation 并没有关闭框架。我会在评论中这么说,但我还不允许。

    object FrameCloseStarter extends App {
      val mainWindow = new MainWindow()
      mainWindow.main(args)
    }
    
    class MainWindow extends SimpleSwingApplication {
      def top = new MainFrame {
        title = "MainFrame"
        preferredSize = new Dimension(500, 500)
        pack()
        open()
    
        def frame = new Frame {
          title = "Frame"
          preferredSize = new Dimension(500, 500)
          location = new Point(500,500)
          pack()
    
          peer.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
    
          override def closeOperation() = {
            println("Closing")
            if (Dialog.showConfirmation(message = "exit?") == Result.Ok) {
              peer.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
              close()
            }
          }
        }
        frame.open()
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2011-09-19
      • 1970-01-01
      相关资源
      最近更新 更多