PDA

View Full Version : Java newbie help


Suzuran
08-30-2007, 02:59 PM
I'm new to Java, I use NetBeans and I copied a code from a tutorial and compiled it but it returns 2 errors.

The code is:package test;

import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame{
public Test()
{
super("Menu example");

JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic('N');
file.add(newItem);
JMenuItem openItem = new JMenuItem("Open");
openItem.setMnemonic('O');
file.add(openItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
file.add(exitItem);

//adding action listener to menu items
newItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("New is pressed");
}
}
);
openItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Open is pressed");
}
}
);
exitItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Exit is pressed");
}
}
);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(file);

getContentPane();
setSize(200, 200);
setVisible(true);
}

public static void main(String[] args)
{
Menu app = new Test();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

And the errors:init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Julien\Test\build\classes
C:\Documents and Settings\Julien\Test\src\test\Main.java:14: class Test is public, should be declared in a file named Test.java
public class Test extends JFrame{
C:\Documents and Settings\Julien\Test\src\test\Main.java:67: cannot find symbol
symbol : class Menu
location: class test.Test
Menu app = new Test();
^
2 errors
BUILD FAILED (total time: 5 seconds)

nikkiH
08-30-2007, 03:09 PM
Just as it says. You saved the file as Main.java. It should be Test.java.

Aradon
08-30-2007, 11:29 PM
Just as it says. You saved the file as Main.java. It should be Test.java.

And the reason is should be Test.java is as such. If you look at your java file you will see the following:



public class Test


Whatever you name this top public class; it is required to be the name of the file. So if you name the class


public class myExampleClass


Your name would have to be myExampleClass.java