
-----------------------------------
an_mui
Sat Oct 09, 2004 2:28 pm

Java Buttons Help
-----------------------------------
1. Write a program that will change a label's colour each time a button is pressed (use at least 5 colours)

I don't know why but the background colour does not show up. Can someone please help me?

package button1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class button1 extends JApplet implements ActionListener
{
  JButton mybutton;
  JLabel label;
  static int counter =0;
  JFrame frame1 = new JFrame ("first name");

  public void init()
  {
  JLabel label = new JLabel (" ");
  label.setSize(300,300);
  label.setBackground(Color.blue);
  label.setOpaque(true);

  mybutton = new JButton ("Press me!");
  mybutton.addActionListener(this);

  Container contents = new Container();
  contents.setLayout(new FlowLayout());
  frame1.setSize(300,300);

  contents.add(label);
  contents.add(mybutton);
  frame1.getContentPane().add(contents);
  frame1.setVisible(true);
  }

  public void actionPerformed (ActionEvent e)
  {
    counter++;
    if (counter ==2)
    {
    label.setBackground(Color.red);
    }
    else if (counter==6)
    {
      JLabel Message = new JLabel ("End of Program");
      Container message_container = new Container ();
      message_container.setLayout(new FlowLayout());
      message_container.add(Message);

      JFrame message_frame = new JFrame ("End of Program");
      message_frame.setSize(300,300);

      message_frame.getContentPane().add(message_container);

      frame1.setVisible(false);
      message_frame.setVisible(true);
    }
  }
}

-----------------------------------
wtd
Sat Oct 09, 2004 6:52 pm


-----------------------------------
First of all, let's clean this up so it can be read.  Code tags are your friend.  :)

Also, it's good to adhere to Java naming conventions, which frown on the use of underscores.

package button1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Button1 extends JApplet implements ActionListener
{
	JButton myButton;
	JLabel label;
	static int counter = 0;
	JFrame frame1 = new JFrame("first name");

	public void init()
	{
		JLabel label = new JLabel(" ");
		label.setSize(300,300);
		label.setBackground(Color.blue);
		label.setOpaque(true);

		myButton = new JButton("Press me!");
		myButton.addActionListener(this);

		Container contents = new Container();
		contents.setLayout(new FlowLayout());
		frame1.setSize(300,300);

		contents.add(label);
		contents.add(mybutton);
		frame1.getContentPane().add(contents);
		frame1.setVisible(true);
	}

	public void actionPerformed (ActionEvent e)
	{
		counter++;
		
		if (counter == 2)
		{
			label.setBackground(Color.red);
		}
		else if (counter==6)
		{
			JLabel message = new JLabel("End of Program");
			Container messageContainer = new Container();
			messageContainer.setLayout(new FlowLayout());
			messageContainer.add(message);

			JFrame messageFrame = new JFrame("End of Program");
			messageFrame.setSize(300,300);

			messageFrame.getContentPane().add(message_container);

			frame1.setVisible(false);
			messageFrame.setVisible(true);
		}
	}
}

-----------------------------------
an_mui
Sat Oct 09, 2004 8:17 pm


-----------------------------------
thanks. sorry i wasn't familiar with the code tag

-----------------------------------
an_mui
Sun Oct 10, 2004 6:17 pm


-----------------------------------
ok problem solved. =)
