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

a. Write an application that extends JFrame and that displays a phrase in one co

ID: 3765679 • Letter: A

Question

a. Write an application that extends JFrame and that displays a phrase in one color the first time the user clicks a JButton. The next time the user clicks the JButton, make the first phrase seem to disappear. (Hint: Redraw it using the background color.) At the same time, draw the phrase again in a different color, size, and horizontal position. The third click should change the color, size, and position of the phrase again. Save the file as JChangeSizeAndColor.java

b. Modify the JChangeSizeAndColor application so that it continuously changes the size, color, and location of a phrase as long as the user continues to click the button. Save the application as JChangeSizeAndColor2.java

Explanation / Answer

JChangeSizeAndColor.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class JChangeSizeAndColor extends JFrame implements ActionListener{
   private JTextField textField;
   private JButton button;
   private int index;
   public JChangeSizeAndColor() {
       initialize();
   }
   private void initialize() {
       index = 0;
       textField = new JTextField("This is a phrase");
       textField.setForeground(Color.WHITE);
       button = new JButton("Click Me!");
       button.addActionListener(this);
       setLayout(new BorderLayout());
       add(textField, BorderLayout.CENTER);
       add(button, BorderLayout.SOUTH);
   }
   @Override
   public void actionPerformed(ActionEvent e) {
       if (index == 0) {
           textField.setForeground(Color.BLACK);
           Font font = new Font(Font.SERIF, Font.BOLD, 22);
           textField.setFont(font);
       }
      
       else if (index == 1) {
           textField.setForeground(Color.RED);
           Font font = new Font(Font.SERIF, Font.BOLD, 12);
           textField.setFont(font);
           add(textField, BorderLayout.NORTH);
          
       }
      
       else if (index == 2) {
           textField.setForeground(Color.GREEN);
           Font font = new Font(Font.SERIF, Font.BOLD, 17);
           textField.setFont(font);
       }
       index++;
   }
   public static void main(String[] args) {
       JChangeSizeAndColor thisFrame = new JChangeSizeAndColor();
       thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       thisFrame.setSize(600,600);
       thisFrame.setVisible(true);
   }

}

JChangeSizeAndColor2.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class JChangeSizeAndColor2 extends JFrame implements ActionListener{
   private JTextField textField;
   private JButton button;
   private int index;
   private Color[] colors = {Color.blue, Color.red, Color.cyan, Color.darkGray, Color.green, Color.yellow, Color.black};
   public JChangeSizeAndColor2() {
       initialize();
   }
   private void initialize() {
       index = 0;
       textField = new JTextField("This is a phrase");
       textField.setForeground(Color.BLACK);
       button = new JButton("Click Me!");
       button.addActionListener(this);
       setLayout(new BorderLayout());
       add(textField, BorderLayout.CENTER);
       add(button, BorderLayout.SOUTH);
   }
   @Override
   public void actionPerformed(ActionEvent e) {
       textField.setForeground(colors[index]);
       Font font = new Font(Font.SERIF, Font.BOLD, getRandomSize());
       textField.setFont(font);
       index++;
       index = index % 7;
   }
   public static void main(String[] args) {
       JChangeSizeAndColor2 thisFrame = new JChangeSizeAndColor2();
       thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       thisFrame.setSize(600,600);
       thisFrame.setVisible(true);
   }
   private int getRandomSize() {
       Random rand = new Random();
       return rand.nextInt(30) + 10;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote