1 Packagetest;2 Importjava.awt.*;3 Importjavax.swing.*;4 Importjava.awt.event.*;5 Public classFrameDemo26 {7 StaticJTextField field1=NewJTextField (5);8 StaticJTextField field2=NewJTextField (5);9 StaticJTextField field3=NewJTextField ("0", 5);Ten One PublicFrameDemo2 () { AJFrame f=NewJFrame ("Adder"); - f.setdefaultcloseoperation (jframe.exit_on_close); -F.setsize (220,100); theF.setlocation (500,300); -JLabel jb=NewJLabel ("+"); -F.setvisible (true); -JPanel p1=NewJPanel (); + F.setcontentpane (p1); -P1.setlayout (NewFlowLayout ()); + A P1.add (field1); at P1.add (JB); - P1.add (field2); - P1.add (field3); -JButton b1=NewJButton ("Calculation"); -Color bg=NewColor (255,255,255); - b1.setbackground (BG); in P1.add (B1); - toB1.addactionlistener (NewActionListener () + { - Public voidactionperformed (ActionEvent e)//overriding the Actionperformed method the { * intb; $A=integer.parseint (Field1.gettext ());//get the data in TextField1Panax Notoginsengb=Integer.parseint (Field2.gettext ()); -Field3.settext (string.valueof (A +b)); the } + } A ); the } + Public Static voidMain (String args[]) - { $FrameDemo2 fd=NewFrameDemo2 (); $ } -}
Operation Result:
Beginner Java Swing, has always felt that swing is useless, so there is no contact, these days Java practice, the teacher asked to write a project with Java Swing, after contact feel Swing is very good, more easy to understand than MFC.
Java Swing Simple Adder