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

I need to rewrite the following code in java, to add a group of radio buttons to

ID: 3627545 • Letter: I

Question

I need to rewrite the following code in java, to add a group of radio buttons to select background colors. The available colors are red, yellow, white, gray and green:

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

Here you are. I had to enlarge the frame size to get everything to fit. 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 JRadioButton redBtn = new JRadioButton("Red"); private JRadioButton yellowBtn = new JRadioButton("Yellow"); private JRadioButton whiteBtn = new JRadioButton("White"); private JRadioButton grayBtn = new JRadioButton("Gray"); private JRadioButton greenBtn = new JRadioButton("Green"); 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(400, 250); frame.setVisible(true); } public ButtonDemo() { // Set the background color of messagePanel messagePanel.setBackground(Color.white); // Create Panel jpButtons to hold two Buttons "" JPanel jpButtons = new JPanel(); jpButtons.add(jbtLeft); jpButtons.add(jbtRight); // birdButton.setMnemonic(KeyEvent.VK_B); redBtn.setActionCommand("red"); yellowBtn.setActionCommand("yellow"); whiteBtn.setActionCommand("white"); whiteBtn.setSelected(true); grayBtn.setActionCommand("gray"); greenBtn.setActionCommand("green"); //Group the radio buttons. JPanel radioGroup = new JPanel(); radioGroup.add(redBtn); radioGroup.add(yellowBtn); radioGroup.add(whiteBtn); radioGroup.add(grayBtn); radioGroup.add(greenBtn); //Register a listener for the radio buttons. // 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); add(radioGroup, BorderLayout.NORTH); // 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(); } }); redBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel.setBackground(Color.red); whiteBtn.setSelected(false); yellowBtn.setSelected(false); grayBtn.setSelected(false); greenBtn.setSelected(false); } }); yellowBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel.setBackground(Color.yellow); whiteBtn.setSelected(false); redBtn.setSelected(false); grayBtn.setSelected(false); greenBtn.setSelected(false); } }); whiteBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel.setBackground(Color.white); redBtn.setSelected(false); yellowBtn.setSelected(false); grayBtn.setSelected(false); greenBtn.setSelected(false); } }); grayBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel.setBackground(Color.gray); whiteBtn.setSelected(false); yellowBtn.setSelected(false); redBtn.setSelected(false); greenBtn.setSelected(false); } }); greenBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel.setBackground(Color.green); whiteBtn.setSelected(false); yellowBtn.setSelected(false); grayBtn.setSelected(false); redBtn.setSelected(false); } }); } }