Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java application that displays a JFrame containing a jPanel. The panel w

ID: 3827001 • Letter: W

Question

Write a Java application that displays a JFrame containing a jPanel. The panel will contain three push buttons labeled: Count Up, Count Down, Clear. There will also be a JLabel that indicates the current count. See listing 4.10 in the text. When the Count Up button is pushed, the count is incremented by one. When the Count Down button is pushed the count is decremented by one. When the Clear button is pushed the count is cleared to zero. The JLabel displays the current count. Allow the count to go negative if the Count Down button is pushed enough times. Play with PushCounter.java from the text to get the idea of how things work. Then add two buttons to it and modify the Listener. Implement this project in BlueJ using two classes: UpDownCounter and UpDownPanel. This is an easy modification of the book's program. But your Listener will need to detect which of the three buttons is pushed. Look at Section 5.7, Left Right. java to see how to do this. Start the program with a documentation header that says what it is, who did it, and when. Also, have some line-by-line documentation that says what is going on. The Java source files for your application. You should have two files: UpDownCounter. java and UpDownPanel. java Don't turn in the entire BlueJ project.

Explanation / Answer


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class UpDownPanel extends JFrame {
   JTextField txt = new JTextField();
   JLabel lable = new JLabel(""+UpDownCounter.getCount());
   JButton btnUp = new JButton("Up");
   JButton btnDown = new JButton("Down");
   JButton btnClear = new JButton("Clear");
   JPanel pnl = new JPanel();
public UpDownPanel() {

initUI();
}

private void initUI() {
   pnl.setLayout(null);
   pnl.setBounds(0, 0, 400, 300);
pnl.add(lable);
pnl.add(btnUp);
pnl.add(btnDown);
pnl.add(btnClear);
  
lable.setBounds(30, 100, 300, 30);
btnUp.setBounds(30, 200, 100, 30);
btnDown.setBounds(150, 200, 100, 30);
btnClear.setBounds(270, 200, 100, 30);
Font font = new Font(Font.SANS_SERIF, Font.CENTER_BASELINE, 30);
lable.setFont(font);
  
btnUp.addActionListener(new ActionListener() {
          
           @Override
           public void actionPerformed(ActionEvent arg0) {
               UpDownCounter.up();
               lable.setText(""+UpDownCounter.getCount());
           }
       });
btnDown.addActionListener(new ActionListener() {
          
           @Override
           public void actionPerformed(ActionEvent arg0) {
               UpDownCounter.down();
               lable.setText(""+UpDownCounter.getCount());
           }
       });
btnClear.addActionListener(new ActionListener() {
  
   @Override
   public void actionPerformed(ActionEvent arg0) {
       UpDownCounter.clear();
       lable.setText(""+UpDownCounter.getCount());
   }
});
  
  
add(pnl);
setTitle("UpDownCounter");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
   UpDownPanel ex = new UpDownPanel();
ex.setVisible(true);
  
}
}

class UpDownCounter{
   private static int count;

   public static int getCount() {
       return count;
   }

   public static void setCount(int count) {
       UpDownCounter.count = count;
   }
   public static void up(){
       count++;
   }
   public static void down(){
       count--;
   }
   public static void clear(){
       count = 0;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote