Rewrite the below programto add a group of radio buttons to select background co
ID: 3607845 • Letter: R
Question
Rewrite the below programto add a group of radio buttons to select background colors. Theavailable colors are red, yellow, white, gray, andgreen.
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 toJava");
// Declare two buttons to movethe message left and right
private JButton jbtLeft = new JButton("<=");
private JButton jbtRight = newJButton("=>");
public static void main(String[]args) {
ButtonDemo frame = new ButtonDemo();
frame.setTitle("ButtonDemo");
frame.setLocationRelativeTo(null); // Center theframe
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setVisible(true);
} //main
public ButtonDemo() { //Constructor
// Set the background color of messagePanel
messagePanel.setBackground(Color.white);
// Create PaneljpButtons to hold two Buttons "<=" and "right =>"
JPanel jpButtons = new JPanel();
jpButtons.setLayout(new FlowLayout());
jpButtons.add(jbtLeft);
jpButtons.add(jbtRight);
// Set keyboardmnemonics
jbtLeft.setMnemonic('L'); //press Alt+L to move left
jbtRight.setMnemonic('R'); // press Alt+Lto move right
// Set icons andremove text
// jbtLeft.setIcon(newImageIcon("image/left.gif"));
// jbtRight.setIcon(newImageIcon("image/right.gif"));
// jbtLeft.setText(null);
// jbtRight.setText(null);
// Set tool tip texton the buttons
jbtLeft.setToolTipText("Move message toleft");
jbtRight.setToolTipText("Move message toright");
// Place panels inthe frame
setLayout(new BorderLayout());
add(messagePanel, BorderLayout.CENTER);
add(jpButtons,BorderLayout.SOUTH);
// Register listenerswith the buttons
jbtLeft.addActionListener (new ActionListener(){
public voidactionPerformed(ActionEvent e) {
messagePanel.moveLeft();
}
});
jbtRight.addActionListener (new ActionListener() {
public voidactionPerformed(ActionEvent e) {
messagePanel.moveRight();
}
});
}
}
Explanation / Answer
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JRadioButtonDemo extends JPanel {static JFrame frame;RadioListener myListener = null;static JRadioButton red_radio,yellow_radio,white_radio,gray_radio,green_radio;public JRadioButtonDemo() {// Create the radio buttons and assign Keyboard shortcuts using// Mnemonicsred_radio = new JRadioButton("Red");red_radio.setMnemonic(KeyEvent.VK_N);red_radio.setActionCommand("Red");red_radio.setSelected(true);yellow_radio = new JRadioButton("Yellow");yellow_radio.setMnemonic(KeyEvent.VK_A);yellow_radio.setActionCommand("Yellow");white_radio = new JRadioButton("white");white_radio.setMnemonic(KeyEvent.VK_S);white_radio.setActionCommand("White");gray_radio = new JRadioButton("gray");gray_radio.setMnemonic(KeyEvent.VK_S);gray_radio.setActionCommand("Gray");green_radio = new JRadioButton("green");green_radio.setMnemonic(KeyEvent.VK_S);green_radio.setActionCommand("Green");// Group the radio buttons.ButtonGroup group = new ButtonGroup();group.add(red_radio);group.add(yellow_radio);group.add(white_radio);group.add(gray_radio);group.add(green_radio);// Register an action listener for the radio buttons.myListener = new RadioListener();red_radio.addActionListener(myListener);gray_radio.addActionListener(myListener);green_radio.addActionListener(myListener);yellow_radio.addActionListener(myListener);white_radio.addActionListener(myListener);// Set up the picture label// Set the Default Image// Put the radio buttons in a column in a panelJPanel jplRadio = new JPanel();jplRadio.setLayout(new GridLayout(0, 1));jplRadio.add(red_radio);jplRadio.add(yellow_radio);jplRadio.add(white_radio);jplRadio.add(gray_radio);jplRadio.add(green_radio);setLayout(new BorderLayout());add(jplRadio, BorderLayout.WEST);setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));}/** Listens to the radio buttons. */class RadioListener implements ActionListener {public void actionPerformed(ActionEvent e) {String s = e.getActionCommand();if(s== "Red")setBackground (Color.red);elseif(s=="Yellow")setBackground (Color.yellow);else if(s=="White")setBackground (Color.white);else if(s=="Gray")setBackground (Color.gray);else if(s=="Green")setBackground (Color.green);System.out.println(s);}}public static void main(String s[]) {frame = new JFrame("JRadioButton Usage Demo");frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});frame.getContentPane().add(new JRadioButtonDemo(),BorderLayout.CENTER);frame.pack();frame.setVisible(true);}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.