// W3app.java demonstrate a label and a button import java.awt.*; import java.awt.event.*; import java.applet.*; public class W3app extends Applet implements ActionListener { Button helloButton; boolean original=true; public void init() { // The init is responsible for setting // up the initial buttons and color background. setBackground(Color.cyan); setForeground(Color.black); setLayout(new FlowLayout(FlowLayout.CENTER, 15, 10)); add(new Label("a label")); // a label helloButton = new Button("Hello World"); // a button helloButton.addActionListener(this); add(helloButton); setSize(200,200); setBackground(Color.green); setForeground(Color.black); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == helloButton) { if(original) { setForeground(Color.white); setBackground(Color.red); // change text to Good by World original = !original; } else { setBackground(Color.green); setForeground(Color.black); original = !original; } } } }