Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Applets
Search Coderanch
Advance search
Google search
Register / Login
Help coderanch get a
new server
by contributing to the
fundraiser
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Applets
cant change background color of applet with button click
Ben Hultin
Ranch Hand
Posts: 135
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am working on an assignment to create an
applet
where I need to change the background color of the applet with some button clicks
I am not getting any errors in eclipse so far, here is my code:
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Applet_Form extends Applet implements ActionListener { /** * */ private static final long serialVersionUID = 1L; Color bgcolor; Button okButton; Button red; Button green; TextField nameField; TextField emailField; TextField phoneField; TextField cityField; public void init() { // Tell the applet not to use a layout manager. setLayout(null); // initialze the button and give it a text. okButton = new Button("Submit"); red = new Button("Red"); green = new Button("Green"); // text and length of the field nameField = new TextField("Name",150); emailField = new TextField("Email",150); phoneField = new TextField("Phone",70); cityField = new TextField("City",150); nameField.setBounds(20,20,150,20); emailField.setBounds(20,50,150,20); phoneField.setBounds(20,80,70,20); cityField.setBounds(20,110,150,20); okButton.setBounds(20,150,100,30); red.setBounds(20,190,100,30); green.setBounds(20,230,100,30); bgcolor=Color.white; // now that all is set we can add these components to the applet add(nameField); add(emailField); add(phoneField); add(cityField); add(okButton); add(red); add(green); } public void paint(Graphics g){ setBackground(bgcolor); } public void actionPerformed(ActionEvent e) { if(e.getSource()==red){ bgcolor=Color.red; setBackground(bgcolor); repaint(); } else if (e.getSource()==green) { bgcolor=Color.green; setBackground(bgcolor); repaint(); } } }
I appreciate the help
Ulf Dittmer
Rancher
Posts: 43081
77
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You haven't added the applet as an action listener to the 3 buttons, hence the actionPerformed method is never called.
Ben Hultin
Ranch Hand
Posts: 135
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
so would that be something like this?
Applet_Form handler = new Applet_Form(); red.addActionListener(handler); green.addActionListener(handler);
Ulf Dittmer
Rancher
Posts: 43081
77
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You shouldn't create a new instance of the applet class; it'd be like:
red.addActionListener(this);
green.addActionListener(this);
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Applet BackGround
. . .not abstract and doesnt overide abstract method actionPerfomed
can't see the background color of a frame , why?
Troubles with ActionListener and event driven program
method not abstract
More...