Dark Horse programmer-java basics ------ graphical interface (GUI)

Source: Internet
Author: User

Two commonly used packages involved: Java: Awt and javax: Swing

Organization diagram:

The difference is as follows:

     FrameWhen a user tries to close a window,JFrameKnow how to respond. When you close the window, the default behavior is to simply hide the JFrame. You can call this method to change the default behavior.  

        setDefaultCloseOperation(int).

Operation

 Create a graphical interface:

1. Create a frame form.

Frame f = new Frame ("my awt ");

2. Perform basic settings on the form. Or you can directly setF. setBounds (300,100,600,500)The four parameters are the form length and width. The distance between the vertex on the left and the distance from the top.

F. setSize (500,400 );

F. setLocation (300,200 );

F. setLayout (new FlowLayout (); // sets the layout mode to stream layout.

3. Define components.

Button B = new Button ("Button ");

4. add the component to the form through the add method of the form.

F. add (B );

F.AddWindowListener(NewWindowAdapter() {// Define the listener and Event Processing

Public void windowClosing (WindowEventE ){

System. out. println (" ");

System. exit (0 );

}

Public void windowActivated (WindowEventE ){

System. out. println ("I am alive. ");

}

Public void windowOpened (WindowEventE ){

System. out. println ("I was opened, hahahhahah ");}

}

);

5. display the form using setVisible (true)

F.SetVisible (true ); 

  Features of the event listening mechanism:1. Event source. 2. Events. 3. listener. 4. event processing.

Event Source: The graphic interface components in the awt package or swing package.

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

Listener: The action that can trigger an event (more than one action) has been encapsulated into the listener.

What we need to do is to process the generated actions. (The first three items have been defined in java. You can directly obtain the object to use .)

Import java. awt .*;

Import java. awt. event .*;

Class MouseAndKeyEvent {

Private Frame f;

Private Button;

Private TextField tf;

MouseAndKeyEvent (){

Init ();

}

Public void init (){

F = new Frame ("my frame ");

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

F. setLayout (new FlowLayout ());

Tf = new TextField (20 );

But = new Button ("my button ");

F. add (tf );

F. add ();

MyEvent ();

F. setVisible (true );

}

Private void myEvent (){

F. addWindowListener (new WindowAdapter (){

Public void windowClosing (WindowEvent e ){

System. exit (0 );

}

});

Tf. addKeyListener (new KeyAdapter (){

Public void keyPressed (KeyEvent e ){

Int code = e.GetKeyCode ();Constant value obtained by pressing a key

         If (! (Code> = KeyEvent. VK_0 & code <= KeyEvent. VK_9 )){

System. out. println (code + "... illegal ");

            E. consume ();This method is the default solution.Cannot be triggered normally

}

}

});

// Add a keyboard listener to.

But. addKeyListener (new KeyAdapter (){

Public void keyPressed (KeyEvent e ){

         If (e. isControlDown () & e. getKeyCode () = KeyEvent. VK_ENTER)KeyEvent. VK_ENTER: Constant

// System. exit (0 );

System. out. println ("ctrl + enter is run ");

// System. out. println (KeyEvent. getKeyText (e. getKeyCode () + "..." + e. getKeyCode ());

}

});

But. addMouseListener (new MouseAdapter (){

Private int count = 1;

Private int clickCount = 1;

Public void mouseEntered (MouseEvent e ){

System. out. println ("move the mouse to this component" + count ++ );

}

Public void mouseClicked (MouseEvent e ){

         If (e. getClickCount () = 2)

System. out. println ("double-click action" + clickCount ++ );

}

});

}

Public static void main (String [] args ){

New MouseAndKeyEvent ();

}

}

........................................ ........................................ ...................................

Import java. awt .*;

Import java. awt. event .*;

Import java. io .*;

Class MyWindowDemo {

Private Frame f;

Private TextField tf;

Private Button;

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, "prompt message-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 ();

F. add (ta );

MyEvent ();

F. setVisible (true );

}

Private void myEvent (){

OkBut. addActionListener (new ActionListener (){

Public void actionreceivmed (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 actionreceivmed (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 = "your input information:" + dirPath + "is incorrect. Re-enter "; lab. setText (info); d. setVisible (true );

}

}

Public static void main (String [] args ){

New MyWindowDemo ();

}

}

........................................ ........................................ .......................

Package mymenu;

Import java. awt .*;

Import java. awt. event .*;

Import java. io .*;

Public class MyMenuTest {

Private Frame f;

Private MenuBar bar;

Private TextArea ta;

Private Menu fileMenu;

Private MenuItem openItem, saveItem, closeItem;

Private FileDialog openDia, saveDia;

Private File file;

MyMenuTest (){

Init ();

}

Public void init (){

F = new Frame ("my window ");

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

Bar = new MenuBar ();

Ta = new TextArea ();

FileMenu = new Menu ("file ");

OpenItem = new MenuItem ("open ");

SaveItem = new MenuItem ("save ");

CloseItem = new MenuItem ("exit ");

FileMenu. add (openItem );

FileMenu. add (saveItem );

FileMenu. add (closeItem );

Bar. add (fileMenu );

F. setMenuBar (bar );

OpenDia = new FileDialog (f, "I want to open", FileDialog. LOAD );

SaveDia = new FileDialog (f, "I want to SAVE", FileDialog. SAVE );

F. add (ta );

MyEvent ();

F. setVisible (true );

}

Private void myEvent (){

SaveItem. addActionListener (new ActionListener (){

Public void actionreceivmed (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. flush ();

Bufw. close ();

}

Catch (IOException ex ){

Throw new RuntimeException ();

}

}

});

OpenItem. addActionListener (new ActionListener (){

Public void actionreceivmed (ActionEvent e ){

OpenDia. setVisible (true );

String dirPath = openDia. getDirectory ();

String fileName = openDia. getFile (); // System. out. println (dirPath + "..." + fileName );

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 ");}

}

});

CloseItem. addActionListener (new ActionListener (){

Public void actionreceivmed (ActionEvent e ){

System. exit (0 );

}

});

F. addWindowListener (new WindowAdapter (){

Public void windowClosing (WindowEvent e ){

System. exit (0 );

}

});

}

Public static void main (String [] args ){

New MyMenuTest ();

}

}

How to Create a jar package that can be double-clicked?

1. encapsulate multiple classes in a package.

2. Define the configuration information of a jar package. Define a file a.txt. File Content: Main-Class :( space) package name. Class Name (Press ENTER)

3. Create a jar package. Jar-cvfm my. jar a.txt package name

4. Run the winrar program to check whether custom configuration information exists in the configuration file of the jar.

5. Use the tool file type-jar type file and advanced to define the program associated with the open action of the jar type file. Jdk \ bin \ javaw.exe-jar

6. Double-click it !.

Related Article

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.