Java - Up/Down - Please use WidgetViewer , similar solutions do not. Write a Wid
ID: 3914026 • Letter: J
Question
Java - Up/Down - Please use WidgetViewer, similar solutions do not.
Write a WidgetViewer 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.
Grading Elements
The program uses WidgetViewer to provide a GUI environment
The program displays two JButtons and two JLabels, as specified
The program uses one or more event handlers to detect user input and take appropriate action
The program generates two random numbers in the appropriate ranges when a button is clicked
The program manipulates the JLabels as specified based on the buttons clicked by the user
Explanation / Answer
As per your requirement i have written code which fulfill all your requirements please follow it step by step.
import javax.swing.*; //It is used for Swing class
import java.util.Random; //It is used for random number creation
import java.awt.event.*; //It is used for Event handling
import java.awt.BorderLayout; //It is used for layout manager
public class randUPDOWN extends JFrame
{
//Here we will creates JButton class object
JButton UpDownElement;
//Next here we will creates JLabel class object
JLabel leftLElement, leftCElement, rightCElement, rightLElement;
//Here we will creates JFrame class object
JFrame ffElement;
//Next here we will creates JPanel object
JPanel p1Element, p2Element;
randUPDOWN()
{
//Here we will reate an instance of JFrame class
ffElement = new JFrame("Up Down Operation");
//Next we will sets the layout manager to border layout
ffElement.setLayout(new BorderLayout());
//after we will initializes the JPanel class object
p1Element = new JPanel();
p2Element = new JPanel();
//Next to that initializes JLabel objects
leftLElement = new JLabel("0");
rightLElement = new JLabel("0");
leftCElement = new JLabel("Left");
rightCElement = new JLabel("Right");
//after that initializes JButton class object
UpDownElement = new JButton("go down/up");
//after that add labels to the panel 1
p1Element.add(leftCElement);
p1Element.add(leftLElement);
p1Element.add(rightCElement);
p1Element.add(rightLElement);
//After that adds button to the panel 2
p2Element.add(UpDownElement);
//Later do positions the panels
ffElement.add(p1Element, BorderLayout.NORTH);
ffElement.add(p2Element, BorderLayout.SOUTH);
ffElement.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ffElement.setSize(200,200);
ffElement.setVisible(true);
//Next perform action listener for UpDownElement button
UpDownElement.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ar0)
{
//Here we will creates the Random class object
Random randomObject = new Random();
int LowElement = 1;
int HighElement = 10;
//Here we will generates the random number
int ResultantValue = randomObject.nextInt(HighElement-LowElement) + LowElement;
int leftValue = Integer.parseInt(leftLElement.getText()) - ResultantValue;
int rightValue = Integer.parseInt(rightLElement.getText()) + ResultantValue;
leftLElement.setText(String.valueOf(leftValue));
rightLElement.setText(String.valueOf(rightValue));
}
});
}
public static void main(String[] args)
{
randUPDOWN randomObject = new randUPDOWN();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.