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

Write a Java program that uses a JFrame-based window to present ten JTextField o

ID: 3546851 • Letter: W

Question

Write a Java program that uses a JFrame-based window to present ten JTextField objects and a JButton labeled "New Numbers". The text field objects initially show the value zero. If the user presses the "New Numbers" button then ten random numbers are generated and placed in the text fields. All randomly generated numbers are in the range 1 to 100. Any values less than fifty are displayed with a white background. Any values greater than or equal to fifty are displayed with a red background (the red and white backgrounds refer to the background color of the each individual JTextField components).

Note that when you set the text of a text field component you must give the "setText" method a String value. If "x" is an integer variable and you have a JTextField reference named "text", then

will not work since "x" is an int and not a String. One way to convert an int value to a String value is via the Integer class' toString method:

When the application is started all of the text fields are zero:

When the user presses the "New Numbers" button then all of the text fields get new random numbers. Large numbers (greater than or equal to fifty) are highlighted in red, the other numbers have white backgrounds:

Each time the "New Numbers" button is pressed new numbers are generated:

If you would like to add some more functionality (just for fun), then you could add a "Big numbers" check box. If the check box is selected then the big numbers are highlighted in red. If the box is not selected, then all numbers, big and small, and shown with the same white background.

You can use the following code to start your project:

Always solve the problem one small step at a time. For example, you could

Explanation / Answer

TenNumbers class:


public class TenNumbers {


public static void main(String[] args) {

ManyNumbers mn = new ManyNumbers("Alan Jackson - Program 6");

}

}


ManyNumbers class:

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JTextField;


class ManyNumbers extends JFrame implements ActionListener {


public static final int MAX_NUM = 10;

JTextField[] nums = new JTextField[MAX_NUM];

JButton newNumber;

JCheckBox bigNumbers;


public ManyNumbers(String s) {

super(s);

setLayout(new FlowLayout());


//setting the textfields with 0s

for (int i = 0; i < MAX_NUM; i++) {

nums[i] = new JTextField(10);

nums[i].setText(Integer.toString(0));

this.getContentPane().add(nums[i]);

}


//adding the new number button

newNumber = new JButton("New Numbers");

this.getContentPane().add(newNumber);

newNumber.setActionCommand("newNumber");

newNumber.addActionListener(this);


bigNumbers = new JCheckBox("Big Numbers");

this.getContentPane().add(bigNumbers);


// You can use "pack" instead of "setSize" -- pack makes

// the window just the right size for the added components.

this.setSize(700, 300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


public void actionPerformed(ActionEvent e) {

if ("newNumber".equals(e.getActionCommand())) {


int randomNums[] = new int[MAX_NUM];


//calling the method getRandomNums which returns array of 10 random numbers

randomNums = getRandomNums();



for (int i = 0; i < MAX_NUM; i++) {

if (randomNums[i] >= 50) {

if (bigNumbers.isSelected()) {

nums[i].setBackground(Color.red);//set color red if number >= 50

}

else

nums[i].setBackground(Color.white);

} else {

nums[i].setBackground(Color.white);//else set white

}

nums[i].setText(Integer.toString(randomNums[i]));//print the random number

}




}

}


//method getRandomNums which returns array of 10 random numbers

public int[] getRandomNums() {

int random[] = new int[MAX_NUM];

Random randomGenerator = new Random();

for (int i = 0; i < MAX_NUM; i++) {

random[i] = randomGenerator.nextInt(101);

}

return random;

}

}

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