Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: another problem

  1. #1
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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:
    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();

    }
    }
    /]


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    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();

    }
    }

  3. #3
    Junior Member james's Avatar
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: another problem

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