11_IT Unit-5 Java
11_IT Unit-5 Java
NetBeans IDE is used to create java applications very easily using the efficient GUI
builder.
JAVA SWING
Swing is a set of classes that provides more powerful and flexible components than
are possible with AWT (Abstract Windows Toolkit). It supplies buttons, chekboxes,
labels, tabbed panes, scrool pans, trees, tables, dialog boxes etc.
The swing controls/components are categorized as:
Components : Self contained graphic entity ( eg. JLabel, JBotten, JTextField etc)
Containers: Component that can hold other components (eg. JPanel, JFrame,
JDialog, JWindow)
Q&A
1. Which window is used to designed the form.
Ans: Design window
9. What is a variable?
Ans: Variables are containers used to store the values for some input, intermediate
result or the final result of an operation. The characteristics of a variable are:
It has a name.
It is capable of storing values.
It provides temporary storage.
It is capable of changing its value during program execution.
10.By default a combo box does not offer editing feature.How would you make a
combo box editable.
Ans: By setting its editable property to false.
11. Write Name the component classes of Swing API for the following
components-
a) frame
(b) button
Ans: (a) JFrame (b) JButton
16.Which method of list is used to determine the value of selected item, if only
one item is selected?
Ans: getSelectedValue()
19.Which expression is used to print the value of a variable “x” of type int.
Ans: jTextField1.setText(“x = ” + x);
28. Which process is used to translate a task into a series of commands that a
computer will use to perform that task.
Ans: Project design
29. Which of the following component is the best suited to accept the country of
the user?
A. List B Combo box C Radio button D Check box
Ans: List and combo box
30. Which construct will be used to find the sum of the first 10 natural numbers?
Ans: for loop
33. Design a GUI application with a STOP button. This application should close on
click of this button.
35. Design a GUI application to display the message "Good Morning" on the click of
the Morning button and display the message "Good Evening" on the click of the
Evening button. Add another STOP button to end the application. (Use jTextField
for output)
// Variable name for jTextField1 is txt1 and the property of txt1 is set to Not editable
text ield
// Code for Morning Button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt1.setText("Good Morning");
}
36. Design a GUI application to accept the name of the user in a Text Field placed
at the top and then display a personalized time-based greeting (greeting along
with the name of the user) in the Text Field placed at the bottom.
// Variable name for jTextField1 is txt1 and jTextField2 is txt. The property of txt2 is set
to Not editable text ield.
// Variable name for jTextField1 is txt1 and jTextField2 is txt2. Added component
buttonGroup1 display name is gender. The property of txt2 is set as Not editable text
ield.
38. Design a GUI application to display personalized message for friends and
colleagues using concatenation in a text area component.
// Variable name for jTextField1 is txt1 and jTextField2 is txt2. Also, the variable name
for jTextArea1 is txt3.
// Code for FRIENDS button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt3.setText(txt1.getText()+"and "+txt2.getText()+" are true friends. \nA true friend
encourages you to live your dreams.");
}
39. Design a GUI application which displays a simple message when the user
inputs a username and password.
40. Design a GUI application to calculate the price of fruit item based on the
following design. (Amount to be paid shall be displayed using a text ield.
// Variable name for jTextField1 is txt1 and jTextField2 is txt2.
41. Design a GUI application to calculate the bill amount based on price and
quantity as per the following layout.
// Variable name for jTextField1 is txt1, jTextField2 is txt2 and jTextField3 is txt3.
txt3.setText(Double.toString(Double.parseDouble(txt1.getText())*Double.parseDouble(t
xt2.getText())));
}
42. Develop a simple application to display the message entered by the user
surrounded by four different characters (*, #, %, “).
}
// Code for % button
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate % to the text in
char Percent;
Percent='%';
jTextField2.setText(Percent+jTextField1.getText()+Percent);
}
// Code for ‘’ button
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate " to the text in
char Quotes;
Quotes='"';
jTextField2.setText(Quotes+jTextField1.getText()+Quotes);
44. Design a simple interest calculator application based on the following layout.
45. Design a GUI application to accept a number from the user in a text ield and
print using option pane whether it is a positive even number or not.