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

In a controller class, the variable radio1 references a RadioButton component, a

ID: 3884238 • Letter: I

Question

In a controller class, the variable radio1 references a RadioButton component, and outputLabel references a Label component. Write an if statement that determines whether the RadioButton is selected. If the RadioButton is selected, display “Selected” in the outputLabel. Otherwise, display “Not selected” in the outputLabel. In a controller class, the variable radio1 references a RadioButton component, and outputLabel references a Label component. Write an if statement that determines whether the RadioButton is selected. If the RadioButton is selected, display “Selected” in the outputLabel. Otherwise, display “Not selected” in the outputLabel.

Explanation / Answer

Please find Programe

package com;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class Controller extends JFrame {
private JRadioButton radio1 = new JRadioButton();
private JLabel label = new JLabel();

public Controller() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize( new Dimension(400, 300) );
radio1.setText("");
radio1.setBounds(new Rectangle(45, 90, 86, 18));
radio1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JRadioButton){
JRadioButton radioButton = (JRadioButton) e.getSource();
if(radioButton.isSelected()){
label.setText("Selected");
}
else {
label.setText("Not Selected");
}
}
}
});
label.setText("Not Selected");
label.setBounds(new Rectangle(40, 30, 120, 35));
this.getContentPane().add(label, null);
this.getContentPane().add(radio1, null);
}

public static void main(String args[]){
Controller fm=new Controller();
fm.isFocused();
fm.setVisible(true);
}
  

  
}