Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java graphical user Interface

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to understand the Java graphical user interface, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Java graphical user interface

For an excellent application, a good graphical user interface is essential. The lack of a good graphical user interface will bring a lot of inconvenience to users to understand and use the application. It's hard to imagine users memorizing a lot of commands in order to learn to use an application.

6.1 Panel

The panel copy; provides the space to build the application. You can put graphic components (including other panels? copy;) on one panel. The Applet class mentions copy; a basic panel.

6.1.1 layout Management

Java copy; several layouts: sequential layout (FlowLayout?copy;, border layout (BorderLayout?copy;), and grid layout (GridLayout?copy;).

6.1.1.1 Sequential layout

Sequential layout (FlowLayout?copy; is the most basic layout, and the default layout of panels is sequential layout. Sequential layout refers to placing graphic elements one by one reg; flat on the panel. Here is an example of a sequential layout:

Importjava.awt.*;importjava.applet.Applet

PublicclassmyButtonsextendsApplet {Buttonbutton1,button2,button3;publicvoidinit () {button1=newButton (OK); button2=newButton (Open); button3=newButton (off); add (button1); add (button2); add (button3);}}

The layout generated by the program is as follows:

Figure 6.1

6.1.1.2 Boundary layout

The boundary layout includes five districts: North, South, East, West and Central. The distribution law of these areas on the panel is "up north and down south, left west and right east". Here is an example of a boundary layout:

Importjava.awt.*;importjava.applet.Applet

PublicclassbuttonDirextendsApplet {

ButtonbuttonN,buttonS,buttonW,buttonE,buttonC

Publicvoidinit () {setLayout (newBorderLayout ()); buttonN=newButton ("? reg;"); buttonS=newButton ("Fire"); buttonE=newButton ("Wood"); buttonW=newButton ("Gold"); buttonC=newButton ("Earth"); add ("North", buttonN); add ("South", buttonS); add ("East", buttonE); add ("West", buttonW); add ("Center", buttonC);}}

Here is the result of running the program:

Figure 6.2

6.1.1.3 Grid layout

The grid layout divides the panel into grids, and you can give the number of rows and columns of the grid. Here is an example of a grid layout:

Importjava.awt.*;importjava.applet.Applet

PublicclassbuttonGridextendsApplet {Buttonbutton1,button2,button3,button4,button5,button6,button7,button8

Publicvoidinit () {setLayout (newGridLayout (4jue 2)); button1=newButton ("dry"); button2=newButton ("Kun"); button3=newButton ("Gen"); button4=newButton ("Zhen"); button5=newButton ("Kan"); button6=newButton ("Li"); button7=newButton ("Sunda"); button8=newButton ("against"); add (button1); add (button2); add (button3); add (button4); add (button5); add (button6); add (button7); add (button8);}}

Here is the result of running the program:

Figure 6.3

6.2 Button 6.2.1 Button event

When the user clicks a button, a button event occurs. You can capture button events by overriding an action member function of applet.

Publicbooleanaction (Evente,Objecto) {if (e.targetinstanceofButton) {system.out.println ((string) o);} else {System.out.println ("Non-buttonevent");} returntrue;}

6.2.2 Button Typ

Java copy; standard push buttons, as well as copy; select buttons and mark buttons.

6.2.2.1 Select button

The selective button copy; provides the ability to select one of several options. The following is an example of selecting a city from several cities, with the city name in the select button:

CityChooser=newChoice ()

CityChooser.addItem ("North copy;"); CityChooser.addItem ("Shanghai"); CityChooser.addItem ("Tianjin")

Add (CityChooser)

Figure 6.4

6.2.2.2 marked button

The status of the tagged button is returned as an object parameter to the tag box event. Here is an example of a tagged button:

CheckboxfillStyleButton;fillStyleButton=newCheckbox ("Solid")

Publicbooleanaction (Evente,Objectarg) {if (e.targetinstanceofCheckbox) {System.out.println ("Checkbox:" + arg);} returntrue;}

Figure 6.5

6.2.2.3 push button

Push button is a set of buttons, one of which can be selected by the user, while the other buttons in this group will be turned off. Here is an example of a push button: publicclassCheckBoxextendsApplet {CheckboxGroupcbg

Publicvoidinit () {cbg=newCheckboxGroup (); add (newCheckbox ("one", cbg,true)); add (newCheckbox ("two", cbg,false)); add (newCheckbox ("three", cbg,false));}}

Figure 6.6

6.2.3 self-include button

The object-oriented nature of the Java language enables us to create fully self-contained buttons. In the self-contained button, you can create event control functions in the copy; display button class. Here is an example of a self-contained button:

Importjava.awt.*;importjava.applet.Applet

ClassokButtonextendsButton {

PublicokButton () {setLabel ("Ok");}

Publicbooleanaction (Evente,Objectarg) {System.out.println ("OKButton"); returntrue;}}

PublicclassbuttontestextendsApplet {okButtonmyOkButton

Publicvoidinit () {myOkButton=newokButton (); add (myOkButton);}}

Figure 6.7

6.3Standard? copy

Mark? copy; is a static text placed on a panel. Here is an example of a standard copy;: importjava.awt.*;importjava.applet.Applet

PublicclasslabelextendsApplet {

Publicvoidinit () {setLayout (newFlowLayout (FlowLayout.CENTER,10,10)); Labellabel1=newLabel ("Hello!"); Labellabel2=newLabel ("another label? copy;"); add (label1); add (label2);}}

The following is the running result:

Figure 6.8

6.4 list box

The list box makes it easy for users to manipulate a large number of options. The method of creating a list box is similar to Choicebutton's? copy;. All entries in the list box are visible, and if there are many options beyond the visible area of the list box, there will be a scroll bar next to the list box. First, create a list box: Listl=newList (4 focus false); this member function creates a list box that displays four rows. The second parameter, "false", indicates that the list box is solo, and if it is "true", it means multiple selections. The following options are added to the list box:

L.addItem ("North copy; University"); l.addItem ("Tsinghua University"); l.addItem ("Jilin University"); l.addItem ("Fu 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 use the member function getSelectedItem () or getSelectedItems () to receive the option selected in the list box. In the radio list box, double-click an option to trigger an event that can be captured by the action () member function. Publicbooleanaction (Evente,Objectarg) {... if (e.targetinstanceofList) {System.out.println ("Listentry:" + arg);}.}

6.4.2 multiple selection list box

For the multi-select list box, to make your choice work, you need to use other external events. For example, you can use button events:

Figure 6.10

Publicbooleanaction (Evente,Objectarg) {... if (e.targetinstanceofButton) {. If ("Ok" .equals (arg)) {string [] selected;selected=l.getSelectedItems (); for (intI=0;I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report