Computer Science Canada Java Buttons Help |
Author: | an_mui [ Sat Oct 09, 2004 2:28 pm ] |
Post subject: | 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); } } } |
Author: | wtd [ Sat Oct 09, 2004 6:52 pm ] | ||
Post subject: | |||
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.
|
Author: | an_mui [ Sat Oct 09, 2004 8:17 pm ] |
Post subject: | |
thanks. sorry i wasn't familiar with the code tag |
Author: | an_mui [ Sun Oct 10, 2004 6:17 pm ] |
Post subject: | |
ok problem solved. =) |