In: Computer Science
1. Write comments on each line of the uploaded Java file.
2. Add two more controls to the Java file. These controls are listed below.
JTextField
JRadioButton
3. Excecute your program and upload the following.
1. Image showing you have added the controls
2. Your updated Java files showing your comments and your added code for above two controls.
---------------------------------------------------------------------------------------------------------------------------------
package swing;
import javax.swing.*;
public class SwingInheritance extends JFrame
{
private static final long serialVersionUID = 1L;
SwingInheritance()
{
JTextField userName = new
JTextField();
//userName.setBounds(x, y, width,
height);
userName.setBounds(80,100,100,
40);
userName.setText("Hello");
JLabel lblText = new
JLabel();
lblText.setBounds(80,150,300,
40);
lblText.setText("You Entered: " +
userName.getText());
JButton button=new
JButton("click"); //create button
button.setBounds(80,200,100,
40);
JButton close = new
JButton("Close");
close.setBounds(80,250,100,
40);
close.addActionListener(e
->
{
dispose();
});
//add to JFrame
add(button);
add(close);
add(userName);
add(lblText);
//set frame properties
setSize(400,500);
setLayout(null);
setVisible(true);
} public static void main(String[] args)
{
new SwingInheritance();
}
}
Source Code :
//used to import swing
import javax.swing.*;
//create a class SwingInheritance
//that extends JFrame
//JFrame is a container, where other components are
placed
public class SwingInheritance extends JFrame
{
private static final long serialVersionUID = 1L;
//constructor
//initialize everything when it is called
SwingInheritance()
{
//a JTextField is created
//which is referenced using 'userName'
JTextField userName = new JTextField();
//userName.setBounds(x, y, width, height);
//sets the bound in the order
x,y,width,height
userName.setBounds(80,100,100, 40);
//set the text "Hello" to JTextField
'userName'
userName.setText("Hello");
//creates a JLabel 'lblText'
JLabel lblText = new JLabel();
//set the bounds
lblText.setBounds(80,150,300, 40);
//set text to label
//userName.getText() is used to get the text entered in JTextField
userName
lblText.setText("You Entered: " + userName.getText());
//JButton is created
//it's label is "click"
JButton button=new JButton("click"); //create button
//listener is added to button
//it listens for an event such as click
button.addActionListener(e->{
//when the button is clicked
//set text inlblText
lblText.setText("You Entered: " + userName.getText());
});
//set bounds of button
button.setBounds(80,200,100, 40);
//close button is created
JButton close = new JButton("Close");
//set the bounds
close.setBounds(80,250,100, 40);
//add listener
close.addActionListener(e ->
{
//it is used to close the JFrame window
dispose();
});
//Adding two more controls
//JRadioButton
JRadioButton jRadioBtn = new JRadioButton("Radio Button");
//set the bound for radio button
jRadioBtn.setBounds(80,280,100, 40);
//JTextField
JTextField radioResult = new JTextField();
//set the bounds
radioResult.setBounds(80,348,180,80);
//add listener to jRadioBtn
jRadioBtn.addActionListener(e ->
{
//checks whether the radio button is checked
if(jRadioBtn.isSelected())
//if checked
radioResult.setText("Radio button is checked");
//checks whether the radio button is not checked
if(!jRadioBtn.isSelected())
//if not checked
radioResult.setText("Radio button is unchecked");
});
//add to JFrame
//add button to JFrame
add(button);
//add close button to JFrame
add(close);
//add JTextField userName to JFrame
add(userName);
//add label to JFrame
add(lblText);
//add JRadioButton
add(jRadioBtn);
//add jtextfield radioResult
add(radioResult);
//set frame properties
//size of the frame
setSize(400,500);
setLayout(null);
//set the visivility of frame
setVisible(true);
}
//main method
public static void
main(String[] args)
{
//call the constructor
//so everything is executed
new
SwingInheritance();
}
}
Output and screenshots :