Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Still stuck-taking different approach plz help
Kendra Payne
Greenhorn
Posts: 22
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have completely changed my code and am still getting errors and have no idea whatsoever what is wrong. Please help if you can.
Thanks in advance.
import java.awt.*; import java.awt.event.*; public class ColorButtons2 extends Frame implements ItemListener { CheckboxGroup colorGroup; Checkbox blue, red, yellow, pink, gray; Panel panel; public ColorButtons() { panel = new Panel(); this.setLayout(new BorderLayout()); panel.setLayout(new FlowLayout()); colorGroup = new CheckboxGroup(); Checkbox blue = new Checkbox("Blue",false,colorGroup); blue.addItemListener(this); Checkbox red= new Checkbox("Red",false,colorGroup); red.addItemListener(this); Checkbox yellow = new Checkbox("Yellow",false,colorGroup); yellow.addItemListener(this); Checkbox pink = new Checkbox("Pink",false,colorGroup); pink.addItemListener(this); Checkbox gray = new Checkbox("Gray",false,colorGroup); gray.addItemListener(this); panel.add(blue); panel.add(red); panel.add(yellow); panel.add(pink); panel.add(gray); add(panel, BorderLayout.CENTER); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public static void main(String args[]) { ColorButtons cb = new ColorButtons(); cb.setBounds(200, 200, 500, 100); cb.setTitle("What's My Color?"); cb.setVisible(true); } public void itemStateChanged(ItemEvent e) { if(blue.getState()) panel.setBackground(Color.blue); else if(red.getState()) panel.setBackground(Color.red); else if(yellow.getState()) panel.setBackground(Color.yellow); else if(pink.getState()) panel.setBackground(Color.pink); else if(gray.getState()) panel.setBackground(Color.gray); } }
Edwin Dalorzo
Ranch Hand
Posts: 961
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well, at first sight... you class is named
ColorButtons2
but your constructor says
ColorButtons
.
Fix that and it should compile and run.
Michael Dunn
Ranch Hand
Posts: 4632
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
because of this
Checkbox blue, red, yellow, pink, gray;
all of these
Checkbox blue = new Checkbox("Blue",false,colorGroup);
should be just
blue = new Checkbox("Blue",false,colorGroup);
Kendra Payne
Greenhorn
Posts: 22
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I fixed and got it to compile but it is a blank white frame. The buttons are not there.
Michael Dunn
Ranch Hand
Posts: 4632
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
if you changed the constructor to ColorButtons2, did you also change this in main()
ColorButtons cb = new ColorButtons();
if not, you might be working with an old compiled class
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Converting a Combobox to Radio Buttons
Stuck on checkboxes -pleeeeze help
ItemListener
problem with updating Panel!
simon game
More...