To create a Java graphical user interface (GUI) as described, you can use the Swing library. Below is a sample code that sets up a JFrame
with a blue background and three buttons ("Yellow", "Red", and "Pink"). When you click a button, the background color of a JTextArea
in the center of the main window changes to the corresponding color.
Here's the complete code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorChanger extends JFrame {
private JTextArea textArea;
public ColorChanger() {
// Set the title of the JFrame
setTitle("Color Changer");
// Set the default close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the layout manager
setLayout(new BorderLayout());
// Create a JTextArea and set its properties
textArea = new JTextArea("Click a button to change my background color");
textArea.setEditable(false);
textArea.setBackground(Color.WHITE); // Initial background color
add(textArea, BorderLayout.CENTER);
// Create a JPanel to hold buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.BLUE); // Panel background will be blue
// Create buttons for different colors
JButton yellowButton = new JButton("Yellow");
JButton redButton = new JButton("Red");
JButton pinkButton = new JButton("Pink");
// Add action listeners for buttons
yellowButton.addActionListener(new ColorAction(Color.YELLOW));
redButton.addActionListener(new ColorAction(Color.RED));
pinkButton.addActionListener(new ColorAction(Color.PINK));
// Add buttons to the button panel
buttonPanel.add(yellowButton);
buttonPanel.add(redButton);
buttonPanel.add(pinkButton);
// Add button panel to the frame
add(buttonPanel, BorderLayout.SOUTH);
// Set the background color of the main window
getContentPane().setBackground(Color.BLUE);
// Set the window size and make it visible
setSize(400, 300);
setVisible(true);
setLocationRelativeTo(null); // Center the window on the screen
}
// Action listener class to change the background color of the JTextArea
private class ColorAction implements ActionListener {
private Color color;
public ColorAction(Color color) {
this.color = color;
}
@Override
public void actionPerformed(ActionEvent e) {
textArea.setBackground(color);
}
}
// Main method to run the application
public static void main(String[] args) {
// Use SwingUtilities to ensure thread safety
SwingUtilities.invokeLater(ColorChanger::new);
}
}
Explanation:
- JFrame: The main window which contains all the components.
- JTextArea: A text area in the center to display information and change colors.
- JPanel: A panel to hold the buttons at the bottom.
- JButton: Three buttons to change the background color to Yellow, Red, and Pink.
- ActionListener: When a button is clicked, it changes the background color of the
JTextArea
using thesetBackground()
method. - setLayout(): Used to set the layout of the
JFrame
. - setSize() and setVisible(): Methods to set the dimensions of the window and to make it visible.
To run this code:
- Copy and paste it into a Java IDE or a text file with a
.java
extension. - Compile and run the program to see the GUI in action.