Java GUI Development 99 multiplication table
(1) Achieve the goal:
Using the Java-brought AWT package, the base control develops a 99 multiplication table, which can be clicked to display the corresponding multiplication formula.
(2) Control selection:
Click--button
Show--textfield
(3) interface design: Overall design layout
(4) Main form writing
Display the program in the center of the screen
1 /*Window Center Display*/2 intWin_width=fr.getwidth ();3 intwin_height=fr.getheight ();4Toolkit kit = Toolkit.getdefaulttoolkit ();//Defining the Toolkit5Dimension screensize = Kit.getscreensize ();//get the size of the screen6 intScreenWidth = Screensize.width;//gets the width of the screen7 intScreenHeight = Screensize.height;//gets the height of the screen8Fr.setlocation (SCREENWIDTH/2-WIN_WIDTH/2, SCREENHEIGHT/2-WIN_HEIGHT/2);
Write Close Program
/* Exit program */ Fr.addwindowlistener (New Java.awt.event.WindowAdapter () {public void windowclosing ( Java.awt.event.WindowEvent e) { system.exit (0); } });
(5) Frameinout form content and main algorithm
1 frameinout ()2 {3 Super("99 multiplication table");//Program Name4tx =NewTextField (20);5Tx.seteditable (false);6 Tx.setbackground (color.orange);7 Tx.setfont (f);8Tx.setbounds (490, 100, 230, 60);9 Add (TX);Ten One intI=0; A intJ=0; - for(i=1;i<=9;i++) - { the for(j=1;j<=i;j++) - { -Button BTN =NewButton (String.valueof (j) + "X" +string.valueof (i) + "=" +string.valueof (i*j)); -Btn.setbounds (j-1) *80+10+ (j-1) *10, 40+ (i-1) *40, 80, 25); + //Btn.setbackground (Color.cyan); - Add (BTN); +Btn.addactionlistener ( This); A } at - } - -SetLayout (NULL); -SetSize (820,410); -SetVisible (true); in SetBackground (color.orange); - } to + - Public voidactionperformed (ActionEvent e) the { *String s=E.getactioncommand (); $ Tx.settext (s);Panax Notoginseng}
(6) All codes
1 Packagep_3142;2 Importjava.awt.*;3 Importjava.awt.event.*;4 Importjava.io.IOException;5 6 Public classp3142 {7 8 Public Static voidMain (String args[])9 {Ten OneFrame fr=Newframeinout (); A /*Window Center Display*/ - intWin_width=fr.getwidth (); - intwin_height=fr.getheight (); theToolkit kit = Toolkit.getdefaulttoolkit ();//Defining the Toolkit -Dimension screensize = Kit.getscreensize ();//get the size of the screen - intScreenWidth = Screensize.width;//gets the width of the screen - intScreenHeight = Screensize.height;//gets the height of the screen +Fr.setlocation (SCREENWIDTH/2-WIN_WIDTH/2, SCREENHEIGHT/2-WIN_HEIGHT/2); - + /*Exit Program*/ AFr.addwindowlistener (NewJava.awt.event.WindowAdapter () at { - Public voidwindowclosing (java.awt.event.WindowEvent e) - { -System.exit (0); - } - }); in - } to } + - classFrameinoutextendsFrameImplementsActionListener the { * TextField TX; $ Label Lab;Panax NotoginsengFont f=NewFont ("Arial", Font.Bold, 40); - the frameinout () + { A Super("99 multiplication table");//Program Name thetx =NewTextField (20); +Tx.seteditable (false); - Tx.setbackground (color.orange); $ Tx.setfont (f); $Tx.setbounds (490, 100, 230, 60); - Add (TX); - the intI=0; - intJ=0; Wuyi for(i=1;i<=9;i++) the { - for(j=1;j<=i;j++) Wu { -Button BTN =NewButton (String.valueof (j) + "X" +string.valueof (i) + "=" +string.valueof (i*j)); AboutBtn.setbounds (j-1) *80+10+ (j-1) *10, 40+ (i-1) *40, 80, 25); $ //Btn.setbackground (Color.cyan); - Add (BTN); -Btn.addactionlistener ( This); - } A + } the -SetLayout (NULL); $SetSize (820,410); theSetVisible (true); the SetBackground (color.orange); the } the - in Public voidactionperformed (ActionEvent e) the { theString s=E.getactioncommand (); About Tx.settext (s); the } the the } +
View Code
Java-gui Development 99 Multiplication table