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