Question

1. Create an app with a button and a text field. Display the current count in...

1. Create an app with a button and a text field. Display the current count in the text field (display 0 when the app loads). CODE EACH OF THESE SUB QUESTIONS AS SEPARATE APPS!

a. Count up to 20, then automatically start back at 1 (instead of reaching 21).

b. Count up by one for each button press for the first 10 presses, then count up by 2 for the next 10 presses, 3 for the next 10, and so on.

c. Count up using the Fibonacci sequence. This sequence starts at 1. On the first press add 1, then 2, then 3, then 5, then 8, and so on.

d. Count up from 1 to 10 then back down from 10 to 1, adding or subtracting 1 for each button press. When it hits 1, start counting back up again.

*GUI in Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//PressButton.java

// *******************************************

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Extending Frame and Implementing
// ActionListener and Runnbale for Thread
public class PressButton extends Frame implements Runnable,ActionListener{

   //Declaration of Buttons,TextFields and Variables
   Button b1;
   TextField t1;
   int sum=1;
   Thread th; //Declaration of thread
   int count=0,count1=1;
   static int i=1;
   //Constructor
   public PressButton(){
           t1=new TextField("0");
   t1.setBounds(50,50, 150,20);
   b1=new Button("Press");
   b1.setBounds(50,100,60,30);
   add(t1);add(b1);
   //Window Closing Adapter method
   addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent we){
           dispose();
       }
           });
   //Button Action
   b1.addActionListener(this);
   }
  
   public static void main(String[] args) {
       //Creating a Frame
       PressButton pb = new PressButton();
       pb.setSize(300, 300);
       pb.setVisible(true);
       pb.setTitle("Press Button");
       pb.setLayout(new FlowLayout());
   }

   //Action Performed when button clicks
   public void actionPerformed(ActionEvent ae) {
       th = new Thread(this); // Thread is initializing
       th.start(); //thread is started with run() when button clicks
   }


   //Task-A Get back to 1 when 20 reaches
  
   public void run() {
       if(sum==20){
           sum = 1;
           t1.setText(new Integer(sum).toString());
       }
       else{  
           sum = Integer.parseInt(t1.getText().toString())+1;
           t1.setText(new Integer(sum).toString());
       }
   }

   //Task-B Count by 10..1,2,3...
   /*public void run(){
       if(count%10==0 && count>0){
           i++;
       }
       sum = Integer.parseInt(t1.getText().toString())+i;
       t1.setText(new Integer(sum).toString());
       count++;
   }*/
  
  
   //Task-C Fibonacci Count
  
   /*public void run(){
       sum = count+count1;
       //sum = Integer.parseInt(t1.getText().toString())+i;
       t1.setText(new Integer(sum).toString());
       count=count1;
       count1=Integer.parseInt(t1.getText().toString());
   }*/
  
  
   //Task-D Adding or Substracting
  
   /*public void run(){
       if(count<10){
           sum = Integer.parseInt(t1.getText().toString())+1;
           t1.setText(new Integer(sum).toString());
       }else{
           sum = Integer.parseInt(t1.getText().toString())-1;
           t1.setText(new Integer(sum).toString());
           if(sum==1){
               count=0;
           }
       }
       count++;
   }*/
}

// **************************************

//Output:

Note: From the above program, i have created four run() methods for four tasks as specified as A,B,C and D

So, please uncomment any one of the tasks to see output individually. Never uncomment more than one.

Add a comment
Know the answer?
Add Answer to:
1. Create an app with a button and a text field. Display the current count in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT