"Java" _ Graphical User interface (GUI)

Source: Internet
Author: User
Tags gettext gety

Learning Source: Stanford University Open Class programming method Cs106ajava

The code used in the learning process, the first draft of notes (mainly recorded in sequence video tutorial) and the course handout has been uploaded to the Download Center (interested people can download their own learning),

Most programs in the learning process need to import the Acm.jar package (uploaded to the Download Center, you can also go to http://jtf.acm.org/to download),


GObject:


Super ()--Call the parent class constructor

acm.graphics--the shapes added into the canvas are stacked in the order

gcanvas--Collage Background Canvas

CanvasGcanvas) and graphics programs (Graphicsprogram) has theMethod:
Add object to the canvas
Add (object,x,y) specifies that object coordinates be added
Remove (object) removes objects
Getelementat (x, y)-frontmost or null gets an object by coordinates
Gethidth () Get width
GetHeight () Get height
SetBackground (c)-color setting the canvas background color

a method unique to a graphics program:
Pause (milliseconds)--pause (in milliseconds)
Waitforclick ()--Wait for mouse click event

CObjects General Method:
setlocation (x, y)--set coordinates
Move (Dx,dy)-offset (DX is moving to the right, DX is negative to the left, DY is moving downward, dy is negative upward movement)
GetX () GetY ()--Returns the x and Y coordinates of the object
Gethidth () getheight ()--return length and width
Contains (x, y)-returns True (whether there is an object at a particular point)
SetColor (c) getColor ()--set/Get Object color
setvisible (flag)--set visibility (true/false)
IsVisible ()--Returns True if the object is visible visible
Sendtofront () SendToBack ()--Change the z-axis order (move the specified object to the top of the z axis | last position)
Sendforward () SendBackward ()--moving an object on the z axis

Common interfaces in GObject (Interface):
Fill interface: gfillable--setfilled (flag) isfilled () Setfillcolor (c) Getfillcolor ()
Set Object window: Cresizable--setsize (width,height) setbounds (x,y,width,height)
Scaling interface: Gscalable--scale (SF) scale (Sx,sy)-sx,sy is the zoom ratio on X, Y

character-related lines related to typography:
Baseline (baseline)-the line where the character appears (some characters will be exceeded)-Glide line
String height (height): refers to the distance between two lines of baseline
On-Line (ascent)-refers to the highest character--used when the character is centered
Off-line (decent)-refers to the distance that the character furthest reaches from the baseline

650) this.width=650; "style=" width:300px;height:216px; "src=" http://s3.51cto.com/wyfs02/M00/73/10/ Wkiom1xzjxcqse74aabenydors4048.jpg "title=" 2.png "alt=" wkiom1xzjxcqse74aabenydors4048.jpg "height=" 216 "hspace=" 0 " Border= "0" vspace= "0" width= "/>"


Gpolygon--draw symmetrical graphs of line segments
Need to have a reference point (imaginary reference point)--usually the center point of the graph


Usage:(Draw a diamond)
Private Gpolygon creatediamend (double width, double height) {
Gpolygon diamend = new Gpolygon ();
Diamend.addvertex (-WIDTH/2, 0);
Diamend.addedge (width/2,-HEIGHT/2);
Diamend.addvertex (0,-HEIGHT/2);
Diamend.addedge (WIDTH/2, HEIGHT/2);
Diamend.addvertex (WIDTH/2, 0);
Diamend.addedge (-WIDTH/2, HEIGHT/2);
Diamend.addvertex (0, HEIGHT/2);
Diamend.addedge (-width/2,-HEIGHT/2);
return diamend;
}
To specify a polygon vertex:
Addvertex (x, y) ———— x, y is the coordinates relative to the reference point
Addedge (Dx,dy)--Add a vertex related to the previous point

Gcompound--Draw composite graphics--compound several shapes together to become an object

Event Driver (Event-driven Programs)

Listener (Listener)-impot java.awt.event.*;
Addmouselisteners ()--Mouse listener
Addkeylisteners ()--Keyboard listener

Mouse Events:
Mouseclicked (E)--click
Mousepressed (e)--Press the mouse
Mousereleased (e)--Release the mouse
Mousemoved (e)--Mobile Mouse
Mousedragged (E)-Drag by mouse

Keyboard Events:
Keypressed (e)--Press a key in the keyboard
Keyreleased (e)--loosen a key in the keyboard
Keytyped (e)--pressed and released compound


Usage:
/* Capture real-time location information for mouse */
public class Mousetracker extends graphicsprogram{

public void Run () {
label = new Glabel ("");
Label.setfont ("Times New Roman-36");//Set Font
Add (Label, 50, 50);

Addmouselisteners ();//Add Mouse Listener
}
public void mousemoved (MouseEvent e) {//action performed when mouse moves
Label.setlabel ("Mouse: (" +e.getx () + "," +e.gety () + ")");//Display Mouse location information
}

Private Glabel label;
}

Graphical User Interface--gui (Graphic User Interface)--"gooey"
Import java.awt.event.*;--Tracking Events
Import javax.swing.*;--Creating an Interaction object

components:
button (Button)
Sliders (sliding block)
Checkboxes (check box)
Radio bottons (Radio box)
Combo Box (Combo frame, drop-down box)
text Box (textbox)

J Components-Interactive components

Action receiver: Addactionlisteners ();


program Window
Divided into five parts:--border layout (border layouts)

650) this.width=650; "style=" WIDTH:300PX;HEIGHT:189PX; "src=" http://s3.51cto.com/wyfs02/M02/73/0D/ Wkiol1xzkfty4l03aaftoybyrai801.jpg "title=" window. png "alt=" wkiol1xzkfty4l03aaftoybyrai801.jpg "height=" 189 "hspace=" 0 "border=" 0 "vspace=" 0 "width="/>

Use of interactive components:


Button:

public void init () {
but = new Jbtton ("Hi");
Add (but, south);//Adds the button to South
Addactionlisteners (); --Action Receiver
}

public void actionperformed (ActionEvent e) {//Receive action and perform appropriate action
String cmd = E.getactioncommend ();//Returns the value of the object
if (Cmd.equals ("Hi")) {
Prinln ("Hello");
}
Or:
if (E.getsource = = but) {//getsource returns an object
Prinln ("Hello");
}
}

Private JButton but;//Declaration button Component

Checkbox:
... init () {
Check = new Jcheckbox ("Front");
Check.setselcted (TRUE); --Set Default to selected state
Add (Check,south); --Add check box
}
Private Jcheckbox check;

RadioButton:
Private Jradiobutton SM;
Private Jradiobutton med;
Private Jradiobutton lag;

SM = new Jradiobutton ("small");
Med = new Jradiobutton ("Medium");
lag = new Jradiobutton ("large");

Buttongroup size = new Buttongroup (); Merge a radio box into a group
Size.add (SM);
Size.add (MED);
Size.add (lag);

Med.setselected (TRUE); Set default options
Add (SM, south);
Add (Med, south);
Add (lag, south);

Combo Box:
Pick = new JComboBox ();
Pick.additem ("Black");//Add options
Pick.additem ("Blue");
......
Pick.seteditable (False);//Set User cannot edit
Pick.setselecteditem ("Black");//Set default options
Add (Pick,south);

Private JComboBox pick;

textbox (text flied)
JTextField tf = new JTextField (10);
Tf.addactionlisteners (this);
Add (TF. South);

if (E.getsource = = tf) {
println ("HI" + tf.gettext ());//tf.gettext () returns the contents of the text box
}

layouts (layout)


Border layouts (Border layout)
Grid layout--add elements from the upper-left corner to the right, until the end of the line, and then wrap the lines
Table layout-Much like a grid layout but more precise (does not let each part fill the entire cell)


    Usage:
        setlayout (new GridLayout (2,3)) ;//2 row 3 columns
        setlayout (new Tablelayout (2,3));
        add (New JButton ("button 1"));
        add (New JButton ("button 2"));
        add (New JButton ("button 3"));
        add (New JButton ("button 4"));
        add (New JButton ("button 5"));
        add (New JButton ("button 6"));
        
components and corresponding containers--component/container

650) this.width=650; "style=" width:500px;height:316px; "src=" http://s3.51cto.com/wyfs02/M00/73/10/ Wkiom1xzkgqq5ipoaagotczmpwm298.jpg class "alt=" Wkiom1xzkgqq5ipoaagotczmpwm298.jpg "height=" in "title=" component "0" Border= "0" vspace= "0" width= "/>"


Addcomponentlistener (this);//listeners
Line analysis function: ParseLine ();


Example: (Create a container MyCanvas)

650) this.width=650; "style=" WIDTH:700PX;HEIGHT:307PX; "src=" http://s3.51cto.com/wyfs02/M00/73/10/ Wkiom1xzkuis40rjaat6w5itqqg451.jpg "title=" container. png "alt=" wkiom1xzkuis40rjaat6w5itqqg451.jpg "height=" 307 "hspace=" 0 "border=" 0 "vspace=" 0 "width="/>


Thread: Start the same program, you can run multiple processes at the same time (one thread is the equivalent of a process, and multiple threads do not interfere with each other)

runnable Interface : classes that inherit from the Runnable interface can be used as a thread

Create a thread:(a Class)
public class Myclass implement Runnable {
Public Myclass () {}
public void Run () {}
}

Calling Thread:(through Thread.Start () calls the execution method of the thread)
Myclass x = new Myclass ();
Thread t = new thread (x);
T.start ();


frame (JFrame)--Frame for window loading


Usage:(Create a graphical hello framework)
public class Graphicalhelloworld {

public static void Main (string[] args) {
JFrame frame = new JFrame ("Graphical Hello");//create frame and name it-graphical Hello
Create label Hello world! And make it show in the play
JLabel label = new JLabel ("Hello world!", jlabel.center);//center Justifield
Add a label to the frame, set the frame size
Frame.add (label);
Frame.setsize (500, 300);
Frame.setdefaultcloseoperation (jframe.exit_on_close);//end process when user exits
Frame.setvisible (TRUE);//Set frame visibility
}
}
650) this.width=650; "style=" WIDTH:400PX;HEIGHT:237PX; "src=" http://s3.51cto.com/wyfs02/M00/73/10/ Wkiom1xzknsybw6aaabc2ofcjii047.jpg "title=" frame. png "alt=" wkiom1xzknsybw6aaabc2ofcjii047.jpg "height=" 237 "hspace=" 0 "border=" 0 "vspace=" 0 "width="/>






This article from "Not Daze" blog, please make sure to keep this source http://tobeys.blog.51cto.com/10620284/1694031

"Java" _ Graphical User interface (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.