SlideShare a Scribd company logo
1 of 3
Enhance the ButtonViewer program so that it prints the time at which the button was clicked.
This is the ButtonViewer I have:
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Click Listener:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
An action listener that prints a message.
*/
public class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
Solution
Answer for Question:
I have updated the code to print the time when button was clicked this is the below code in
I have ran it it's working fine.
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( "I was clicked at :"+sdf.format(cal.getTime()) );
}
}
public class ButtonViewer
{
private static final int FRAME_WIDTH = 100;
private static final int FRAME_HEIGHT = 60;
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
frame.add(button);
ActionListener listener = new ClickListener();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

More Related Content

Similar to Enhance the ButtonViewer program so that it prints the time at which t.docx

PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDchessvashisth
 
10 awt event model
10 awt event model10 awt event model
10 awt event modelBayarkhuu
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorialsumitjoshi01
 
This program create a Java Frame with 5 rows of button with capti.pdf
 This program create a Java Frame with 5 rows of button with capti.pdf This program create a Java Frame with 5 rows of button with capti.pdf
This program create a Java Frame with 5 rows of button with capti.pdfannamalaicells
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonEric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonMatthew McCullough
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
Program klik sederhana
Program klik sederhanaProgram klik sederhana
Program klik sederhanaHenfry Kai
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdffashionfolionr
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 

Similar to Enhance the ButtonViewer program so that it prints the time at which t.docx (20)

PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
 
10 awt event model
10 awt event model10 awt event model
10 awt event model
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Swing
SwingSwing
Swing
 
ch20.pptx
ch20.pptxch20.pptx
ch20.pptx
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
This program create a Java Frame with 5 rows of button with capti.pdf
 This program create a Java Frame with 5 rows of button with capti.pdf This program create a Java Frame with 5 rows of button with capti.pdf
This program create a Java Frame with 5 rows of button with capti.pdf
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Program klik sederhana
Program klik sederhanaProgram klik sederhana
Program klik sederhana
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
Event handling
Event handlingEvent handling
Event handling
 

More from todd401

Exercise 7-15 During October- Wichita Light Company experiences the fo.docx
Exercise 7-15 During October- Wichita Light Company experiences the fo.docxExercise 7-15 During October- Wichita Light Company experiences the fo.docx
Exercise 7-15 During October- Wichita Light Company experiences the fo.docxtodd401
 
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docx
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docxExercise 4-3 (Part Level Submission) The adjusted trial balance column.docx
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docxtodd401
 
examples of how being familiar with and following digital forensic bes.docx
examples of how being familiar with and following digital forensic bes.docxexamples of how being familiar with and following digital forensic bes.docx
examples of how being familiar with and following digital forensic bes.docxtodd401
 
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docx
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docxEst 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docx
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docxtodd401
 
examples of materials that have directional properties as a single cry.docx
examples of materials that have directional properties as a single cry.docxexamples of materials that have directional properties as a single cry.docx
examples of materials that have directional properties as a single cry.docxtodd401
 
Examine the importance for business financial leaders to recognize the.docx
Examine the importance for business financial leaders to recognize the.docxExamine the importance for business financial leaders to recognize the.docx
Examine the importance for business financial leaders to recognize the.docxtodd401
 
Examine the primary reasons why project management causes a cultural c.docx
Examine the primary reasons why project management causes a cultural c.docxExamine the primary reasons why project management causes a cultural c.docx
Examine the primary reasons why project management causes a cultural c.docxtodd401
 
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docx
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docxFal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docx
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docxtodd401
 
Everything compiles and everything runs correctly except my insertionS.docx
Everything compiles and everything runs correctly except my insertionS.docxEverything compiles and everything runs correctly except my insertionS.docx
Everything compiles and everything runs correctly except my insertionS.docxtodd401
 
Explain three advantages and one disadvantage to CONSUMER from standar.docx
Explain three advantages and one disadvantage to CONSUMER from standar.docxExplain three advantages and one disadvantage to CONSUMER from standar.docx
Explain three advantages and one disadvantage to CONSUMER from standar.docxtodd401
 
Explain the various reasons why atoms and molecules form chemical bond.docx
Explain the various reasons why atoms and molecules form chemical bond.docxExplain the various reasons why atoms and molecules form chemical bond.docx
Explain the various reasons why atoms and molecules form chemical bond.docxtodd401
 
Explain (in your own words) the concept colbertism and what-'s interes.docx
Explain (in your own words) the concept colbertism and what-'s interes.docxExplain (in your own words) the concept colbertism and what-'s interes.docx
Explain (in your own words) the concept colbertism and what-'s interes.docxtodd401
 
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docx
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docxExplain the 3 Emissive flat-panel devices- electroluminescent- plasma.docx
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docxtodd401
 
Experimental Questions 1- Which types of compounds have the strongest.docx
Experimental Questions 1- Which types of compounds have the strongest.docxExperimental Questions 1- Which types of compounds have the strongest.docx
Experimental Questions 1- Which types of compounds have the strongest.docxtodd401
 
Explain how dopants affect the conductivity of a solid- how one can ma.docx
Explain how dopants affect the conductivity of a solid- how one can ma.docxExplain how dopants affect the conductivity of a solid- how one can ma.docx
Explain how dopants affect the conductivity of a solid- how one can ma.docxtodd401
 
Explain how SSL protocol is used to ensure the confidentiality and int.docx
Explain how SSL protocol is used to ensure the confidentiality and int.docxExplain how SSL protocol is used to ensure the confidentiality and int.docx
Explain how SSL protocol is used to ensure the confidentiality and int.docxtodd401
 
explain how revenues and expenditures are classified in the General Fu.docx
explain how revenues and expenditures are classified in the General Fu.docxexplain how revenues and expenditures are classified in the General Fu.docx
explain how revenues and expenditures are classified in the General Fu.docxtodd401
 
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docx
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docxExperiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docx
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docxtodd401
 
Explain covalent bond- Explain Metallic bond and what important proper.docx
Explain covalent bond- Explain Metallic bond and what important proper.docxExplain covalent bond- Explain Metallic bond and what important proper.docx
Explain covalent bond- Explain Metallic bond and what important proper.docxtodd401
 
English Read the sentences- Which sentence contains a nonrestrictive.docx
English Read the sentences-  Which sentence contains a nonrestrictive.docxEnglish Read the sentences-  Which sentence contains a nonrestrictive.docx
English Read the sentences- Which sentence contains a nonrestrictive.docxtodd401
 

More from todd401 (20)

Exercise 7-15 During October- Wichita Light Company experiences the fo.docx
Exercise 7-15 During October- Wichita Light Company experiences the fo.docxExercise 7-15 During October- Wichita Light Company experiences the fo.docx
Exercise 7-15 During October- Wichita Light Company experiences the fo.docx
 
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docx
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docxExercise 4-3 (Part Level Submission) The adjusted trial balance column.docx
Exercise 4-3 (Part Level Submission) The adjusted trial balance column.docx
 
examples of how being familiar with and following digital forensic bes.docx
examples of how being familiar with and following digital forensic bes.docxexamples of how being familiar with and following digital forensic bes.docx
examples of how being familiar with and following digital forensic bes.docx
 
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docx
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docxEst 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docx
Est 12-16 AM edugen-wileyplus-com Open Assignment t experience please.docx
 
examples of materials that have directional properties as a single cry.docx
examples of materials that have directional properties as a single cry.docxexamples of materials that have directional properties as a single cry.docx
examples of materials that have directional properties as a single cry.docx
 
Examine the importance for business financial leaders to recognize the.docx
Examine the importance for business financial leaders to recognize the.docxExamine the importance for business financial leaders to recognize the.docx
Examine the importance for business financial leaders to recognize the.docx
 
Examine the primary reasons why project management causes a cultural c.docx
Examine the primary reasons why project management causes a cultural c.docxExamine the primary reasons why project management causes a cultural c.docx
Examine the primary reasons why project management causes a cultural c.docx
 
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docx
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docxFal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docx
Fal or - Seawat A pur ture Chapter Review Speaking1- How is a mixture.docx
 
Everything compiles and everything runs correctly except my insertionS.docx
Everything compiles and everything runs correctly except my insertionS.docxEverything compiles and everything runs correctly except my insertionS.docx
Everything compiles and everything runs correctly except my insertionS.docx
 
Explain three advantages and one disadvantage to CONSUMER from standar.docx
Explain three advantages and one disadvantage to CONSUMER from standar.docxExplain three advantages and one disadvantage to CONSUMER from standar.docx
Explain three advantages and one disadvantage to CONSUMER from standar.docx
 
Explain the various reasons why atoms and molecules form chemical bond.docx
Explain the various reasons why atoms and molecules form chemical bond.docxExplain the various reasons why atoms and molecules form chemical bond.docx
Explain the various reasons why atoms and molecules form chemical bond.docx
 
Explain (in your own words) the concept colbertism and what-'s interes.docx
Explain (in your own words) the concept colbertism and what-'s interes.docxExplain (in your own words) the concept colbertism and what-'s interes.docx
Explain (in your own words) the concept colbertism and what-'s interes.docx
 
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docx
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docxExplain the 3 Emissive flat-panel devices- electroluminescent- plasma.docx
Explain the 3 Emissive flat-panel devices- electroluminescent- plasma.docx
 
Experimental Questions 1- Which types of compounds have the strongest.docx
Experimental Questions 1- Which types of compounds have the strongest.docxExperimental Questions 1- Which types of compounds have the strongest.docx
Experimental Questions 1- Which types of compounds have the strongest.docx
 
Explain how dopants affect the conductivity of a solid- how one can ma.docx
Explain how dopants affect the conductivity of a solid- how one can ma.docxExplain how dopants affect the conductivity of a solid- how one can ma.docx
Explain how dopants affect the conductivity of a solid- how one can ma.docx
 
Explain how SSL protocol is used to ensure the confidentiality and int.docx
Explain how SSL protocol is used to ensure the confidentiality and int.docxExplain how SSL protocol is used to ensure the confidentiality and int.docx
Explain how SSL protocol is used to ensure the confidentiality and int.docx
 
explain how revenues and expenditures are classified in the General Fu.docx
explain how revenues and expenditures are classified in the General Fu.docxexplain how revenues and expenditures are classified in the General Fu.docx
explain how revenues and expenditures are classified in the General Fu.docx
 
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docx
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docxExperiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docx
Experiment# Kinetics of an Iodine Clock Reaction 1- A color change wil.docx
 
Explain covalent bond- Explain Metallic bond and what important proper.docx
Explain covalent bond- Explain Metallic bond and what important proper.docxExplain covalent bond- Explain Metallic bond and what important proper.docx
Explain covalent bond- Explain Metallic bond and what important proper.docx
 
English Read the sentences- Which sentence contains a nonrestrictive.docx
English Read the sentences-  Which sentence contains a nonrestrictive.docxEnglish Read the sentences-  Which sentence contains a nonrestrictive.docx
English Read the sentences- Which sentence contains a nonrestrictive.docx
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 

Enhance the ButtonViewer program so that it prints the time at which t.docx

  • 1. Enhance the ButtonViewer program so that it prints the time at which the button was clicked. This is the ButtonViewer I have: import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; /** This program demonstrates how to install an action listener. */ public class ButtonViewer { private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } Click Listener: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** An action listener that prints a message. */ public class ClickListener implements ActionListener
  • 2. { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } } Solution Answer for Question: I have updated the code to print the time when button was clicked this is the below code in I have ran it it's working fine. import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Calendar; class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); System.out.println( "I was clicked at :"+sdf.format(cal.getTime()) ); } }
  • 3. public class ButtonViewer { private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }