Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import javax.swing.*; import java.awt.event.*; public class EventObject extends

ID: 3535426 • Letter: I

Question

import javax.swing.*;
import java.awt.event.*;

public class EventObject extends JFrame
{
private JPanel panel;
private JButton button1;
private JButton button2;
private JButton button3;
final int WINDOW_WIDTH = 450;
final int WINDOW_HEIGHT = 450;

// CONSTTRUCTOR
public EventObject()
{
// set the title bard text
setTitle("Color window");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createPanel();

// add panel to the content pane
//add(panel);

//setVisible(true);
}
public void createPanel()
{
button1 = new JButton("red");
button2 = new JButton("Black");
button3 = new JButton("green");

// register actionListener for the buttons
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());

// create panel
panel = new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
// add
add(panel);
setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Button1"))
{
JOptionPane.showMessageDialog(null,"you clicked buttone 1");
}
else if(actionCommand.equals("Button2"))
{
JOptionPane.showMessageDialog(null, "You clikec b 2");
}
else if(actionCommand.equals("Button3"))
{
JOptionPane.showMessageDialog(null,"You clicked button3");
}
}
}
public static void main(String[] args)
{
new EventObject();
}


}


This code should give me feed back when i click button but it doesn't do anything help me..

explaination would be great...

Explanation / Answer

// the edited version of your code, which is running and giving feedback that you want,just check it


import javax.swing.*;

import java.awt.event.*;


public class EventObject extends JFrame

{

private JPanel panel;

private JButton button1;

private JButton button2;

private JButton button3;

final int WINDOW_WIDTH = 450;

final int WINDOW_HEIGHT = 450;


// CONSTTRUCTOR

public EventObject()

{

// set the title bard text

setTitle("Color window");

setSize(WINDOW_WIDTH,WINDOW_HEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

createPanel();


// add panel to the content pane

//add(panel);


//setVisible(true);

}

public void createPanel()

{

button1 = new JButton("Red");

button2 = new JButton("Black");

button3 = new JButton("Green");


// register actionListener for the buttons

button1.addActionListener(new ButtonListener());

button2.addActionListener(new ButtonListener());

button3.addActionListener(new ButtonListener());


// create panel

panel = new JPanel();

panel.add(button1);

panel.add(button2);

panel.add(button3);

// add

add(panel);

setVisible(true);

}

private class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String actionCommand = e.getActionCommand();

if(actionCommand.equals("Red"))

{

JOptionPane.showMessageDialog(null,"you clicked button 1 which is red");

}

else if(actionCommand.equals("Black"))

{

JOptionPane.showMessageDialog(null, "You clikecd button 2 which is black");

}

else if(actionCommand.equals("Green"))

{

JOptionPane.showMessageDialog(null,"You clicked button 3 which is green");

}

}

}

public static void main(String[] args)

{

new EventObject();

}



}


/*reason you were not getting correct output:---Jbutton actionCommand() methods returns a String that serves to identify the component that sent the event. By default, the action command of a JButton is the same as its label; it is included in action events, so you can use it to figure out which button an event came from.The reason you were not getting a feedback beacuse you were comparing the string returned from actionCommand() method to "Button 1", "Button 2 and "Button3 " respectively, actually the buttons were returning labels of the buttons which was "red", "green" and "blue".I just replaced the "Button 1", "Button 2 and "Button3 " with "red", "green",and "Blue".And the program is giving result that yoyu want.*/