Question

Write a program in Java (using swing) that opens a window that contains a button. The...

Write a program in Java (using swing) that opens a window that contains a button. The button is labled with "0" (zero).

With every click on the button, the number in its label increments by 1.

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//ButtonClickCounter.java

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

public class ButtonClickCounter extends JFrame implements ActionListener {

          // declaring button

          private JButton button;

          // constructor to initialize the GUI

          public ButtonClickCounter() {

                   // initializing button with value 0

                   button = new JButton("0");

                   // adding action listener to the buttton

                   button.addActionListener(this);

                  

                   //using flow layout as the layout manager

                   setLayout(new FlowLayout());

                  

                   //adding button to the frame

                   add(button);

                  

                   // setting up and displaying the window

                   setDefaultCloseOperation(EXIT_ON_CLOSE);

                   setSize(200, 100);

                   setVisible(true);

          }

          public static void main(String[] args) {

                   // initializing the GUI

                   new ButtonClickCounter();

          }

          @Override

          public void actionPerformed(ActionEvent e) {

                   //getting the current number from the button as an integer

                   int current = Integer.parseInt(button.getText());

                   //incrementing it

                   current++;

                   //updating the button with new number

                   button.setText("" + current);

          }

}

/*OUTPUT*/

/* after some clicks */

Add a comment
Know the answer?
Add Answer to:
Write a program in Java (using swing) that opens a window that contains a button. The...
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
  • Write a Java program that constructs a window having the title "The Hello Application - Ver....

    Write a Java program that constructs a window having the title "The Hello Application - Ver. 1.0" . Make sure that your program contains a window closing event object that correctly closes the window when the Close (X) button is clicked

  • Using IntelliJ create a Java GUI window program that contains all the interface components covered in...

    Using IntelliJ create a Java GUI window program that contains all the interface components covered in the lecture. JPanel - Green with Yellow text JButton - four buttons JLabel - four labels JTextField - three text fields JPasswordField - one is fine JCheckBox - four different birds JComboBox - four different flowers JList - three different bikes JSlider - goes up to max 100 at 10 increments JMenu - contains File with pull down options-> save -> save all

  • Java Write an application using the FileInputStream that OPENS a file which contains the name of...

    Java Write an application using the FileInputStream that OPENS a file which contains the name of the user's favorite book and then DISPLAYS it to the user. IF the file does not exist, PROMPT the user for the book's title, and then WRITE it to the file by using a FileOutputStream. Save the file as BookApplication.java.

  • Re-write this program into a javafx program, using javafx components only. NO awt or swing. import...

    Re-write this program into a javafx program, using javafx components only. NO awt or swing. import java.awt.Button; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; public class DecimalBinary { public static void main(String[] args) { JFrame frame=new JFrame(); //Create a frame frame.setTitle("Decimal-Binary Converter"); //Set its label frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); //Set size fo frame JPanel panel=new JPanel(); //Create a panel to contain controls frame.add(panel); //add panel to frame panel.setLayout(null); Label decimalLabel=new Label("Decimal"); //Create label for decimal decimalLabel.setBounds(10,...

  • Please code the following Java App using Swing No additional classes are required. With that in...

    Please code the following Java App using Swing No additional classes are required. With that in mind ... Write a program that emulates a calculator. Create a Label with a title of "Super Calculator", a textbox, where the numbers are displayed, a series of buttons labeled as shown, and a “Clear” button. As the user enters the numbers, pressing the number buttons, display the numbers right justified to the textbox. When the user press any one of the operators, +,...

  • Write a Java program to make a grid of 10x10 matrices with "0 & 1" generated...

    Write a Java program to make a grid of 10x10 matrices with "0 & 1" generated randomly that lets the user click the Refresh button

  • Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints th...

    Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints the string in reverse order. (Tip: Once the string has been entere aved, retrieve and display characters starting from the end of the string). Write a Java program that has 3 buttons. Clicking the first button shows the message "See no evil", clicking the second button shows the message "Hear no evil", clicking the third button shows the message "Speak...

  • Write a program to play "Three Button Monte." Your program should draw three buttons labeled "Door...

    Write a program to play "Three Button Monte." Your program should draw three buttons labeled "Door 1' " " Door 2' " and "Door 3" in a window and randomly select one of the buttons (without telling the user which one is selected) . The program then prompts the user to click on one of the buttons. A click on the special button is a win, and a click on one of the other two is a loss. You should...

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • In Java, Write a program in Java FX or Swing that sets up a game of...

    In Java, Write a program in Java FX or Swing that sets up a game of concentration(Memory Matching Card Game), first shuffle the cards well and then place eachcard face down in 4 rows of 13 cards each. Each player takes a turn by turning two cards over. If the cards match, then the player picks up the cards and keeps them. If they don't match, the player turns the cards back over. I need the code! You have to...

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