Getting started in Java - Chapter 6 Java graphics user interface

zhaozj2021-02-17  51

Chapter 6 Java Graphical User Interface

For an excellent application program, good graphic user interface is unpleasant. The lack of good graphic user interface will give the user to solve and use the application procedure. It's hard to think that the user will learn to use a should be used to remember a large stack of commands. Java mentioned; a good graphical user interface required? COPY; basic component: panel (panel? Copy; buttton (butt, copy; (label? Copy; Copy; Scrollbar? copy;, list box (list? copy;, text field? copy ;, text area

6.1 panel

Top of the panel COPY; the space for the establishment of a procedure. You can put the graphic components (including other panels? Copy; put on a panel. Applet class? Copy; a basic panel.

6.1.1 布 布

Java mentioned; several layouts: order layout (Flow Layout? Copy; Border Layout? Copy; and grid layout (Grid Layout? Copy ;.

6.1.1.1 Sub-order

Sequential layout (Flow Layout? Copy; is the most basic layout, the default layout of the panel is the order layout. The order layout refers to a graphic component to pick up one place? REG; placed on the panel. Below is a order Example of layout:

Import java.Awt. *; import java.applet.applet;

Public class mybuttons extends applet {Button Button1, Button2, Button3; Public void init () {Button1 = new button ("OK"); button2 = new button ("Open); Button3 = New Button; add (Button1); add (button2); add (button3);}}

The layered layout is as follows:

Figure 6.1

6.1.1.2 Borderline Board

Borderlines include five districts: North District, South District, Eastern District, Western Region and Central District. These partitions on the panels are "Upper South, left West Right". The lower side is an example of a side boundary:

Import java.Awt. *; import java.applet.applet;

Public class buttir extends applet {

Button Buttonn, Buttonw, Buttone, ButtonC;

Public void init () {setLayout (new borderLayout ()); button = new button ("? reg;"); Buttons = New Button; Buttone = New Button; buttonw = New Button ("Gold"); Buttonc = New Button ("Soil"); Add ("South", Buttonn; Add ("EAST", Buttone); add ("West", Buttonw ); add ("center", buttonc);}} The following is the result of the program run:

Figure 6.2

6.1.1.3 Net Grid

The grid fabric divides the panel into one of the netizers, you? To give the number of rows and columns of the grid. The lower surface is an example of a grid layout:

Import java.Awt. *; import java.applet.applet;

Public class button1, button2, button3, button5, button6, button7, button8;

Public void init () {setLayout (4, 2)); Button1 = New Button ("dry"); button2 = new button ("Kun"); Button3 = New Button (""); button4 = New Button5 = New Button; Button6 = New Button; Button7 = New Button ("巽"); Button8 = New Button; Add (Button1); Add (Button1 Add (Button2); add (button3); add (button5); add (button6); add (button7);}}}

The lower surface is the result of the program operation:

Figure 6.3

6.2 button

6.2.1 button incident

With a button down, there will be a button event to generate. You can capture the button event by overlying an Action member function of an applet.

Public Boolean Action (EVENT E, Object O) {IF (E.TARGET InstanceOf Button) {system.out.println ((String) O);} else {system.out.println ("non-button evenet");} Return True;}

6.2.2 Button Type

JAVA mentioning; the standard pressed button, also mentioned when it is also mentioned; the select button and the marker button are selected.

6.2.2.1 Select the button

Select the option button to mention Copy; select the function of selecting an option in several options. The next surface is selected from several markets, the city name is placed in the selected button: citychooser = new choice ();

Citychooser.Additem ("North? Copy;"); Citychooser.Additem ("Shanghai"); Citychooser.Additem ("Tianjin");

Add (Citychooser);

Figure 6.4

6.2.2.2 Tag Press

The status of the labeled button is returned to the object parameter of the standard box event. The lower surface is an example of a marking button:

Checkbox FillStyleButton; FillStybutton = New Checkbox ("Solid");

Public Boolean Action (EVENT E, Object Arg) {IF (E.TARGET InstanceOf Checkbox) {system.out.println ("Checkbox:" arg);} return true;

Figure 6.5

6.2.2.3 Press the button button

Press the key button is a set of buttons, users can select one of them, and other buttons in this group will be closed. The lower surface is an example of a button button: public class checkbox extends applet {checkboxgroup CBG;

Public void init () {CBG = New CheckboxGroup (); Add ("One", CBG, TRUE); Add ("Two", CBG, False); Add (New Checkbox ("Three "CBG, FALSE);}}

Figure 6.6

6.2.3 Self-package button

Java language facing the objective features make us have enough to build a full self-contained button. In the self-package button, you can build a status control function in the case. The lower surface is an example of a self-package button:

Import java.Awt. *; import java.applet.applet;

Class Okbutton Extends Button {

Public okbutton () {setlabel ("ok");

Public Boolean Action (Event E, Object Arg) {system.out.println ("okbutton"); return true;}}}

Public class buttonTest Extends applet {okbutton myokbutton;

Public void init () {myokbutton = new okbutton (); add (myokbutton);}}

Figure 6.7

6.3 mark? Copy;

Shooting; is a static text that is placed on the panel. Is the next side? Copy; example: Import java.awt. *; Import java.applet.applet;

Public class label extends applet {

Public void init () {setLayout (new flowLayout (FlowLayout.center, 10, 10)); label label1 = new label ("Hello!"); label label2 = new label ("another signage copy;"); Add (label1); add (label2);}} The following is the result:

Figure 6.8

6.4 list box

The list box makes the user easy to operate a large number of options. How to create a list box and choice button? Copy; All entries of the list box are visible, if you have a lot of choices, you will be around the range of the list box visible area, and there will be a scroll bar next to the list box. First, create a list box: List L = New List (4, false); this member function created a list box that displayed 4 rows. The second parameter "false" indicates that this list box is single, if it is "True", the representation is more selected. The choice of the list box below:

L.ADDITEM ("North? Copy; University"); L.Additem ("Tsinghua University"); L.Additem ("Jilin University"); L.Additem ("Reicing Copy; University"); L.Additem "Nankai University"); L.Additem ("Tianjin University"); L.ADDITEM ("South? Copy; University"); Add (L);

Figure 6.9

6.4.1 Select in the list box

You can receive the options selected in the list box with the member function GetSelectedItem () or getSelectedItems (). In a single-selection list box, a "Double-click" an option can be triggered a event that can be captured by the action () member function. Public Boolean Action (EVENT E, Object Arg) {.........};

6.4.2 Multi-Select List Box

For multi-selection list boxes, you should make your choice of production, you need to use other external events. For example, you can use the button event:

Figure 6.10

Public Boolean Action (EVENT E, OBJECT ARG) {... if (e.target instanceof button) {........ ("ok" .Equals (arg)) {string [] selected; selected = l.getSelected For (int i = 0; i

6.5 text

Text domains are used to allow users to enter information such as a name, a letter card number, which is a small block area entered with a keyboard that can be received.

6.5.1 Creating a text This domain When you create a text domain, there are four types? Copy; you choose: empty, empty, and has a specified length, with initial text This content and the initial text content is specified. The next is a code that is born into these four texts:

Textfield TF1, TF2, TF3, TF4;

// empty text domain TF1 = new textfield (); // Length 20 empty text field TF2 = new textfield (20); // Text field with initial text content TF3 = New TextField ("Hello" ); // Text domain with initial text and has a designated length TF4 = New TextField ("Hello", 30); Add (TF1); add (tf2); add (tf3); add (tf4);

Figure 6.11

6.5.2 Text domain events

When the user knocked on the "Enter" button in the text field, it produced a textual event. You can capture this event in your event () in the Member Function Action ().

Public Boolean Action (EVENT E, Object Arg) {... if (e.target instanceof textfield) {system.out.println ("textfield:" arg);}.

6.6 text

Text area can display the text of the large section.

6.6.1 Creating a text

Similar to textual domain, there are four types of types when creating a text. Copy; select, but if you specify the size of the area, you must specify the number and column number at the same time.

TEXTAREA TA1, TA2; // An empty text area ta1 = new textarea ();

// A text of the text in the initial content, a size of 5 x 40. TA2 = New Textarea ("Hello!", 5, 40);

You can use the member function setEDITABLE () to determine if the user can edit the contents of the text area.

/ / Make text district for read-only TA2.SetEtedItable (false)

Figure 6.12

6.6.2 Connecting the contents of the text

You can obtain the front content of the text area with the member function GetText (). For example: system.out.println (ta1.gettext ()); text area does not have a self-employed event. But you can use external events to receive the contents of the text:

Public Boolean Action (EVENT E, OBJECT O) {if ("send" .Equals (o)) {string texttosend = ta1.gettext (); system.out.println ("Sending: " texttosend); MysendFunction (TEXTTOSEND);}} else {..

6.7 drawing board

Can the drawing board catch? Copy; dew accident, mouse event, and other similar events. Basic paintings are not processed this? Copy; incubation, but can you be? Copy; show it to create a painting board that you need. 6.7.1 Creating a Painting Board

Import java.Awt. *; import java.applet.applet;

Public class supergui extends applet {... Mycanvas doodle;.... public void init () {................................................................. LeftPanel.Add ("Center", Doodle);...

Class Mycanvas Extends Canvas {Public Void Paint (Graphics G) {g.drawRect (0, 0, 99, 99); G.DrawString ("Canvas", 15, 40);}}

6.7.2 drawing board event

You can cover a general event to handle the function function. The lower surface is an example containing the mousedown event:

Import java.Awt. *; import java.applet.applet;

Public class canvas extends applet {

Button B1;

Public void init () {// set outlayout (New BorderLayout (15, 15)); b1 = new button ("test"); mycanvas c1 = new mycanvas (100, 100); // add The Canvas and The Button to The Applet Add ("Center", C1); Add ("South", B1);

Public Boolean Action (Event E, Object Arg) {System.out.println ("Event:" arg); Return true;}

Public Boolean MouseDown (Event E, INT X, INT Y) {System.out.Println ("Mouse Works: (" X "," Y ")") "); Return true;}}}

Class Mycanvas Extends Canvas {Private Int Width; Private INT Height

Public mycanvas (int W, int h) {width = W; height = h; reshape (0, 0, w, h);}

Public void Paint (Graphics G) {g.setcolor (color.blue); g.FillRect (0, 0, width, height);}

Public Boolean MouseDown (Event E, INT X, INT Y) {IF ((x

In a case? Copy; program, you need to adjust the linear value, which is required to scroll. Scroll strips Copy; the range of the range or district of easy-to-operate value.

6.8.1 Creating a scroll bar

When creating a scroll bar, you must specify its direction, initial value, size, minimum, and maximum value of the slider.

Public Scrollbar (int Orientation, Int InitialValue, int sizeofslider, int minvalue, int maxvalue);

Below is an example:

ScrollBar Redslider; public void init () {redslider = new scrollbar (scrollbar.vertage, 0, 1, 0, 255); add (redslider);}

Figure 6.13

6.8.2 Rolling Bar Event

And other interface components, the scroll strip is generated by a controlling event, but it is not true with other things, you must use the membership function "Handleevent () without using the member function action ().

Public Boolean Handleevent (E) {IF (E.TARGET InstanceOf Scrollbar) {System.out.println ("Scrollbar:" ((scrollbar) E.TARGET .GetValue ()); return true;} return super.handleevent (e);

6.8.3 Display of the value of the scroll bar

If you want to display the value of the slider, you need to add a self-employed language. Below is an example.

Import java.Awt. *; import java.applet.applet;

Public Class Redslider Extends Applet {scrollbar redslider; textfield redvalue; label redlabel

Public void init () {setLayout (New GridLayout (1, 3)); redslider = new scrollbar (scrollbar.horizontal, 0, 1, 0, 255); redValue = new textfield ("0", 5); redValue.setedItTable (false); redlable = new label ("RED (0-255)"); add (redlabel); add (redslider); add (redvalue);}

Public Boolean Handleevent (E) {if (E.TARGET InstanceOf Scrollbar) {redValue.setText (Integer.tostring ((scrollbar)) .GetValue ())); return true;} return super.handleevent (e );} public boolean action (Event E, Object Arg) {system.out.println ("Event" arg); return true;}}

Figure 6.14

chapter summary

1. JAVA mentioned; generated a good graphical user interface required? COPY; basic component: panel (panel? Copy; button (button Copy; (label? Copy; (Canvas? Copy;, scroll bar? Copy;, list box (list? Copy;, text field (text field? Copy; You can capture and process them.

转载请注明原文地址:https://www.9cbs.com/read-29494.html

New Post(0)