Question: 1. Consider the following code snippet which is supposed to show the total order amount when the button is clicked: public static void main (String[]

1. Consider the following code snippet which is supposed to show the total order amount when the button is clicked:

public static void main (String[] args)

{

Jframe frame = new JFrame();

final Order myOrder = newOrder();

JButton button = new JButton("Calculate");

final JLabel label = new JLabel("Total amount due");

JPanel panel = new JPanel();

panel.add(button);

panel.add(label);

class MyListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

label.setText("Total amount due" + myOrder.getAmountDue());

}

}

ActionListener listener = new MyListener();

panel.addActionListener(listener);

frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

What is wrong with this code?

A. panel should be declared as final.

B. The listener has been attached to the panel.

C. The listener cannot access the methods of the myOrder object.

D. The button and label will appear on top of each other.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!