Jump to content

Displaying Color, please help.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
seph6664

seph6664

    Newbie

  • Members
  • PipPip
  • 10 posts
My java final is coming up and we have a practice assignment to do, he basically said that if can set this up we will do fine on the exam...except I cannot figure out one last part.
The assignment is to make a frame with a panel inside that will change colors once a button is pushed. I set up a frame and have the buttons but I do not know how to make the panel change colors.
If anyone could help me in anyway to understand how to do this I would greatly appreciate it.

Frame Code

package test;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;


/**

 * A frame that shows the growth of an investment with variable interest.

 */

public class ThreeColor extends JFrame {

	public ThreeColor() {


		resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);

		resultArea.setEditable(false);


		// Use helper methods


		createButton();

		createGreenButton();

		createBlueButton();

		createPanel();


		setSize(FRAME_WIDTH, FRAME_HEIGHT);

	}


	private void createButton() {

		redButton = new JButton("Red");


		{

			class AddInterestListener implements ActionListener {

				public void actionPerformed(ActionEvent event) {

					repaint();

				}

			}

		}


	}

	private void createGreenButton() {

		greenButton = new JButton("Green");


		{

			class AddInterestListener implements ActionListener {

				public void actionPerformed(ActionEvent event) {

					repaint();

				}

			}

		}


	}

	private void createBlueButton() {

		blueButton = new JButton("Blue");


		{

			class AddInterestListener implements ActionListener {

				public void actionPerformed(ActionEvent event) {

					repaint();

				}

			}

		}


	}


	private void createPanel() {

		panel = new JPanel();


		panel.add(redButton);

		panel.add(greenButton);panel.add(blueButton);

		


		JScrollPane scrollPane = new JScrollPane(resultArea);

		panel.add(scrollPane);

		add(panel);

	}


	

	private JButton redButton;

	private JButton greenButton;

	private JButton blueButton;


	private JTextArea resultArea;

	private JPanel panel;


	private static final int FRAME_WIDTH = 400;

	private static final int FRAME_HEIGHT = 250;


	private static final int AREA_ROWS = 10;

	private static final int AREA_COLUMNS = 30;


}


Frame Viewer

package test;




import javax.swing.JFrame;

public class ThreeColorViewer

{

   public static void main(String[] args)

   {

      JFrame frame = new ThreeColor();

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setTitle("ThreeColor");

      frame.setVisible(true);

   }

}




#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
The easy part of this is setting the color, which is accomplished by just using panel.setBackground(Color), no problem:
            public void actionPerformed(ActionEvent event) {
                panel.setBackground(Color.GREEN);
                panel.repaint();
            }
You'll also need to import java.awt.Color.

The less obvious problem is that these buttons won't even do anything, they're broken. You neglected to use the JButton.addActionListener() method on each of them, so they don't do anything until you add that ActionListener object! Get rid of your code called "AddInterestListener" class or whatever, and do this:
    private void createButton() {
        redButton = new JButton("Red");
        redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                panel.setBackground(Color.RED);
                panel.repaint();
            }
        });
    }
for each button. That should take care of everything.

What I did was I used an anonymous class, it's something that is used frequently in Java, especially Swing, so you should get used to it. All you need to do is declare a new object of the ObjectType and use brackets to fill in the necessary methods or properties that must be added.
Wow I changed my sig!