Java Gui Programming Using Swing i Solved Assignment Ip Class 11
Short Description
Java Gui Programming Using Swing i Solved Assignment Ip Class 11...
Description
JAVA GUI PROGRAMMING USING SWING-I Type A: Very Short Short Answer Answer Questions Questions 1
What is GUI programming?
Ans.
GUI stands for Graphical User Interface. A program's graphical user interface presents an easy -to-use visual display to the user. It is made up of graphical components (e.g., buttons, buttons, labels, windows). windows).
2.
What is Java JFC?
Ans.
Swing API is part of a larger family of java products known as Java Foundation Classes (JFC).
3
How is Swing related to GUI pr programming?
Ans.
The swing toolkit, shipped as part of java SE platform, includes a rich set of graphical components for building GUIs and adding interactivity to java applications.
4
What is container component?
Ans.
A container component is a special type of c omponent that can hold other components. Some swing containers are JPanel, JFrame etc.
5
Name some top level containers.
Ans.
JFrame, JDialog, JApplet
6
Name some special purpose containers.
Ans.
JInternalFrame, JLayeredPane, RootPane
7
What is an event? What is event handler?
Ans.
Event is an occurrence of an activity. Event handler is a method that understands the event and processes it. The event handler method takes the event object as a parameter.
8
What is layout manager?
Ans.
Layout manager is a way to control arrangement, position and size of visual components on a GUI form.
9 Ans.
Name Name at leas leastt thre three e Eve Event nt type typess that that can can occ occur ur duri durin ng an appl applic icat atio ion n run run.. 1. Window Ev E vents 2. Action Ev E vents 3. Mouse Ev E vents
10
What What is defa defaul ultt lay layou outt man manag ager er for for a fra frame me and and for for a pan panel el? ?
Ans.
Border er Layo Layout ut and and pane Flow Layout Layout frame –Bord panell –Flow
11
What What is is the the defa defaul ultt name name of actio action n even eventt hand handle lerr of of a but butto ton n nam namel ely y Test TestBi Bin? n?
Ans. Ans.
Tes TestBin tBinAc Acti tio onPer nPerfforme ormed d()is the default name of action event handler of a button namely TestBin.
12
Why Why are are GUI GUI com compo pone nent ntss tha thatt you you add add to your your appl applic icat atio ion n are are terme termed d as as obj objec ects ts? ?
Ans.
GUI components which we add to our application is termed as object because all basically are classes and when we add those component netbeans automatically create object of that r espective class.
13
Name some properties of JFrame object. ct.
Ans.
title, cursor, iconImage, resizeable, defaultCloseOPeration.
14
Name so some properties of of JP JPanel ob object. ct.
Ans.
background, border, foreground, toolTipText, preferredSize.
15
Name Name some some prop roperti ertie es of JButt Butto on obje bject. ct.
Ans.
actionCommand, background, border, font, foreground, icon,text.
16
What What prop proper erty ty woul would d you you set set to assi assign gn acce access ss key key to to a but butto ton? n?
Ans.
mnemonic
17
What What does does getA getAct ctio ion nCom Comman mand() d() meth metho od do?
http://cbsecsnip.in
Page 1 of 6
Ans.
Returns the string set in actionCommand property of the button.
18
Which method can programmatically mimic the click action of a push button?
Ans.
doClick() method can programmatically mimic the click action of a push button.
Type B: Short Answer Questions 1. Ans.
2 Ans.
Discuss briefly about JFC and its constituent APIs. JFC expands to Java Foundation Classes. JFC is a suit of five libraries designed to assist programmers in creating enterprise applications. 1. Swing API 2. AWT (Abstract Window Toolkit) 3. Accessibility API 4. 2D API 5. Drag and Drop API Differentiate between a container and a component.
container
component
Container is a window-like component that can contain A component is an object, like a button or a scroll bar. other components. Examples of containers are JPanel, JFrame, JApplet.
3 Ans.
4 Ans
5 Ans.
6
Examples of components are JLabel, JTextField, JButton.
What are various categories of Swing controls? Give examples for each of them. Following are the various categories of Swing controls: 1. Basic Control –JButton, JCheckBox, JComboBox, JList, JMenu, JRadioButton, JSlider, JSpinner, JTextField, JPasswordField. 2. Interactive Displays –JcolorChooser, JEditorPane, JTextPane, JFileChooser, JTable, JTextArea, JTree. 3. Uneditable Information Displays –Jlabel, JProgressBar, JSeparator, JToolTip. 4. Top-Level Container –Japplet, JDialog, JFrame. 5. General-Purpose Container –Jpanel, JScrollPane, JSplitPane, JTabbedPane, JToolBar. 6. Special-purpose Container – JinternalFrame, JLayeredPane, RootPane Write steps to create a GUI application in java. 1. Define a container to hold components. Examples of containers are : JFrame, Jpanel, JDialog etc. 2. Add required GUI components/widgets to the container. Example of GUI components are : JButton, JLabel, JTextField etc. 3. Use Layout Manager to determine position and size of components. 4. Add action to GUI by adding event listeners to GUI components. 5. Save and test run your GUI application. Discuss briefly about different layout managers offered by Swing API. Flow Layout: Default layout manager. It lays out the components from left to right Border Layout: It has five areas for holding components: north, east, west, south and center. When there are only 5 elements to be placed, border layout is t he right choice. Grid Layout: Grid Layout places components in a grid of equally sized cells, adding them to the grid from left to right and top to bottom. Card Layout: Different components at different times are laid out. Controlled by a combo box, determining which panel the Card layout displays. Box Layout: Box Layout allows multiple components to be arranged either vertically or horizontally, but not both. Components managed by Box Layout are arranged from le ft to right or top to bottom in the order they are added to the container. Spring Layout: Spring Layout do their job by defining directional relationship or constraints between the edges if components. Spring Layout is very low level Layout Suggest the layouts for the following scenarios.
http://cbsecsnip.in
Page 2 of 6
Ans.
1. A space hungry component is to be added at the centre of the frame. 2. Components should appear in a row. 3. Components should appear in rows and columns. 1. Border Layout 2. Flow Layout 3. Grid Layout How does Java handle events? Java handles the event by using 3 components - Event Source, Event Handler and Event Listener. When the user clicks on the event source which can be a button, ActionEvent event is generated. Then the ActionEvent object is created and linked to the listener which is ActionListener for processing. The Listener object contains the actionPerformed() method which is event handler and this event handler processes the ActionEvent and our code is executed Which event listeners would respond to following actions? 1. User moves the mouse 2. User presses a key on keyboard 3. User clicks a button 4. A component loses focus 5. Window is closed. 1. Mouse Listener 2. Key Listener 3. Action Listener 4. Focus Listener 5. Window Listener What are top-level containers? What is their significance? 1. The top level containers are JFrame, JDialog, JApplet. 2. Top level containers and all its child components can be moved, hidden, shown, etc. in a single operation e.g., if you move a container then its child components also move with it; if you hide a container then all of its child controls also get hidden. Name component classes for the following GUI components. 1. Text field 2. Text area 3. Label 4. Panel 5. Button 1. JTextField 2. JTextArea 3. JLabel 4. JPanel 5. JButton
11
Write code for both the buttons of following GUI application.
Ans.
7 Ans.
8
Ans. 9 Ans.
10
Button2 Button1 Full Form Label
Ans.
//Button1 jTextField2.setText("Non-resident Indian");
//Button2 jTextField2.setText("Central Board for Secondary Education");
12
Write code for event handler of each of the buttons in following application:
Button1
IntialValue
Button2
Increment
Button3
Decrement
Button1 Button2
CalcLabel
Initializes the value of label to 0 (zero) Increments the value of label by 1 (one)
http://cbsecsnip.in
Page 3 of 6
Button3 Ans.
Decrements the value of label by 1 (one)
int i=0;
// Button 1 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { i=0; jLabel1.setText(Integer.toString(i)); }
//Button 2 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { i=i+1; jLabel1.setText(Integer.toString(i)); }
//Button 3 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { i=i-1; jLabel1.setText(Integer.toString(i)); }
TYPE C : Practical/Lab Questions 1
Design a GUI applicaton having an interface as shown below:
Text Area
Label 1
Label 2
Ans.
1. When user clicks on a button, Lable1 lists an image/picture of that tourist place. 2. The Text Area lists the package details for the selected tourist place. 3. Label2 shows some interesting information about the selected tourist place. // Shimla Button jTextArea1.setText(""); jLabel1.setIcon(new ImageIcon("Mention You image file name with its path ")); jLabel2.setText("Shimla, is the capital city of the Indian state of Himachal Pradesh, located in northern India. "); jTextArea1.append("Package"+'\n');
//Darjeeling Button jTextArea1.setText(""); jLabel1.setIcon(new ImageIcon("Mention You image file name with its path")); jLabel2.setText("Darjeeling is a town in the Indian state of West Bengal."); jTextArea1.append("Package"+'\n');
//Srinagar Button jTextArea1.setText(""); jLabel1.setIcon(new ImageIcon("Mention You image file name with its path")); jLabel2.setText("Srinagar is the most populous district in the state of Jammu and Kashmir,Indian Administered Kashmir and is home to the summer capital of
http://cbsecsnip.in
Page 4 of 6
Jammu and Kashmir."); jTextArea1.append("Package"+'\n');
// Kodaikanal Button jTextArea1.setText(""); jLabel1.setIcon(new ImageIcon("Mention You image file name with its path ")); jLabel2.setText("Kodaikanal is a city in the hills of the taluk division of the Dindigul district in the state of Tamil Nadu, India. "); jTextArea1.append("Package"+'\n');
// Pachmarhi Button jTextArea1.setText(""); jLabel1.setIcon(new ImageIcon("Mention You image file name with its path ")); jLabel2.setText("Pachmarhi is a hill station in Madhya Pradesh state of central India, also known for the Pachmarhi Cantonment. It is widely known as Satpura ki Rani, situated at a height of 1100 m in a valley of the Satpura Range in Hoshangabad district. "); jTextArea1.append("Package"+'\n');
2
An institute offers MCA cources of duration 3 years. The interface given below is designed to capture the marks secured by student in the semester exams. Your help is sought to enhance the interface so that it is able to: 1. When user clicks the button Total, the total marks and the percentage of marks secured by the student should be displayed in text txtTotal and txtPercent respectively. (Assume that the marks in each semester are out of 600)
2. When the user clicks on the button Report, the final repirt should be displayed in the label lblResult in the following format: Total marks = ... and Percentage =… % //Total Button private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int m1=Integer.parseInt(jTextField2.getText()); int m2=Integer.parseInt(jTextField3.getText()); int m3=Integer.parseInt(jTextField4.getText()); int m4=Integer.parseInt(jTextField5.getText()); int m5=Integer.parseInt(jTextField6.getText()); int m6=Integer.parseInt(jTextField7.getText()); int total=m1+m2+m3+m4+m5+m6; float per=total/36; jTextField8.setText(Integer.toString(total)); jTextField9.setText(Float.toString(per)); }
//Report Button private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
http://cbsecsnip.in
Page 5 of 6
int total=Integer.parseInt(jTextField8.getText()); float per=Float.parseFloat(jTextField9.getText()); jLabel8.setText("Total marks = "+total+" and Percentage ="+per+"%"); }
3
Design an application having interface as shown below:
Text area
Ans.
When the user clicks Submit button after entering data in textfields, all the entered information should be appended to text area. Recall that information can be appended to a text area using method: .append (“ info_to_be appended”); // SUBMIT BUTTON private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextArea1.append("Name :" + jTextField1.getText() + "\n"); jTextArea1.append("House No :" + jTextField2.getText() + "\n"); jTextArea1.append("Building :" + jTextField3.getText() + "\n"); jTextArea1.append("Street :" + jTextField4.getText() + "\n"); jTextArea1.append("City :" + jTextField5.getText() + "\n"); jTextArea1.append("State :" + jTextField6.getText() + "\n"); jTextArea1.append("info to be appended" + "\n\n") }
http://cbsecsnip.in
Page 6 of 6
View more...
Comments