taryn3
16.08.2020 •
Computers and Technology
Lidia is assigning her first name as the value to a variable. Which is the correct format to use? firstName = "Lidia" First Name = "Lidia" firstName! = "Lidia" firstName = Lidia
Solved
Show answers
More tips
- A Animals and plants Money Tree Care Secrets: How to Keep Your Plant Thriving...
- F Food and Cooking Delight for Gourmets: How to Prepare Liver Pate...
- S Style and Beauty How to braid friendship bracelets?...
- H Health and Medicine Mercury Thermometer Danger: What to do when a thermometer breaks?...
- F Food and Cooking Which Calamari Salad is the Most Delicious?...
- S Society and Politics 10 Tips for Boosting Your Self-Esteem...
- F Food and Cooking The Most Delicious and Simple Fish in Batter Recipe...
- H Health and Medicine What is Autism? Understanding the Basics of This Neurodevelopmental Disorder...
- P Philosophy How to Develop Extrasensory Abilities?...
- S Style and Beauty Don t Sacrifice Your Brows: How to Properly Pluck Stubborn Hairs...
Ответ:
firstName = "Lidia"
Explanation:
you declare a variable by
variableName = value
her name is of data type string, so it should be enclosed in " "
Ответ:
Action Listener class has to present in a program only once, actionPerformed() method is overriding method also present once.
When we have multiple source of event like here button and textfield, single actionPerformed() method will provide implementation
We can detect the source from when event is fired, and accordingly write separte code for each source or same code (like here).
Hence we have to add common addActionListener to both button and textfield.
Same reading mechanism will work inside that common actionPerformed method.
Both source will fired event only if they have both bounded to addActionListener
AS our case Button will fire event and TextField will also fire event.
So both of them registered with addActionListener method.
So from question of choices
a (add an actionListener to btnRead (Among other things)
d (add an actionListener to txtNumber (Among other things)
are correct
TO VERIFY THE CONCEPT A SAMPLE PROGRAM WITH OUT PUT HAS GIVEN BELOW
*/
/* PROGRAM TO SET EVENT HANDELER TO BOTH BUTTON AND TEXTFIELD, SO BOTH oF THEM CAN FIRED EVENT TO READ TEXT VALUE*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TestInput {
JButton btnRead;
JTextField txtNumber,txtDispaly;
JFrame f;
public TestInput() //// Constructor
{
f=new JFrame();
f.setLayout(new FlowLayout()); //// SETTING FRAME TO FLOW LAYOUT
txtNumber=new JTextField("0.0",15); //// CREATED A TEXTFIELD WHEN ENTER PRESSED, IT SHOULD READ OWN VALUE
f.add(txtNumber);
btnRead=new JButton(" Read The Number "); // CREATED A BUTTON WHEN PRESSED TO READ FROM NUMBER TEXTFIELD
f.add(btnRead); // ADD BUTTON TO FRAME
txtDispaly=new JTextField("0.0",15); // VALUE TO BE DISPLAY HERE AFTER READ , EVENT FIRED EITHER BUTTON OT TEXTFIELD
f.add(txtDispaly); // ADD TEXTFIELD TO FRAME
f.setSize(300,300); // FRAME SIZE TO DISPLAY
f.setVisible(true);
ActionListener myHandler = new EventHandler(); // INSTANCE OF CUSTOME EVENT HANDELER
btnRead.addActionListener(myHandler); /// Binding the custom event handler TO BUTTON
txtNumber.addActionListener(myHandler); /// Binding event handler to TextField, so on enter key press it will also read
}
/// Define inner class as listener.
private class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// EVENT IS TRIGGERED EITHER BUTTON IS CLICKED OT TEXTFIELD IS ENTER KEY PRESSED
if(e.getSource() == btnRead || e.getSource() == txtNumber )
{
txtDispaly.setText(txtNumber.getText()); // VALUE DISPLAYED IN DISPLAY TEXTFIELD AFTRE READ FROM NUMBER TEXTFIELD
}
}
}
public static void main(String[] args)
{
new TestInput();
}
}
Explanation:
For Detailed and better Explanation the screenshot of code and output is given in the attached files. Please have a look on them.