redesign so each button has it’s own listener. Each listener has it’s own class.
ID: 3572830 • Letter: R
Question
redesign so each button has it’s own listener. Each listener has it’s own class. Class must implement action listener interface.
//********************************************************************
// LeftRightPanel.java Authors: Lewis/Loftus
//
// Demonstrates the use of one listener for multiple buttons.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LeftRightPanel extends JPanel
{
private JButton left, right;
private JLabel label;
private JPanel buttonPanel;
//-----------------------------------------------------------------
// Constructor: Sets up the GUI.
//-----------------------------------------------------------------
public LeftRightPanel()
{
left = new JButton("Left");
right = new JButton("Right");
ButtonListener listener = new ButtonListener();
left.addActionListener(listener);
right.addActionListener(listener);
label = new JLabel("Push a button");
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(200, 40));
buttonPanel.setBackground(Color.blue);
buttonPanel.add(left);
buttonPanel.add(right);
setPreferredSize(new Dimension(200, 80));
setBackground(Color.cyan);
add(label);
add(buttonPanel);
}
//*****************************************************************
// Represents a listener for both buttons.
//*****************************************************************
private class ButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Determines which button was pressed and sets the label
// text accordingly.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == left)
label.setText("Left");
else
label.setText("Right");
}
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LeftRightPanel extends JPanel
{
private JButton left, right;
private JLabel label;
private JPanel buttonPanel;
//-----------------------------------------------------------------
// Constructor: Sets up the GUI.
//-----------------------------------------------------------------
public LeftRightPanel()
{
left = new JButton("Left");
right = new JButton("Right");
LeftClickListener leftClickListener = new LeftClickListener();
left.addActionListener(leftClickListener);
RightClickListener rightClickListener = new RightClickListener();
right.addActionListener(rightClickListener);
label = new JLabel("Push a button");
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(200, 40));
buttonPanel.setBackground(Color.blue);
buttonPanel.add(left);
buttonPanel.add(right);
setPreferredSize(new Dimension(200, 80));
setBackground(Color.cyan);
add(label);
add(buttonPanel);
}
private class LeftClickListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
label.setText("Left");
}
}
private class RightClickListener implements ActionListener{
public void actionPerformed(ActionEvent event)
{
label.setText("Right");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.