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

Recommended Answers

All 3 Replies

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.

just remove

}

from line34th.

just remove

}

from line34th.

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

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.