单击按钮上的scala打开框架

时间:2017-02-24 16:29:30

标签: swing 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 个答案:

答案 0 :(得分:1)

你可以做这样的事情,如下所示。在这里,我有意创建了一个新的Try2实例,而不是之前创建的obj,因为这是一个更简洁的方法。

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