I am trying to compile my java file here but I am getting 8 erros with the same sybmol : illegal start of an expression

here is my coding:

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

public class JChangeFont extends JApplet implements ActionListener
{
   JLabel banner = new JLabel("Your Name");
   int fontSize = 18;
   Font theFont = new Font("TimesRoman",Font.BOLD, fontSize);
   JButton button1 = new JButton("Press");
   Container con = getContentPane();
   FlowLayout flow = new FlowLayout();

   public void init()
   {
      Container con = getContentPane();
      FlowLayout flow = new FlowLayout();
      con.setLayout(flow);
      banner.setFont(theFont);
      con.add(banner);
      con.add(button1);
      button1.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
     Object source = e.getSource();
     if (source == button1)
      {
        Font newFont = new Font("Helvetica",Font.ITALIC, fontSize +  10);

     banner.setFont(newFont);
}

   public static void main(String args[]);
{

        JChangeFont applet = new JChangeFont();

        Frame window = new Frame("JChangeFont");

window.addWindowListener(new WindowAdapter() 
{

            public void windowClosing(WindowEvent e) 
{

                System.exit(0);

            }

        });



        applet.init();

        window.add("Center", applet);

        window.pack();

        window.setVisible(true);

    }

}

what is wrong with it? I just don't get it. I am stuck. Can you please give some hints.

Thanks a lot

First: Please use code tags! [code=c]code[/code]
Second: I think this :

window.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
});

should be:

window.addWindowListener(new WindowAdapter()){
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
};

First of all, an Applet doesn't have a main method. You have to remove that main method and must write that windowClosing() event within actoinPerformed(). Hope when you remove the main method almost half of your problem will be solved. Please do it and let me know the error you get after that.

I am trying to compile this java file and it is giving me an error say : java.lang.nosuchmethodeorror.main

what could be the problem?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class JChangeFont extends JApplet implements ActionListener
{
   JLabel banner = new JLabel("Your Name");
   int fontSize = 18;
   Font theFont = new Font("TimesRoman",Font.BOLD, fontSize);
   JButton button1 = new JButton("Press");
   Container con = getContentPane();
   FlowLayout flow = new FlowLayout();
 
   public void init()
   {
      Container con = getContentPane();
      FlowLayout flow = new FlowLayout();
      con.setLayout(flow);
      banner.setFont(theFont);
      con.add(banner);
      con.add(button1);
      button1.addActionListener(this);
   }
 
   public void actionPerformed(ActionEvent e)
   {
     Object source = e.getSource();
     if (source == button1)
      {
        Font newFont = new Font("Helvetica",Font.ITALIC, fontSize +  10);
        banner.setFont(newFont);
 
     }
 
   }
 
 
 
}

Are you trying to run it in Eclipse? I think you have to tell it to run as a Java Applet under "Run as" if that is the case...

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

public class JChangeFont extends JApplet implements ActionListener {

	JLabel banner = new JLabel("Your Name");
	int fontSize = 18;
	Font theFont = new Font("TimesRoman",Font.BOLD, fontSize);
	JButton button1 = new JButton("Press");
	Container con = getContentPane();
	FlowLayout flow = new FlowLayout();

	public void init(){

		Container con = getContentPane();
		FlowLayout flow = new FlowLayout();
		con.setLayout(flow);
		banner.setFont(theFont);
		con.add(banner);
		con.add(button1);
		button1.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e){

		Object source = e.getSource();

		if (source == button1) {

			Font newFont = new Font("Helvetica",Font.ITALIC, fontSize + 10);
			banner.setFont(newFont);
		}
	}
	public static void main(String args[]){

		JChangeFont applet = new JChangeFont();

		Frame window = new Frame("JChangeFont");

		window.addWindowListener(new WindowAdapter(){

			public void windowClosing(WindowEvent e){

				System.exit(0);

			}

		});


		applet.init();

		window.add("Center", applet);

		window.pack();

		window.setVisible(true);

	}

}

This program is error free and works fine. Please compare this code with the code which is giving you an error. Then you'll come to know where I've edited it. Have a great day and enjoy!!

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.