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 2 of 2

Thread: no constructor found for thread

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default no constructor found for thread

    im trying to make the light auto changing with implementing runnable but i got runtime error cause of no constructor found for thread any idea wht i hav to do to make it run
    import java.awt.*;
    import javax.swing.*;
     
     
    public class lamp extends JFrame implements Runnable {
     
     
          Signal green = new Signal(Color.green);
          Signal yellow = new Signal(Color.yellow);
          Signal red = new Signal(Color.red);
          S
     
     
        public void run()
        {
     
            boolean nyala = true; 
            while(nyala)
            {
                try
                {
                if (nyala)
                {
                    red.turnOn(false);
                    yellow.turnOn(false);
                    green.turnOn(true);
                }
                else
                {
                    red.turnOn(true);
                    yellow.turnOn(false);
                    green.turnOn(false);
                }      
     
            } 
                catch (Exception e)
                {
                }
    			nyala = !nyala;
    		}
        }
        public lamp(){
            super("Traffic Light");
            getContentPane().setLayout(new GridLayout(2,2,25,25));
     
     
            green.turnOn(false);
            yellow.turnOn(false);
            red.turnOn(true);
     
     
     
            JPanel p1 = new JPanel(new GridLayout(3,1));
            p1.add(red);
            p1.add(yellow);
            p1.add(green);
     
     
     
            getContentPane().add(p1);
     
            pack();
     
            new Thread(red).start();
     
     
        }
     
     public static void main(String[] args){
            lamp tl = new lamp();        
            tl.setVisible(true);
     
        } 
     
     
    }     
     
    class Signal extends JPanel{
     
        Color on;
        int radius = 40;
        int border = 10;
        boolean change;
     
        Signal(Color color){
            on = color;
            change = true;
     
        }
     
        public void turnOn(boolean a){
            change = a;
            repaint();        
        }
     
        public Dimension getPreferredSize(){
            int size = (radius+border)*2;
            return new Dimension( size, size );
        }
     
        public void paintComponent(Graphics g){
            g.setColor( Color.black );
            g.fillRect(0,0,getWidth(),getHeight());
     
            if (change){
                g.setColor( on );
            } else {
                g.setColor( on.darker().darker().darker() );
            }
            g.fillOval( border,border,2*radius,2*radius );
        }
    }


  2. #2
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: no constructor found for thread

    new Thread(red); // Red or signal does not implement Runnable
    // Anonymous Thread and Runnable method
            new Thread(new Runnable(){
     
    			@Override
    			public void run() {
    				// Dowork;
     
    			}
     
            });
     
    // Or Signal would need to implement Runnable

    That would at least get it to compile, but I doubt this is the best way to implement what you are trying to do.

Similar Threads

  1. [SOLVED] no suitable constructor found for JTextArea error
    By steph_malcampo in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 31st, 2013, 01:04 AM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 05:42 PM
  3. Replies: 4
    Last Post: June 15th, 2012, 02:50 PM