Home arrow Java arrow Page 3 - Working with JFC/Swing Controls using NetBeans IDE
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Working with JFC/Swing Controls using NetBeans IDE - Working with text boxes, buttons, labels: understanding the code continued
(Page 3 of 6 )

This is the continuation of the previous section.  Further proceeding we have the following:

  setDefaultCloseOperation
    (javax.swing.WindowConstants.EXIT_ON_CLOSE);
  setTitle("Sample for adding two numbers");

You can observe that whatever properties you set during the design will be automatically converted to appropriate Java code.  Now we shall see how all the controls are being added to the frame.  Let us consider the following code fragment:

  getContentPane().add(txtFirst);
  txtFirst.setBounds(130, 20, 130, 20);

Our first text box is "txtFirst," and it needs to be added to the "contentpane."  The fist line in the above code fragment accomplishes this. The second line simply specifies the top, left, width and height properties of the text box.  Now, let us see how the other controls are added:

  getContentPane().add(txtSecond);
  txtSecond.setBounds(130, 50, 130, 20);

  lblEnterFirst.setText("Enter First No:");
 
getContentPane().add(lblEnterFirst);
 
lblEnterFirst.setBounds(20, 20, 90, 14);

  lblEnterSecond.setText("Enter Second No:");
  getContentPane().add(lblEnterSecond);
  lblEnterSecond.setBounds(20, 50, 100, 14);

  lblResult.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
  getContentPane().add(lblResult);
  lblResult.setBounds(130, 110, 130, 14);

You could observe that adding labels is very similar to adding text boxes except that we are specifying some caption/text for a few labels.  The last label (lblResult) was provided with a special type of border (a line border), just to make it different from the other labels.


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials