Project Three: Experiment six graphical user interface(textbook p279-280)
Goal: Complete the contents of the experiment six.
run code
Import Java.awt.frame;import Java.awt.button;import Java.awt.Label; Import Java.awt.event.ActionListener; Import Java.awt.event.actionevent;import java.awt.flowlayout;import java.awt.textfield;import Java.awt.TextArea; Import java.awt.Dimension; Import Java.awt.font;import Java.awt.color;import java.lang.Exception; public class Chatserver implements actionlistener{private Frame Fm = new Frame ();p rivate button quit = New button ("Exit "); Private button Add = New button ("+"); Private button Sub = New button ("-"); Private button Mul = New button ("X"); Private button Div = New button ("÷"); Private Label Addend = new Label ("Operand 1:"); Private Label Augend = new Label ("Operand 2:");p rivate Label Sum = new label ("Result:"); Private TextField Fieldadd = new TextField (); Private TextField Fieldaug = new TextField (); Private TextArea areasum = new TextArea (); Private font Ft = new Font ("Arial", font.bold,18);p ublic chatserver () {fm.settitle ("tyh number Calculator"); Fm.setbounds (200,100,400,300); Fm.setlayout (New FLowlayout (flowlayout.left,20,20)); Fm.add (Addend);//Label--operand 1 addend.setfont (Ft);//font type addend.setforeground (color.black);//Set Font color Addend.setpreferredsize (New Dimension (100,30));//Set Component size Fm.add (FIELDADD);//input area-operand 1 fieldadd.setpreferredsize ( New Dimension (100,30));//Set Component size Fieldadd.setfont (FT);//font type fieldadd.setforeground (Color.Black); Fm.add (add); Add.addactionlistener (this); Fm.add (Sub); Sub.addactionlistener (this); Fm.add (augend);//Label--operand 2 augend.setfont (Ft); Augend.setforeground (Color.Black); Augend.setpreferredsize (New Dimension (100,30));//Set Component size Fm.add (FIELDAUG);//input area-operand 2 fieldaug.setpreferredsize ( New Dimension (100,30));//Set Component size Fieldaug.setfont (FT); Fieldaug.setforeground (Color.Black); Fm.add (Mul); Mul.addactionlistener (this); Fm.add (DIV); Div.addactionlistener (this); Fm.add (Sum);//Label--result Sum.setfont (Ft); Sum.setforeground (Color.Black); Sum.setpreferredsize (New Dimension (100,30));//Set Component size Fm.add (areasum); Areasum.setpreferredsize (New DimensIon (100,30));//Set Component size Areasum.setfont (FT); Areasum.setforeground (color.red); Fm.add (quit); Quit.addactionlistener (this); Fm.setvisible (TRUE); }public void actionperformed (ActionEvent e) {try {if (E.getsource () ==quit) system.exit (0); else if (E.getsource () ==add) Areasum.settext (double.tostring (double.parsedouble ()) + Double.parsedouble (Fieldaug.gettext ())); else if (E.getsource () ==sub) Areasum.settext (double.tostring (double.parsedouble ())- Double.parsedouble (Fieldaug.gettext ())); else if (E.getsource () ==mul) Areasum.settext (double.tostring (double.parsedouble ()) * Double.parsedouble (Fieldaug.gettext ())); else if (E.getsource () ==div) Areasum.settext (double.tostring (double.parsedouble ())/ Double.parsedouble (Fieldaug.gettext ())); } catch (Exception Ex) {areasum.settext ("error:" +ex); }} public static void Main (String args[]) {Chatserver call = new Chatserver (); } }
Run results
Problem: The background does not get, the panel will not layout
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Programming Week 16th Thursday: GUI Programming and File Dialog Usage Calculator