Java--gui

Source: Internet
Author: User
Tags event listener

GUI is a graphical user interface of the English abbreviation, say a bit straightforward GUI is the window, buttons and other elements of the collective name. the implementation GUI in Java is programmed by using the java.awt package and its sub-packages.

In the java.awt package component and container are its two cores, the former includes the implementation class of button, TextField, and the latter is the subclass of component, but the latter is some frame, Container implementation classes such as panel, which can hold component objects.

The simple thing to say is that the container object can add buttons, TextField, and so on. is further explained in this regard.



Layout Manager

Like a house, bought a lot of furniture, as for how to put these furniture , it is entirely up to the owner of the house to decide, and the owner of the house is the layout manager.

example: Set up 7 buttons and arrange them with the FlowLayout center.

import java.awt.*;p ublic class TestFlowLayout2 {public static void main (String arg        S[]) {frame F = new Frame ("Java frame");        FlowLayout L = new FlowLayout (Flowlayout.center, 20, 40);         F.setlayout (l);        F.setlocation (300,400);        F.setsize (300,200);        F.setbackground (New Color (204,204,255));        for (int i = 1; i<=7; i++) {f.add (New button ("button"));    } f.setvisible (True); }} 

Event Monitoring

Adds an event to an element such as a button in a container, and listens with an object that implements the object listener interface.

Example: Click on the keyboard "arrow up" button to print "Up" in the cmd window .

<pre name= "code" class= "java" >import java.awt.*;import java.awt.event.*;p ublic class testkey{public static void Main (String arg[]) {new Keyframe (). Launchframe ();}} Class Keyframe extends Frame{public void Launchframe () {setSize (200,200); setlocation (300,300); Addkeylistener (new Mykeymon ()); Register Event setvisible (TRUE);}} Class Mykeymon extends keyadapter{   //Listener class public void keypressed (KeyEvent e) {int keycode=e.getkeycode (); if (keycode ==KEYEVENT.VK_UP) {System.out.println ("up");  Listener Action}}}

Listener Class Creation Method:

Inner classes: Write a class inside the perimeter class that facilitates access to member variables and member methods.

The transformation of the above example.

Import Java.awt.*;import java.awt.event.*;p ublic class testkey{public static void Main (String arg[]) {new Keyframe (). Launchframe ();}} Class Keyframe extends Frame{public void Launchframe () {setSize (200,200); setlocation (300,300); Addkeylistener (new Mykeymon ()); setvisible (true);} Class Mykeymon extends keyadapter{   //internal class public void keypressed (KeyEvent e) {int keycode=e.getkeycode (); if (keycode= =KEYEVENT.VK_UP) {System.out.println ("Up");}}}}

Anonymous classes: declare them with the new statement when they are created.

The transformation of the above example.

Import Java.awt.*;import java.awt.event.*;p ublic class testkey{public static void Main (String arg[]) {new Keyframe (). Launchframe ();}} Class Keyframe extends Frame{public void Launchframe () {setSize (200,200); setlocation (300,300); This.addkeylistener ( New Mykeymon () {public void keypressed (KeyEvent e) {    //anonymous class int keycode=e.getkeycode (); if (keycode==keyevent.vk_up) { System.out.println ("Up");}}); SetVisible (True);}}

Summary: 1. The GUI is implemented by the java.awt package and its child packages. 2. Add these elements to the container element, such as button. 3. Layout Manager manages the layout of elements in the container. 4. The elements in the container can be added to the event listener.


Java--gui

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.