import java.awt.event.ActionListener; import java.util.concurrent.locks.Conditio
ID: 3788433 • Letter: I
Question
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.
* <p>
*
* @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();
}
}
Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TestView {
public static Random numGen =new Random();
public static int RandNum(){
int rand = Math.abs((1)+numGen.nextInt(9));
return rand;
}
public static void main(String[]Args){
final int x = RandNum();
final int y= RandNum();
final WidgetView wigView = new WidgetView();
JComponent compX = new JLabel("Value of x : ");
compX.setBounds(50,50, 150,20);
JComponent compXVal = new JLabel(Integer.toString(x));
compXVal.setBounds(200,50, 150,20);
wigView.add(compX);
wigView.add(compXVal);
JComponent compY = new JLabel("Value of y : ");
compY.setBounds(50,100, 150,20);
JComponent compYVal = new JLabel(Integer.toString(y));
compYVal.setBounds(200,100, 250,20);
wigView.add(compY);
wigView.add(compYVal);
JComponent compOption = new JLabel("Enter an operation number");
compOption.setBounds(50,150, 200,20);
wigView.add(compOption);
final JTextField compUserInput = new JTextField();
compUserInput.setBounds(250,150, 150, 20);
wigView.addAndWait(compUserInput);
final JTextField compResult = new JTextField("Result :");
compResult.setBounds(50,280, 200,20);
compResult.setEnabled(false);
compResult.setVisible(false);
wigView.add(compResult);
/*JComponent compResultF = new JLabel(Integer.toString(Operation));
compResultF.setBounds(250,280, 200,20);
wigView.add(compResult);
*/
JButton compButton = new JButton("Press");
compButton.setBounds(80, 200, 300, 40);
compButton.setVisible(true);
compButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int Operation = Integer.parseInt(compUserInput.getText());
int Result;
float result;
if(Operation>=1 && Operation <=10){
Result = x+y;
compResult.setVisible(true);
compResult.setText("Result is : "+ Result);
}else if(Operation % 4 == 0){
Result = x-y;
compResult.setVisible(true);
compResult.setText("Result is : "+ Result);
}else if(Operation % 5 == 0){
Result = y/x;
compResult.setVisible(true);
compResult.setText("Result is : "+ Result);
}else if(Operation % 2 == 0){
result = y/x;
compResult.setVisible(true);
compResult.setText("Result is : "+ result);
}else{
Result = x*y;
compResult.setVisible(true);
compResult.setText("Result is : "+ Result);
}
}
});
wigView.addAndWait(compButton);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
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.
* <p>
*
* @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);
jframe.setLayout(null);
}
/**
* 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();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.