Member Avatar for dj118

Hi viewers, I'm in dire need of some help. I'm currently doing a program for my studies and I have successfully compiled it and tested it. However when I try to execute the program it comes up with the error java.lang.NoSuchMethodError: Main. I've identified the problem which is the fact that I'm missing the public static void main(String[] args) line of code. however what my main problem is when I place it at under the public class line of code it still gives me an error. Is there anyway around this. I will be grateful if there is a solution to this

Thanks

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class ChangingFace extends JFrame implements ActionListener
{
        private boolean isHappy = true; // will determine the mood of the face
        private JButton happyButton = new JButton("Smile");
        private JButton sadButton = new JButton("Frown");
       
        public ChangingFace()
        {
                // set the title
                setTitle("Changing Face");
               
                // choose a Flow Layout policy
                setLayout(new FlowLayout());
               
                // add the buttons to the frame
                add(happyButton); add(sadButton);
               
                // set the background to yellow
                getContentPane().setBackground(Color.yellow);
               
                // enable the buttons to listen for a mouse-click
                happyButton.addActionListener(this);
                sadButton.addActionListener(this);
               
                // configure the frame
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setSize(250, 200);
                setLocation(300,300);
                setVisible(true);
        }
       
        public void paint(Graphics g)
        {
                // call the paint method of the superclass, Jframe
                super.paint(g);
                // paint the face
                g.setColor(Color.red);
                g.drawOval(85,75,75,75);
                g.setColor(Color.blue);
                g.drawOval(100,95,10,10);
                g.drawOval(135,95,10,10);
                g.drawString("Changing Face", 80,185);
                if(isHappy == true)
                {
                        // draw a smiling mouth
                        g.drawArc(102,115,40,25,0,-180);
                }
                else
                {
                        // draw a frowning mouth
                        g.drawArc(102,115,40,25,0,180);
                }
        }
                // this is where we code the event-handling routine
        public void actionPerformed(ActionEvent e)
        {
                if(e.getSource() == happyButton)
                {
                        isHappy = true;
                        repaint();
                }
                if(e.getSource() == sadButton)
                {
                        isHappy = false;
                        repaint();
                }
        }
}

I'm not sure why you would be getting an error while placing it after the public class line. Below is what I did and it compiles fine:

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

public class ChangingFace extends JFrame implements ActionListener 
{
	public static void main(String[] args)
	{
		...
	}
              ....
Member Avatar for dj118

Thanks for that I done it again and it compiled. Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.