Question: I'm writing a program using GUI, radio buttons to change color of the background and text. I'm running to some error as I'm compiling it.
I'm writing a program using GUI, radio buttons to change color of the background and text. I'm running to some error as I'm compiling it. here are some errors I got from Java:
Exception in thread "main" java.lang.NullPointerException
at ColorFactory.buildTopPanel(ColorFactory.java:64)
at ColorFactory.
at ColorFactory.main(ColorFactory.java:133)
Please check my program.
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorFactory extends JFrame{
private final int WINDOW_WIDTH = 500; //window width
private final int WINDOW_HEIGHT= 300; //window height
private JButton redButton; //change background color to red
private JButton orangeButton; // change background color to orange
private JButton yellowButton; //change background color to yellow
private JRadioButton greenButton; //change text color to green
private JRadioButton blueButton; //change text color to blue
private JRadioButton cyanButton; //change text color to cyan
private ButtonGroup radioButtonGroup; //to group radio buttons
private JPanel topPanel; // top panel that holds 3 buttons
private JPanel bottomPanel; // bottom panel that holds radio buttons
private JLabel messageLabel; //A message to user
//constructor
public ColorFactory () {
//set title
setTitle("Color Factory");
//set size
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
//specify an action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set Layout
setLayout(new BorderLayout());
//build the top panel and add it to the frame
buildTopPanel();
add(topPanel, BorderLayout.NORTH);
//build the bottom panel and ad it to the frame
buildBottomPanel();
add(bottomPanel, BorderLayout.SOUTH);
//create a label that contains the message
messageLabel = new JLabel ("Top buttons change the panel color and bottom raido buttons change the text color");
add(messageLabel, BorderLayout.CENTER);
//display the window
setVisible(true);
}
private void buildTopPanel() {
//set Flow Layout
setLayout (new FlowLayout());
//set background
setBackground(Color.WHITE);
//create the label for buttons and set button's background
redButton = new JButton ("Red");
redButton.setBackground(Color.RED);
orangeButton = new JButton ("Orange");
orangeButton.setBackground(Color.ORANGE);
yellowButton = new JButton ("Yellow");
yellowButton.setBackground(Color.YELLOW);
//set action command
redButton.setActionCommand("R");
orangeButton.setActionCommand("O");
yellowButton.setActionCommand("Y");
// add listener
redButton.addActionListener(new RedButtonListener());
orangeButton.addActionListener (new OrangeButtonListener());
yellowButton.addActionListener (new YellowButtonListener());
topPanel.add(redButton);
topPanel.add(orangeButton);
topPanel.add(yellowButton);
}
private void buildBottomPanel() {
//set Flow Layout
setLayout (new FlowLayout());
//set background
setBackground(Color.WHITE);
//create label for radio buttons and set buttons' foreground
greenButton = new JRadioButton ("Green");
greenButton.setForeground(Color.GREEN);
blueButton = new JRadioButton ("Blue");
blueButton.setForeground(Color.BLUE);
cyanButton = new JRadioButton ("Cyan");
cyanButton.setForeground(Color.CYAN);
// grouping radio button
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(greenButton);
radioButtonGroup.add(blueButton);
radioButtonGroup.add(cyanButton);
//add action listeners to the radio buttons
greenButton.addActionListener(new RadioButtonListener());
blueButton.addActionListener(new RadioButtonListener());
cyanButton.addActionListener(new RadioButtonListener());
}
/**
* private inner classes that handle the event when the user click Red, Orange, Yellow buttons
*/
private class RedButtonListener implements ActionListener{
public void actionPerformed (ActionEvent e) {
messageLabel.setBackground(Color.RED);
}
}
private class OrangeButtonListener implements ActionListener{
public void actionPerformed (ActionEvent e) {
messageLabel.setBackground(Color.ORANGE);
}
}
private class YellowButtonListener implements ActionListener{
public void actionPerformed (ActionEvent e) {
messageLabel.setBackground(Color.YELLOW);
}
}
/**
* private inner class that handle the event when the user choose one of the radio buttons
*/
private class RadioButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == greenButton) {
messageLabel.setForeground(Color.GREEN);
}
else if (e.getSource() == blueButton) {
messageLabel.setForeground(Color.BLUE);
}
else if (e.getSource() == cyanButton) {
messageLabel.setForeground(Color.CYAN);
}
}
}
/**
* the main method creates an instance of the ColorFactory class, displaying its window
* @param args
*/
public static void main (String[]args) {
new ColorFactory();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
