Go Back   Java Programming Forums > Java Standard Edition Programming Help > What's Wrong With My Code?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-02-2010, 01:13 AM
james's Avatar
Junior Member
 

Join Date: Jan 2010
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
james is on a distinguished road
Unhappy another problem

hey guys it's me again,well i designed one program but could you help me in correcting it because it has too many errors. well the requirements is written below::confu sed:
design a GUI that contains two rows of JButtons with three buttons in each row the buttons in the top row are labelled Red, Yellow, and Green.the buttons in the bottom row are labelled Stop, Look and Go.Iniatially, all buttons inthe top row are white.When the user clicks on the Stop button, the Red button changes its colour to red and the Yellow and green buttons remain white, Similarly,when the user clicks the Look button the yellow changes it's colur to yellow and the Red and Green buttons become white, and so on.

AND HERE IS MY PROGRAM:
[import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class trafficlights extends JFrame

{
private JButton redB,yellowB,greenB;
private JButton stopB,lookB,goB;
private StopButtonHandler sHandler;
private LookButtonHandler lHandler;
private GoButtonHandler gHandler;
private Container pane;
private static final int WIDTH = 456;
private static final int HEIGHT = 450;

public trafficlights()
{
redB = new JButton("Red", SwingConstants.RIGHT);

yellowB = new JButton("Yellow ", SwingConstants.CENTER);

greenB = new JButton("Green ", SwingConstants.LEFT);


stopB = new JButton("Stop");
sHandler = new StopButtonHandler();
stopB.addActionListener(sHandler);

lookB = new JButton("Look");
lHandler = new LookButtonHandler();
lookB.addActionListener(lHandler);


goB = new JButton("Go");
gHandler = new GoButtonHandler();
goB.addActionListener(gHandler);

Container pane = getContentPane();

pane.setLayout(new GridLayout (6, 3));

pane.add(redB);
pane.add(yellowB);
pane.add(greenB);
pane.add(stopB);
pane.add(lookB);
pane.add(goB);




setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class StopButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
redB.setForeground(Stop.red);


}
}

private class LookButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
yellowB.setForeground(Look.yellow);

}
}


private class GoButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
greenB.setForeground(Go.green);
}
}


public static void main(String [] args)
{
trafficlights demoObject = new trafficlights();

}
}
/]



Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 09-02-2010, 05:51 AM
Member
 

Join Date: Feb 2010
Location: Dehradun, India
Posts: 37
Thanks: 1
Thanked 7 Times in 6 Posts
jassi is on a distinguished road

I'm feeling Cold
Default Re: another problem

Here is your resolved Program. James May i Know you are using which editor???

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class trafficlights extends JFrame

{
private JButton redB,yellowB,greenB;
private JButton stopB,lookB,goB;
private StopButtonHandler sHandler;
private LookButtonHandler lHandler;
private GoButtonHandler gHandler;
private Container pane;
private static final int WIDTH = 456;
private static final int HEIGHT = 100;

public trafficlights()
{

redB = new JButton("Red");

yellowB = new JButton("Yellow ");

greenB = new JButton("Green ");

redB.setForeground(Color.white);
yellowB.setForeground(Color.white);
greenB.setForeground(Color.white);

stopB = new JButton("Stop");
sHandler = new StopButtonHandler();
stopB.addActionListener(sHandler);

lookB = new JButton("Look");
lHandler = new LookButtonHandler();
lookB.addActionListener(lHandler);


goB = new JButton("Go");
gHandler = new GoButtonHandler();
goB.addActionListener(gHandler);

Container pane = getContentPane();

pane.setLayout(new GridLayout (2, 3));


pane.add(redB);
pane.add(yellowB);
pane.add(greenB);
pane.add(stopB);
pane.add(lookB);
pane.add(goB);





setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class StopButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
redB.setForeground(Color.red);
yellowB.setForeground(Color.white);
greenB.setForeground(Color.white);

}
}

private class LookButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
yellowB.setForeground(Color.yellow);
redB.setForeground(Color.white);
greenB.setForeground(Color.white);
}
}


private class GoButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent e)
{
greenB.setForeground(Color.green);
redB.setForeground(Color.white);
yellowB.setForeground(Color.white);
}
}


public static void main(String [] args)
{
trafficlights demoObject = new trafficlights();

}
}
Reply With Quote
  #3 (permalink)  
Old 09-02-2010, 10:52 PM
james's Avatar
Junior Member
 

Join Date: Jan 2010
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
james is on a distinguished road
Thumbs up Re: another problem

hey,jassi thanks very much for your help it really worked.By the way i,am using Edit plus3.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java convert list to map java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 02:34 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.