Java Instance Small item: Lists the disk directories.
Import java.awt.*;
Import java.awt.event.*;
Import Java.io.File;
Class mywinddemo{
Define a dialog box
Private Dialog D;
Define a label
Private Label Lab;
Define a button on a dialog box
Private Button But_dlg;
Define a form
Private Frame F;
Define a text box
Private TextField TF;
Define a button
Private Button But,but_close;
Define a text area
Private TextArea ta;
Defines a form constructor. 、
Mywinddemo ()
{
Init ();
}
Define a UI interface setting method
public void Init ()
{
Instantiate a dialog box to set basic information for a dialog box
If the argument is true, the dialog box exists that cannot manipulate the form, which is false instead.
D=new Dialog (F, "hint message-self", true);
D.setbounds (400, 200,240,150);
D.setlayout (New FlowLayout ());
Instantiate a label
Lab=new Label ();
Instantiate button
But_dlg=new button ("OK");
Instantiating a form
F=new Frame ("my Window");
Set the size and position of a form
F.setbounds (300,100,600,500);
Set how the form is laid out
F.setlayout (New FlowLayout ());
Set the number of columns in a text box
Tf=new TextField (60);
Set button name
But=new button ("Go to");
But_close=new button ("Exit program");
Set the rows and columns of a text area
Ta=new TextArea (25,70);
All the components are added to the form, and the dialog box exists only when needed and does not need to be added to the form.
F.add (TF);
F.add (But);
F.add (TA);
F.add (But_close);
D.add (Lab);
D.add (BUT_DLG);
Add an event to a form
MyEvent1 ();
Set form visible
F.setvisible (TRUE);
}
To create an event listener method
private void Showdir ()
{
Defines a string variable that receives a directory of files.
String Dirpath=tf.gettext ();
Defining File Objects
File dir =new file (Dirpath);
Get file directory
if (Dir.exists () &&dir.isdirectory ())
{
Ta.settext ("");
All the directories in the list are stored in a string.
String names[]=dir.list ();
Traverse the names to save it in the text area.
/*
* void append (String str)
Appends the given text to the current text in the text area.
*/
for (String name:names)
Ta.append (name+ "\ r \ n");
}
else{
String info= "The information you entered" +dirpath+ "wrong, please re-lose";
Lab.settext (info);
D.setvisible (TRUE);
}
/*//text Box calls the GetText method to get text information entered by the keyboard
String Text=tf.gettext ();
Text area call SetText method to get text information
Ta.settext (text);
System.out.println (text); */
Empty the text message inside the textfile.
Tf.settext ("");
}
Defining the MyEvent1 method to add listeners
private void MyEvent1 ()
{
D.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e)
{
Do not show dialog box
D.setvisible (FALSE);
}
});
Exit the program, close the window, call the Addwindowlistener method, pass the Windowadapter object (listener adapter).
F.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e)
{
System.exit (0);
}
});
Add time listening to the But_close method to realize the function of exiting the program.
But_close.addactionlistener (new ActionListener () {
Call the addActionListener (Active listener) method to pass the ActionListener (activity listeners) object
The Actionperformed method, which realizes the function inside.
public void actionperformed (ActionEvent e)
{
System.exit (0);
}
});
Add a listener to But_dlg
But_dlg.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated method stubs
D.setvisible (FALSE);
}
});
Tf.addkeylistener (New Keyadapter ()
{
public void keypressed (KeyEvent e)
{
if (E.getkeycode () ==keyevent.vk_enter)
Showdir ();
}
}
);
But_dlg.addkeylistener (New Keyadapter ()
{
public void keypressed (KeyEvent e)
{
if (E.getkeycode () ==keyevent.vk_enter)
Showdir ();
}
}
);
But.addactionlistener (New ActionListener ()
{
public void actionperformed (ActionEvent e)
{
Showdir ();
}
});
}
}
public class MyWindowDemo1 {
public static void Main (string[] args) {
New Mywinddemo ();
}
}
//
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java GUI (Instance small Item--list disk directory)