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

rewrite current listing exercise 17.2 to add a group of radio buttons to select

ID: 3528433 • Letter: R

Question

rewrite current listing exercise 17.2 to add a group of radio buttons to select background colors. tha available colors are red, yellow, white, gray, and green. here is the code that i have to rewrite, import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.*; public class ButtonDemo extends JFrame { // Create a panel for displaying message protected MessagePanel messagePanel = new MessagePanel("Welcome to Java"); // Declare two buttons to move the message left and right private JButton jbtLeft = new JButton("<="); private JButton jbtRight = new JButton("=>"); public static void main(String[] args) { ButtonDemo frame = new ButtonDemo(); frame.setTitle("ButtonDemo"); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 100); frame.setVisible(true); } public ButtonDemo() { // Set the background color of messagePanel messagePanel.setBackground(Color.white); // Create Panel jpButtons to hold two Buttons "<=" and "right =>" JPanel jpButtons = new JPanel(); jpButtons.add(jbtLeft); jpButtons.add(jbtRight); // Set keyboard mnemonics jbtLeft.setMnemonic('L'); jbtRight.setMnemonic('R'); // Set icons and remove text // jbtLeft.setIcon(new ImageIcon("image/left.gif")); // jbtRight.setIcon(new ImageIcon("image/right.gif")); // jbtLeft.setText(null); // jbtRight.setText(null); // Set tool tip text on the buttons jbtLeft.setToolTipText("Move message to left"); jbtRight.setToolTipText("Move message to right"); // Place panels in the frame setLayout(new BorderLayout()); add(messagePanel, BorderLayout.CENTER); add(jpButtons, BorderLayout.SOUTH); // Register listeners with the buttons jbtLeft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { messagePanel.moveLeft(); } }); jbtRight.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { messagePanel.moveRight(); } }); } }

Explanation / Answer

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;


public class ButtonDemo extends JFrame

{

// Create a panel for displaying message

protected MessagePanel messagePanel = new MessagePanel("Welcome to Java");

// Declare two buttons to move the message left and right

private JButton jbtLeft = new JButton("<=");

private JButton jbtRight = new JButton("=>");

ButtonGroup buttonGroup = new ButtonGroup();

private JRadioButton _jrbred = new JRadioButton("Red");

private JRadioButton _jrbYellow = new JRadioButton("Yellow");

private JRadioButton _jrbWhite = new JRadioButton("White");

private JRadioButton _jrbGray = new JRadioButton("Gray");

private JRadioButton _jrbGreen = new JRadioButton("Green");

public static void main(String[] args)

{

ButtonDemo frame = new ButtonDemo();

frame.setTitle("Radio Buttons Demo!!");

frame.setLocationRelativeTo(null);

// Center the frame

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 400);

frame.setVisible(true);

}

public ButtonDemo()

{

// Set the background color of messagePanel

messagePanel.setBackground(Color.white);

// Create Panel jpButtons to hold two Buttons "<=" and "right =>"

JPanel jpButtons = new JPanel();

jpButtons.add(jbtLeft);

jpButtons.add(jbtRight);

// Set keyboard mnemonics

jbtLeft.setMnemonic('L');

jbtRight.setMnemonic('R');

// Set icons and remove text

// jbtLeft.setIcon(new ImageIcon("image/left.gif"));

// jbtRight.setIcon(new ImageIcon("image/right.gif"));

// jbtLeft.setText(null);

// jbtRight.setText(null);

// Set tool tip text on the buttons

jbtLeft.setToolTipText("Move message to left");

jbtRight.setToolTipText("Move message to right");

// Place panels in the frame

setLayout(new BorderLayout());

add(messagePanel, BorderLayout.CENTER); add(jpButtons, BorderLayout.SOUTH);

// Register listeners with the buttons

jbtLeft.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

messagePanel.moveLeft();

}

});

jbtRight.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

messagePanel.moveRight();

}

});

buttonGroup.add(_jrbred);

buttonGroup.add(_jrbGray);

buttonGroup.add(_jrbGreen);

buttonGroup.add(_jrbWhite);

buttonGroup.add(_jrbYellow);

jpButtons.add(_jrbred);

jpButtons.add(_jrbGray);

jpButtons.add(_jrbGreen);

jpButtons.add(_jrbWhite);

jpButtons.add(_jrbYellow);

}

}


Note: Let me kow, if we need to add action listeners to these radi buttons