73rd: Scala interface and event handling programming advanced combat

Source: Internet
Author: User

Today, I learned the 73rd lecture of Scala programming by Liaoliang, mainly the use of file selectors. Let's go through the code to try it out for yourself.

Import Scala.swing._
Import Java.io.File
Import scala.swing.event.ButtonClicked
Import Scala.swing.Label

Object Gui_event extends simpleswingapplication{
Val filechooser = new Filechooser (New File (".")) Define file selector, path to local file
filechooser.title= "File Chooser"

Val button = new button{
Text= "Choose a File from local"

}

Val Label =new label{
text= "No any files selected yet."
}

Val Mainpanel = new flowpanel{//define the main frame
Contents + = button
Contents + = Label
}

def top = new mainframe{
title = "Scala GUI programming advanced!"
Contents = Mainpanel

Listento (button)//Listen to button
Reactions + = {
Case ButtonClicked (b) =>{
Val result = Filechooser.showopendialog (mainpanel)//File Selector
if (result==filechooser.result.approve) {
Label.text=filechooser.selectedfile.getpath ()
}
}
}



}

OK, the main frame, buttons and labels are not spoken, as mentioned earlier. So let's take a look at the file selector primarily.

Val filechooser = new Filechooser (New File ("."))

This defines a file selector, which, like a button, is a component.

When we listen to the button, the file selector jumps out by clicking on the trigger condition.

Case ButtonClicked (b) =>{
Val result = Filechooser.showopendialog (Mainpanel)

We select the file through the file selector, and after the selection is complete, change the label text to show the path of the selected file.

if (result==filechooser.result.approve) {
Label.text=filechooser.selectedfile.getpath ()

Information from DT Big Data Dream Factory public account: Dt_spark

Follow the account to learn more about the Liaoliang Teacher's course content

Teacher Wang qq:1740415547
No.: 18610086859


73rd: Scala interface and event handling programming advanced combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.