2011-07-04 1 views
6

나는 scala 스윙에서 SimpleSwingApplication 특성을 가진 GUI를 만들고 있습니다. 내가하고 싶은 일은 그가 문서를 아직 저장하지 않았다면 사용자에게 (예, 아니오, 취소)를 묻는 메커니즘을 제공하는 것입니다. 사용자가 취소를 누르는 경우 Application이 닫히지 않아야합니다. 하지만 지금까지 시도한 모든 것은 MainFrame.closecloseOperation으로 작동하지 않았습니다.스왈라 스윙에서 윈도우 닫기 메커니즘을 방해하는 방법

이렇게 스칼라 스윙에서 어떻게 이루어 집니까?

스칼라 2.9입니다.

미리 감사드립니다.

답변

5

약간 다른 변화 스칼라 프레임에 WindowEvent.WINDOW_CLOSING 이벤트가 수신 될 때 수행해야 할 작업을 정의 할 수있는 기회가 주어집니다. 스칼라 프레임이 WINDOW_CLOSING 이벤트를 받으면 closeOperation을 호출하여 반응합니다. 따라서 사용자가 프레임을 닫으려고 할 때 대화 상자를 표시하려면 closeOperation을 재정의하고 원하는 동작을 구현하면 충분합니다.

+0

감사합니다. deprecations를 제거한 후에, 이것은 트릭을했습니다. – man

+0

도움이 되니 기쁩니다! –

1

내가 스칼라 스윙 정말 익숙하지 오전하지만 난 내 옛날 테스트 프로그램의 일부 코드를 발견 : 하워드가 당신을 DO_NOTHING_ON_CLOSE를 사용하여

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 _ =>() 
     } 
    } 
    } 
} 

을 제안 것과

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) 
     } 
    } 
    } 
} 
+0

잘 모르겠습니다.이 목적으로'sys.exit (0)'이 정상입니까? 처음에는 너무 무거워 보였지만 틀렸을 수도 있습니다. – Suma

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 
    } 
    } 
} 
+0

유감스럽게도 메인 윈도우가 이미 사라진 후에 만 ​​확인 대화 상자가 나타나기 때문에이 기능은 작동하지 않습니다 (적어도 메인 프레임 인 경우에는 안됨). – sambe

0

이 내가하고 싶었던 것을 수행; 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() 
    } 
}