Hi all,
I am trying to learn java here, and having some difficulties with a very simple program that I am working on in an attempt to get a feel for what the language can do. This is my code so far:

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

public class Test extends JFrame{
public static void main(String[] args){
JFrame box = new JFrame();
box.setTitle("Week1 test");
box.setSize(240,240);
box.setVisible(true);
box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
label.setSize(160,160);
label.setVisible(true);
box.add(label);
JButton B1 = new JButton("Exit");
B1.setVisible(true);
box.add(B1);
box.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.Exit(0);}
});}}

Unfortunately, whenever I try to compile this in the Windows 7 command prompt, it comes back with a confusing error messages claiming that it cannot find symbol. The exact errors I am getting is:

Test.java:22: error: cannot find symbol
> System.Exit();}
>              ^
>     symbol: method Exit(int)
>     location: class System
> 
> Test.java:20: error: cannot find symbol
> box.addActionListener(new ActionListener(){
>       ^
>       symbol: method addActionListener((anonymous ActionListener))
>       location: variable box of type JFrame
> I was under the impression that both of these, System.Exit(), and addActionListener(), were imported with javax.swing.*;

Was I mistaken in that assumption?? Please explain what is wrong here to me, so I can continue to get more understanding of java, and thanks in advance!!

Recommended Answers

All 4 Replies

exit(), not Exit(). Java is case-sensitive
actionlisteners are for buttons etc, you seem to be trying to add one to a JFrame

to be clearly (like the above post),

importing actionListner class into your class that doesn't a matter

your class should be implemented ActionListner class too

and the remainng error is as above post said ( replace exit() instead of Exit() )

Radhakrishna: why should that class implement ActionListener? the way he does it works just as well.

Yes, Radhakrishna doesn't seem to know about anonymous inner classes. The action listener code is OK, it's just that OP is trying to add it to something that doesn't support action listeners.

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.