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.
Next: How about the JButton and its events? >>
More Java Articles
More By Jagadish Chaterjee