Question

In: Computer Science

1. Write comments on each line of the uploaded Java file. 2. Add two more controls...

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();
   }
}
  

Solutions

Expert Solution

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 :


Related Solutions

This has to be in java and will be uploaded onto zybooks. Write a Java program...
This has to be in java and will be uploaded onto zybooks. Write a Java program that asks the user for a date entered as 4 integers: dayNumber monthNumber date year. Where: dayNumber An integer from 1-7, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday monthNumber An integer from 1-12, where 1 = January, 2 = February, ..., 12 = December date An integer from 1-31 representing the date. year An integer representing the year. Your prompt...
Write a Java application "WellFormedExpression.java". Start with the file attached here. Please read the comments and...
Write a Java application "WellFormedExpression.java". Start with the file attached here. Please read the comments and complete the function 'isWellFormed()'. It also has the main() to test the function with several input strings. Feel free to change and add more tests with different kinds of input data for correct implementation. Note that you need MyStack.java and Stackinterface.java in the same package. WellFormedExpression.java package apps; public class WellFormedExpression {    static boolean isWellFormed(String s) {        /**** Create a stack...
2. Create a java program that reads a file line by line and extract the first...
2. Create a java program that reads a file line by line and extract the first word.        The program will ask for the name of input and output file. Input file: words.txt today tomorrow sam peterson peter small roy pratt Output file: outwords.txt tomorrow peterson small pratt
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
It's java file 1. declare an vector without specifying the size 2. use push_back to add...
It's java file 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector 3. display the smallest, largest, and average of the numbers in the vector
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it. The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms: Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT