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

UpDown.java Please help me the JAVA program. WidgetView.java is given below. Ple

ID: 3779644 • Letter: U

Question

UpDown.java

Please help me the JAVA program. WidgetView.java is given below. Please use WidgetView.java for your program. Please don't change WidgetView.java
Write a GUI that has the following widgets:

a button labeled "go up/down"

a label initialized to 0 (we'll call this the left label)

a label initialized to 0 (we'll call this the right label)

a button labeled "go down/up"


When the "go up/down" button is pushed, a random number between 1 and 10 (inclusive) is generated and added to the left label, and another random number between 1 and 10 (inclusive) is generated and subtracted from the right label.


When the "go down/up" button is pushed, a random number between 1 and 10 (inclusive) is generated and subtracted from the left label, and another random number between 1 and 10 (inclusive) is generated and added to the right label.

WidgetView.java - please use this program for display Swing widgets in a FlowLayout GUI.

import java.awt.event.ActionListener;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
* A really simple class to display Swing widgets in a FlowLayout GUI.
*


*
* @author parks
*/
public class WidgetView {
  
   public static final int DEFAULT_X_SIZE = 600;
   public static final int DEFAULT_Y_SIZE = 400;

private JFrame jframe;
private JPanel anchor;
private boolean userClicked = false;

private Lock lock;
private Condition waitingForUser;

private JComponent userInputComponent = null;
private ActionListener eventHandler;

/**
* Default constructor will display an empty JFrame that has a Flow layout
* JPanel as its content pane and initialize the frame to a default size.
*/
public WidgetView() {
   this(DEFAULT_X_SIZE, DEFAULT_Y_SIZE);
}
  
/**
* Constructor will display an empty JFrame that has a Flow layout
* JPanel as its content pane and initialize the frame to a given size.
*/
public WidgetView(int pixelSizeInX, int pixelSizeInY) {
lock = new ReentrantLock();
waitingForUser = lock.newCondition();
// lambda expression requires Java 8
eventHandler = e -> {
if (e.getSource() != userInputComponent) {
return;
}
lock.lock();
userClicked = true;
waitingForUser.signalAll();
lock.unlock();
};

/* java 7 solution
* eventHandler = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() != userInputComponent) {
return;
}
lock.lock();
userClicked = true;
waitingForUser.signalAll();
lock.unlock();
}
};
*/

jframe = new JFrame();
anchor = new JPanel();
jframe.setContentPane(anchor);
jframe.setSize(pixelSizeInX, pixelSizeInY);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}

/**
* Add a Swing widget to the GUI.
*
* @param jcomp Swing widget (subclasses of JComponent--like JLabel and
* JTextField) to be added to the JFrame
*/
public void add(JComponent jcomp) {
anchor.add(jcomp);
jframe.setContentPane(anchor);
}

/**
* Add an Abstract Button (like a JButton) to the JFrame. Create an action
* listener to wait (suspend the caller) until it is clicked.
*
* @param absButton Button (like a JButton) to add to the JFrame
*/
public void addAndWait(AbstractButton absButton) {
userInputComponent = absButton;
absButton.addActionListener(eventHandler);

addWait(absButton);
}

/**
* Add a JTextField to the JFrame, and wait for the user to put the cursor
* in the field and click Enter. The caller is suspended until enter is
* clicked.
*
* @param jTextField Field to add to the JFrame
*/
public void addAndWait(JTextField jTextField) {
userInputComponent = jTextField;
jTextField.addActionListener(eventHandler);

addWait(jTextField);
}

private void addWait(JComponent jcomp) {
add(jcomp);
lock.lock();
try {
while (!userClicked) {
waitingForUser.await();
}
}
catch (InterruptedException e1) {
System.err.println("WidgetView reports that something really bad just happened");
e1.printStackTrace();
System.exit(0);
}
userClicked = false;
waitingForUser.signalAll();
lock.unlock();
}
}

------------------------------------------------------

WidgetViewActionEvent.java

import java.awt.event.ActionListener;

public abstract class WidgetViewActionEvent implements ActionListener {

}

------------------------------------------------------

===================

here is example program, may help you how WidgetView and WidgetViewActionEvent work:

Piggy.java

ublic class Piggy {
  
   private int quarter;
   private int dime;
  
   public Piggy() {
       quarter = 0;
       dime = 0;
   }
  
   public void addDime() {
       dime++;
   }
  
   public void addQuarter() {
       quarter++;
   }
  
   public int getValue() {
       return quarter * 25 + dime * 10;
   }
  
   public String toString() {
       return getValue() + " cents, quarter: " + quarter + " dime: " + dime;
   }

}

==========

GuiPiggy.java

import javax.swing.*;
import java.awt.event.ActionEvent;


public class GuiPiggy {

private Piggy bank;
private JTextArea history;
private JButton dime;
private JButton quarter;

public GuiPiggy(Piggy piggyBank) {
bank = piggyBank;
WidgetView wv = new WidgetView();

dime = new JButton("add dime");
dime.addActionListener(new ButtonAction(bank));
quarter = new JButton("add quarter");
quarter.addActionListener(new ButtonAction(bank));
history = new JTextArea();

wv.add(dime);
wv.add(quarter);
wv.add(history);
}

class ButtonAction extends WidgetViewActionEvent{

private Piggy myBank;

ButtonAction(Piggy bank){
myBank = bank;
}
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == dime){
myBank.addDime();

}
else {
myBank.addQuarter();
}
history.append(" " + bank.toString());
}
}

}

============

PiggyGuiMain.java


public class PiggyGuiMain {

   public static void main(String[] args) {

       new GuiPiggy(new Piggy());
   }

}

---------------------------------------

Note to expert: I already gave you WidgetView, WidgetViewActionEvent. please don't change those programs.

-------------------------------------------

PiggyGuiMain.java, GuiPiggy.java and Piggy.java are just example programs. I just post here for you know how WidgetView and WidgetViewActionEvent work.

Thank you for your help.

Explanation / Answer

code for UpDown.java class

package swingexample;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.border.Border;

public class UpDown {

//create labels

private JLabel left_label;

private JLabel right_label;

  

//create buttons

private JButton up_down;

private JButton down_up;

  

public UpDown(){

//object of WidgetView class is created

WidgetView wv = new WidgetView();

  

//intialize button Up/down and add to Action class i.e. buttonAction

up_down = new JButton("Up/Down");

up_down.addActionListener(new ButtonAction());

  

//intialize button down/up and add to Action class i.e. buttonAction

down_up = new JButton("Down/Up");

down_up.addActionListener(new ButtonAction());

  

//intialize label leftLabel

left_label = new JLabel();

left_label.setText(Integer.toString(0));

  

//intialize label righttLabel

right_label = new JLabel();

right_label.setText(Integer.toString(0));

  

//add buttons up/down and down/up in widgetView

wv.add(up_down);

wv.add(down_up);

//add labels left and right in widgetView

wv.add(left_label);

wv.add(right_label);

  

}

class ButtonAction extends WidgetViewActionEvent {

Random randomno1 = new Random();

Random randomno2 = new Random();

int num1,num2;

public ButtonAction() //constructor of buttonAction class

{

//creating random number

num1 =randomno1.nextInt(10);

num2 =randomno2.nextInt(10);

}

//when any button is clicked

@Override

public void actionPerformed(ActionEvent e) {

//check whether button up/down is clicked

if (e.getSource() == up_down){

int temp_left=Integer.parseInt(left_label.getText()); //get value from left label

int newValL=num1+temp_left; // random value is added

left_label.setText(Integer.toString(newValL)); // set value to the left label

int temp_right=Integer.parseInt(right_label.getText()); //get value from right label

int newValR=num2-temp_right; //random value is subtracted

right_label.setText(Integer.toString(newValR)); //set value to right label

}

//check whether button down/up is clicked

else if(e.getSource() == down_up){

int temp_left=Integer.parseInt(left_label.getText());//get value from left label

int newValL=num1-temp_left; // random value is subtracted

left_label.setText(Integer.toString(newValL)); //set value to left label

int temp_right=Integer.parseInt(right_label.getText());//get value from right label

int newValR=num2+temp_right; // random value is added

right_label.setText(Integer.toString(newValR)); //set value to right label

}

}

}

/**

*main method

* @param args the command line arguments

*/

public static void main(String[] args) {

new UpDown();// creating object of updown class

}

}

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