Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a WidgetView GUI that has the following widgets: a button labeled go up/down a label initialized to 0 (we'll call this the left label)

Write a WidgetView 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.

Please use the following code to set up. This is a simple GUI, so any advanced code for a beginner java class is unneccessary. Make as simple as possible.

WidgetViewerActionEvent.java

import java.awt.event.ActionListener;

public abstract class WidgetViewerActionEvent implements ActionListener {

}

WidgetViewer.java

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;

import javax.swing.JButton;

/**

* A really simple class to display Swing widgets in absolute Layout GUI.

*

*

* @author parks

*/

public class WidgetViewer {

/**

* This is the width of the JFrame if no value is specified on the constructor

*/

public static final int DEFAULT_X_SIZE = 600;

/**

* This is the height of the JFrame if no value is specified on the constructor

*/

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 Absolute layout

* JPanel as its content pane and initialize the frame to a default size.

*/

public WidgetViewer() {

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 WidgetViewer(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();

JComponent jbx = (JComponent) e.getSource();

anchor.remove(jbx);

};

jframe = new JFrame();

anchor = new JPanel();

anchor.setLayout(null);

jframe.setContentPane(anchor);

jframe.setSize(pixelSizeInX, pixelSizeInY);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jframe.setVisible(true);

}

/**

* Add a Swing widget to the GUI.

* GUI coordinates start at the top left corner of the frame, and

* are measured in pixels.

*

*

x increases horizontally to (to the right)

*

y increases vertically GOING DOWN.

*

*

* @param jcomp

* Swing widget (subclasses of JComponent--like JLabel and

* JTextField) to be added to the JFrame

*

* @param x the x value of this widget's top left corner

* @param y the y value of this widget's top left corner

* @param w the width, in pixels, of this widget

* @param h the height, in pixels, of this widget

*/

public void add(JComponent jcomp, int x, int y, int w, int h) {

jcomp.setBounds(x, y, w, h);

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, absButton.getText().length() + 2);

}

/**

* Convenience method to create a JButton with the given text and use it

* in an addAndWait.

*

* @param prompt Text for the JButton to display

*/

public void addButtonAndWait(String prompt) {

JButton jb = new JButton(prompt);

addAndWait(jb);

}

/**

* 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, jTextField.getText().length() + 2);

}

@SuppressWarnings("unused")

private void addWait(JComponent jcomp) {

addWait(jcomp, 5);

}

private void addWait(JComponent jcomp, int charWidth) {

int guessAtWidth = Math.min(charWidth * 10, jframe.getWidth());

add(jcomp, 0, 10, guessAtWidth, 20);

lock.lock();

try {

while (!userClicked) {

waitingForUser.await();

}

// we need this to make the just clicked widget disappear in some circumstances

jframe.setContentPane(anchor);

} catch (InterruptedException e1) {

System.err.println("WidgetViewer reports that something really bad just happened");

e1.printStackTrace();

System.exit(0);

}

userClicked = false;

waitingForUser.signalAll();

lock.unlock();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

which 2 batch actions can be performed in the customers center in

Answered: 1 week ago