GUI interface Programming Summary __ Programming

Source: Internet
Author: User
Tags event listener gettext save file



GUI (graphical User Interface) programming summary of Graphical interface

Java.Awt:Abstract Windows ToolKit (Abstract Window Toolkit)

Javax.swing:

Basic setup of graphical interface:

Set the form size: setSize (long, wide);

Set the form position: setlocation (distance from left, distance); SetBounds (long, wide, distance left, distance);

Set layout: setlayout (New FlowLayout ());

Make form visible: setvisible (True);

Event Listener mechanism:

The characteristics of the event monitoring mechanism:

1, the event source.

2, the event.

3, listener.

4, event handling.

Event Source: These are the graphical interface components in the AWT package or swing package.

Events: Each event source has its own unique corresponding event and common events.

Listener: An action that can trigger an event (more than one action) is encapsulated in the listener.

All three of them are already defined in Java.

It's OK to get the object directly.

The thing we're going to do is to deal with the resulting action.

Eg: Write a program to practice graphical interface programming.

Import java.awt.*;

Import java.awt.event.*;

Import java.io.*;

Class Mywindowdemo

{

Private Frame F;

Private TextField TF;

Private Button but;

Private TextArea ta;

Private Dialog D;

Private Label Lab;

Private Button okbut;

Mywindowdemo ()

{

Init ();

}

public void Init ()

{

f = new Frame ("my Window");

F.setbounds (300,100,600,500);

F.setlayout (New FlowLayout ());

tf = new TextField (60);

but = New button ("Go");

Ta = new TextArea (25,70);

D = new Dialog (f, "hint info-self", true);

D.setbounds (400,200,240,150);

D.setlayout (New FlowLayout ());

Lab = new Label ();

okbut = New button ("OK");

D.add (Lab);

D.add (okbut);

F.add (TF);

F.add (But);

F.add (TA);

MyEvent ();

F.setvisible (TRUE);

}

private void MyEvent ()

{

Okbut.addactionlistener (New ActionListener ()

{

public void actionperformed (ActionEvent e)

{

D.setvisible (FALSE);

}

});

D.addwindowlistener (New Windowadapter ()

{

public void windowclosing (WindowEvent e)

{

D.setvisible (FALSE);

}

});

Tf.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 ();

}

});

F.addwindowlistener (New Windowadapter ()

{

public void windowclosing (WindowEvent e)

{

System.exit (0);

}

});

}

private void Showdir ()

{

String Dirpath = Tf.gettext ();

File dir = new file (Dirpath);

if (dir.exists () && dir.isdirectory ())

{

Ta.settext ("");

string[] names = Dir.list ();

for (String name:names)

{

Ta.append (name+ "\ r \ n");

}

}

Else

{

String info = "The information you entered:" +dirpath+ "is wrong." Please lose again ";

Lab.settext (info);

D.setvisible (TRUE);

}

}

public static void Main (string[] args)

{

New Mywindowdemo ();

}

}

Menu:

MenuBar menu as a whole; menus are contained in MenuBar; MenuItem is included in menu.

The above 3 relationships are added with add ();

Put the menubar into the frame with setmenubar ();

Filedialog.load: Open mode

Filedialog.save: Saved mode

Practice: A simple Notepad

/**

Write a little program about Notepad.

@author Tian Jian

@version v1.1

*/

Import java.awt.*;

Import java.awt.event.*;

Import java.io.*;

Class Mymenudemo

{

Private Frame F;

Private TextArea ta;

Private MenuBar MB;

Private Menu me;

Private MenuItem minew,misave,miload;

private file file;

Private FileDialog Opendia,savedia;

Mymenudemo ()

{

Init ();

}

public void Init ()

{

F=new Frame ("Tian Jian-Notepad");

F.setbounds (300,100,650,600);

Ta=new TextArea ();

Ta.setbounds (302,105,400,300);

F.setlayout (New FlowLayout ())//Why setting the text area after streaming layout has no effect

Mb=new MenuBar ();

Me=new Menu ("File (F)");

Minew=new MenuItem ("new");

Misave=new MenuItem ("Save (S)");

Miload=new MenuItem ("Open (O)");

Mb.add (Me);

Me.add (minew);

Me.add (Miload);

Me.add (Misave);

F.setmenubar (MB);

F.add (TA);

Opendia=new FileDialog (F, "Open File", filedialog.load);

Savedia=new FileDialog (F, "Save File", Filedialog.save);

MyEvent ();

F.setvisible (TRUE);

}

public void MyEvent ()

{

F.addwindowlistener (New Windowadapter ()

{

public void windowclosing (WindowEvent e)

{

System.exit (0);

}

});

Miload.addactionlistener (New ActionListener ()

{

public void actionperformed (ActionEvent e)

{

Opendia.setvisible (TRUE);

String dirpath=opendia.getdirectory ();

String Filename=opendia.getfile ();

if (dirpath==null| | Filename==null)

Return

Ta.settext ("");

File=new File (dirpath,filename);

Try

{

BufferedReader bufr=new BufferedReader (new FileReader (file));

String Line=null;

while ((Line=bufr.readline ())!=null)

{

Ta.append (line+ "\ r \ n");

}

Bufr.close ();

}

catch (IOException ex)

{

throw new RuntimeException ("read failed");

}

}

});

Misave.addactionlistener (New ActionListener ()

{

public void actionperformed (ActionEvent e)

{

if (file==null)

{

Savedia.setvisible (TRUE);

String dirpath=savedia.getdirectory ();

String Filename=savedia.getfile ();

if (dirpath==null| | Filename==null)

Return

File=new File (dirpath,filename);

}

Try

{

BufferedWriter bufw=new BufferedWriter (new FileWriter (file));

String Text=ta.gettext ();

Bufw.write (text);

Bufw.close ();

}

catch (IOException ex)

{

throw new RuntimeException ("");

}

}

});

}

public static void Main (string[] args)

{

New Mymenudemo ();

}

}

The process of playing jar packs:

1, compile package JAR–CVF jar Name Package name

2, write a file Main-class: Package name. class Name "Add a

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.