2017-02-24 63 views
0

我正在scala中創建一個應用程序。我想打開一個按鈕點擊框架。我是斯卡拉新手。下面是代碼scala打開框架上的按鈕點擊

import scala.swing._; 
import java.io._; 
import scala.swing.event._; 
import javax.swing.ImageIcon; 

object Try1 extends SimpleSwingApplication { 
def top = new MainFrame { 
title = "First Swing App"; 

val button = new Button { 
    text = "Proceed" 
} 
contents = new BoxPanel(Orientation.Vertical) { 


    contents += button 

    border = Swing.EmptyBorder(30, 30, 30, 30) 
} 
val obj = new Try2(); 
listenTo(button) 

reactions += { 
    case ButtonClicked(button) => 
    //here 2nd frame must be open 

} 
} 

對於要打開的窗口中的代碼是這樣的

import javax.swing.ImageIcon 
import scala.swing._ 

class Try2 extends SimpleSwingApplication { 
def top = new MainFrame { 
title = "Second Swing App"; 

val button = new Button { 
    text = "Proceed" 
} 
contents = new BoxPanel(Orientation.Vertical) { 


    contents += button 

    border = Swing.EmptyBorder(30, 30, 30, 30) 
    } 
    } 
} 

我怎樣才能打開新的窗口。請幫助

回答

1

你可以做如下所示的事情。這裏故意創建一個Try2的新實例,而不是之前創建的obj,因爲這是一個更清晰的方法。

reactions += { 
    case ButtonClicked(x: Button) if x.text == "Proceed" => 
    new Try2().top.visible = true 
} 
+0

感謝您節省我的一天 –