Common methods and examples of GUI

Source: Internet
Author: User
Tags gettext gety

Frame:

Frame: Create a box
1 frame f = new Frame ("This is the title of the Box");//Create a box caption is this is the title of the box
The default call to paint (Graphics g) method requires overwrite

2 or write a class to inherit the frame class is also OK;
public class GUI extends frame{
public static void Main (string[] args) {
GUI A = new GUI ();//Create a box
Since the instantiation of the call to the GUI of the non-parametric construction without a parameter structure inside the default first row has a super (), call the parent class of the non-parametric construction, will create a frame box
}
}

SetLocation:

SetLocation ()://Set the position of the box to the upper left corner of the screen (that is, the screen origin) as standard
F.setlocation (100,100);//The position of the setting box is standard (x, y) in the upper-left corner of the screen

SetSize:

SetSize ()://Set Box size
F.setsize (200,200);//Width high (w,h)

SetBackground:

SetBackground ()://Set the background color of the box
1 F.setbackground (Color.Blue);//The background color of the set box is blue
2 F.setbackground (new color (0,0,102));//rgb:red,green,blue-> three primary Colors

Setresizable:

Setresizable ()://sets whether the size of the box can be changed or not to lengthen what
F.setresizable (false);//boolean type True/false

SetVisible:

SetVisible ()://Setting box is visible
F.setvisible (TRUE);//Do not write the default is False, generally you have to set either you create the box why

SetBounds:

SetBounds ()://Set position and width height x,y,w,h
F.setbounds (444,444,222,222);//Set position and width height at the same time

Panel:

Panel default layout manager is FlowLayout floating layout

1 panel p = new Panel ();//Create small control
2 panel p = new Panel (null);//small controls can be set directly inside the layout
3 panel p = new Panel (new FlowLayout (Flowlayout.left/right/center));
Left and right floating
Right float
Middle Floating in center
P.setbounds (50,50,100,100)///Set widget X,y,w,h Note! This setting is in the upper-left corner of the box (origin), as the standard is F
P.setbackground (new color (200,200,255));//Set the background color of the Gizmo
F.add (P);//The small space is added to the frame frame.

SetLayout:

SetLayout ()://Set Layout manager
There are three types of layout managers:
1 FlowLayout (); The Panel default
2 BorderLayout (); The frame default
3 GridLayout (); It's all going to work.

FlowLayout:

1 Panel default layout manager but you can also use BorderLayout to specify
panel p = new Panel (new FlowLayout (Flowlayout.left/right/center));
Panel p = new panl (new BorderLayout (Borderlayout.east/west/north/south/center));
Left and right floating
Right float
Middle Floating in center

BorderLayout:

2 Frame default layout manager but specified can also be used FlowLayout
1 frame f = new frame (borderlayout.east/west/north/south/center);
2 FlowLayout fl = new FlowLayout (flowlayout.center,20,40);//Set layout mode, horizontal spacing, vertical spacing
F.setlayout (FL);
East-west scaling to North and south
North-south to east-west scaling
Zooming in and around
East Eastern
West Western
Northern North
South
In CENTER
GridLayout:

3 layout manager for both frame and panel
GridLayout ();//Grid Layout Manager
F.setlayout (New GridLayout (2,2));//Two rows of two-column frame
P.set (New GridLayout (2,2));//panel's

Button:

Button B1 = New button ("button");//Create button after the parenthesis is the name of the button
Add a button to the frame and specify two of their locations by constants (public static final XXX (must be capitalized)):
1 F.add (B1, Borderlayout.east/west/north/south/center);//East (right), West (left), North (Upper), South (down), middle
2 F.add (B1, "East/west/north/south/center");//East (right), West (left), North (Upper), South (down), medium

Pack:

Pack ()://Whether packaging, the frame inside all the elements packaged, and then according to the size of the package, the frame is open, f.setsize (), it is invalid
F.pack ();//frame box with the elements inside

ActionListener:

To create a listener:
1 Listener class must implement ActionListener interface
2 The actionperformed (ActionEvent e) method in the ActionListener interface must be implemented

addActionListener:

Binding listeners:
addActionListener ();//give the specified button what the binding listener trigger listener will call actionperformed (ActionEvent e) This method by default
B.addactionlistener (m);//to the B-button binding listener m in the following example,

Setactioncommand:

Setactioncommand ()://to the listener binding description
B.setactioncommand ("I am the first button");

For example://Simple property settings Many properties are not set
Class Monitor implements actionlistener{//to create a listener class to implement the ActionListener interface
Implementing the Actionperformed (ActionEvent e) method in the ActionListener interface
public void actionperformed (ActionEvent e) {
System.out.println ("I clicked this button");
System.out.println (E.getactioncommand ());//Gets the description added to the listener
}
}
public class gui{
public static void Main (string[] args) {
Frame f = new Frame ("First name");//Create Box
Button B = New button ("button");//Create buttons
Monitor m = new monitor ();//Instantiate Listener class
B.addactionlistener (m);//Bind the listener and pass the object in
F.add (b);//Add the button to the box
F.setvisible (TRUE);//box visible
}
}

TextField:

TextField (): Bound listener, triggered by carriage return

TextField t = new TextField ();//Create a text box
TextField t = new TextField (10);//Create a text box and enter a maximum of 10
F.add (t);//Add a text box to the frame
T.addactionlistener (New Tfactionlistener ());//binding listener triggered by carriage return
T.setechochar (' * ');//password mechanism to mask the input text with *
F.pack ();//package has content to prop up frame frame
SetVisible (TRUE);//settings visible

Class Tfactionlistener implements actionlistener{//Create listener
Receiving an Event object,////a class to access local variables inside a method inside another class, is not possible
public void actionperformed (ActionEvent e) {
Gets the source object of the event object: In one class, you want to access the event source object inside another class, GetSource ()
TextField tf = (TextField) e.getsource ();
Pass the source of the event, and then move down.
Forces the type to be converted to a TextField object and then the ToString () output
System.out.println (Tf.gettext ());//Output text box contents
Then set the content to empty
Tf.settext ("");
}
}

GetSource:

GetSource (): Object that gets the event source
TextField tf = (TextField) e.getsource ();//As the above code, get the object of the event source, and then transformation down, you can access the unique properties

GetText:

T.gettext ();//Get the contents of a text box

SetText:

T.settext ("");//re-assign the text box to a "" is set to null

Setechochar:

T.setechochar (' * ');//The text box input Word with the * number instead of (just masking, output is what, or what, * just don't let directly see)

Label:

Label L = new label ("Note:");//Create a callout
F.add (L);//Add to Frame

Paint:

Paint (Graphics g);//The method is called by default when creating a frame frame
Graphics: As a brush, the frame as a canvas
GetColor:
Color c = G.setcolor ();//Get the original color of the brush
SetColor:
G.setcolor (color.red);//Set the color of the brush
G.setcolor (c);//IS to wash the brush, change the brush to the original color
Filloval:
G.filloval (50,55,55,55);//Draw a solid ellipse (inner-cut ellipse), position, width-height
FillRect:
G.fillrect (44,44,44,44);//Draw a solid rectangle, position, width and height
DrawOval:
G.drawoval (22,22,22,22);//Draw a hollow ellipse (inner-cut ellipse), position, width-height
DrawRect:
G.drawrect (22,22,22,22);//Draw a hollow rectangle, position, width and height

Mouseadapter:

F.addmouselistener (New Listener Class);//binding mouse listener trigger Listener, default call mousepressed (MouseEvent e) This method
Binding Mouse Monitoring:
1 Create listener class must inherit mouseadapter this abstract class
2 must be implemented (one of the methods, which is implemented here) mousepressed (MouseEvent e) This method
Class Monitor extends mouseadapter{
Mouselistener-->mouseadapter-->monitor (): If you implement the interface directly, you need to implement the interface method, but still mouseadapter all the methods of the interface are empty implementation, We're just going to write to cover the way we need it.
Mounseevent, when an event occurs, the event source sends the relevant object of the time through the listener to our processing class
The monitor is responding to the mouse down this event.
public void mousepressed (MouseEvent e) {

Get the event source object, who is the event source, Component,frame is the event source, the essence is to get the time source object reference
MyFrame f = (myframe) e.getsource ();//Get a reference to the source of the event
F.addpoint (New Point (E.getx (), E.gety ())),//point is a class GetX (), GetY () the coordinates of the mouse, with this coordinate as the origin, to spend a point, and then add the points to the frame frame

and re-paint the frame and all the elements inside it.
Canvas REDRAW mechanism: Front canvas requires state change before line, size, display or not, etc.
Repaint-->update-->paint (); Force refresh screen, entire redraw
Here the paint () method is called through the repaint, traversing the collection, forcing each point to redraw, point once, and send it from memory to the screen, forcing the data to refresh once
F.repaint ();
}
}

Windowadapter:

Creating a Window Listener
F.addwindowlistener (New Listener Class);//Frame Frame Binding window listener (can be closed)
To create a window listener:
1 Creating a listener class must inherit windowadapter this abstract class
2 must be implemented (one of the methods, which is implemented here) Windowclosing (WindowEvent) method
F.addwindowlistener (New Mywinmonitor ());//Binding window monitoring
Class Mywinmonitor extends windowadapter{//creating window listener classes
public void windowclosing (WindowEvent e) {//Overwrite method
SetVisible (false);//
System.exit (0);//Shutdown JVM
}

Anonymous inner class notation:
This.addwindowlistener (
New Windowadapter () {
public void windowclosing (WindowEvent e) {
SetVisible (TRUE);
System.exit (0);//Shutdown JVM
}
}
);

Keyadapter:

To create a keyboard listener:
1 Creating a keyboard listener class must inherit Keyadapter abstract class
2 The keypressed (KeyEvent e) method must be implemented

F.addkeylistener (New Listener Class);//To frame frame bound keyboard monitoring
Class Keymonitor extends keyadapter{
public void keypressed (KeyEvent e) {
int keycode = E.getkeycode ();//getKeyCode
if (keycode = = keyevent.vk_up) {//constant vk_up Here I only set the UP key,
System.out.println ("Up--------");
}
}
}
getKeyCode:
getKeyCode ();//get the corresponding integer value of the key
KeyEvent:
KeyEvent is a class. The constant vk_up inside has a corresponding int value.

Common methods and examples of GUI

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.