954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help required in simple java program??

i was coding GUI in Java. i'm just a beginner and i can't figure out on what program is mistaken please help me on this:

Compilation error is:
helloworld\FourthApp.java:38: class, interface, or enum expected
public static void main(String[] args) {
^
helloworld\FourthApp.java:40: class, interface, or enum expected
}
^
2 errors

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

public class FourthApp extends JFrame implements ActionListener {
	JFrame interfaceFrame;
	JButton startButton, stopButton;
	public FourthApp() {	
		JFrame.setDefaultLookAndFeelDecorated(true);
		interfaceFrame = new JFrame("GUI");
		interfaceFrame.setSize(200,200);
		interfaceFrame.setVisible(true);
		interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
		
			startButton = new JButton("Start");
			startButton.addActionListener(this);
			interfaceFrame.add(startButton);
			
			stopButton = new JButton("Stop");
			stopButton.addActionListener(this);
			interfaceFrame.add(stopButton);
			interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			interfaceFrame.setVisible(true);
	}
	
		public void actionPerformed(ActionEvent a) {
			if(a.getSource() == startButton){
				System.out.println("Start Button was Pressed");
			}	
			if(a.getSource() == stopButton) {
				System.out.println("Stop Button was Pressed");
			}
		}
	}
	public static void main(String[] args) {
		new FourthApp();
	}
}
Chitru
Newbie Poster
8 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

Fixing your indentation your code looks like this

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

public class FourthApp extends JFrame implements ActionListener {
	JFrame interfaceFrame;
	JButton startButton, stopButton;
	public FourthApp() {	
		JFrame.setDefaultLookAndFeelDecorated(true);
		interfaceFrame = new JFrame("GUI");
		interfaceFrame.setSize(200,200);
		interfaceFrame.setVisible(true);
		interfaceFrame.setLayout(new java.awt.GridLayout(1,2));
		
		startButton = new JButton("Start");
		startButton.addActionListener(this);
		interfaceFrame.add(startButton);
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(this);
		interfaceFrame.add(stopButton);
		interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		interfaceFrame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a) {
		if(a.getSource() == startButton){
			System.out.println("Start Button was Pressed");
		}	
		if(a.getSource() == stopButton) {
			System.out.println("Stop Button was Pressed");
		}
	}
}
public static void main(String[] args) {
	new FourthApp();
}
}


Do you notice the problem? Namely that extra closing brace after the actionPerformed method that ends that class definition, meaning that it expects the next line to declare a new class/interface/enum. As the error says.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

just remove

}


from line34th.

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

just remove

}
from line34th.

It would help to explain to himwhy he should do that. Which has already been done, above, BTW.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: