Find any errors in each of the following lines of code, and explain how to corre
ID: 3730416 • Letter: F
Question
Find any errors in each of the following lines of code, and explain how to correct them 1. import java.swing.JFrame //import JFrame 2. container.add(okButton, WEST); //add OkButton to BorderLayout's West region 3 container.setLayout(new FlowLayout(FlowLayout.DEFAULT); I/set up to FlowLayout's default aslignment 4. JLabel firstLabel, JLabel; //create two references of JLabel 5. objectokButton JButton("Ok") 6. catch (NumberFormatException n, ArithmeticException e) //handle two exceptions inside one catch block 7·JTextArea ttArea-new JTextArea(20, 5); /create a textarea with 20 columns and 5 rows 8. The following is an inner class that will be registered as an action listener for a JButton component: private class ButtonListener public void actionPerformedot //code appears here)Explanation / Answer
1. import java.swing.JFrame // import JFrame Answer : Jframe does't exist into the java.swing package but this is in javax.swing package Correct : import javax.swing.JFrame; 2. container.add(okButton, WEST) // ok button to burder layout Answer : Error in WEST there is should be an index or object if we want to use layout then it should be like container.add(okButton, BorderLayout.WEST); otherwise Correct syntex : container.add(Component comp, int index) 3. container.setLayout(new FlowLayout(FlowLayout.RIGHT)); // set up to default Answer : Error in Flowlayout.RIGHT there is no such variable in flowlayout. Variables in Flowlayout : public static final int LEFT public static final int RIGHT public static final int CENTER public static final int LEADING public static final int TRAILING foe example : container.setLayout(new FlowLayout(FlowLayout.RIGHT)); 4. JLabel firstLAbel,JLabel // create two instances Answer : Error in creating instance in JLabel because this is name of class. This should not as class name. So use Jlabell firstLabel,jlabel; 5. objectokButton = JButton("Ok") Answer : Button is creating using new keyword. Correct : objectokButton = new JButton("Ok"); 6. catch(NumberFormatException n, ArithmeticException e) // handle two exceptions Answer : Syntex error in catching two exception. Correct : catch(NumberFormatException | ArithmeticException e) 7. JTextArea tstArea = new JTextArea(10,5); //create textarea with 20 col 5 row Answewr: Wrong paramerters pass first parameter is row second is columns Correct : JTextArea tstArea = new JTextArea(5,10); 8. Error in method. actionPerformed method should have an ActionEvent class object. Correct : private class ButtonListner implements ActionListener { public void actionPerformed(ActionEvent e) { // code } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.