Hello,
I am just starting to learn Java. I tried to compile the following code
(from divelog tutorial on Sun's "New to Java"), and it returned five
<identifier> expected errors. What identifiers do I put there?
--
[code]
package divelog;
import java.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DiveLog
{
private JFrame dlframe;
private JTabbbedPane tabbedpane;
dlframe = new JFrame("Dive Log - by Shengzhi Li");
dlframe.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tabbedpane = new JTabbedPane(SwingConstants.LEFT);
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
}
[/code]
--
Here is the compiler output:
[output]
DiveLog.java:12: <identifier> expected
dlframe = new JFrame("Dive Log - by Shengzhi Li");
^
DiveLog.java:13: <identifier> expected
dlframe.addWindowListener( new WindowAdapter(){
^
DiveLog.java:20: <identifier> expected
tabbedpane = new JTabbedPane(SwingConstants.LEFT);
^
DiveLog.java:21: <identifier> expected
tabbedPane.setBackground(Color.blue);
^
DiveLog.java:22: <identifier> expected
tabbedPane.setForeground(Color.white);
^
5 errors
Press any key to exit
[/output]
Please help. Thanks.
~sphoenixee~
Tor Iver Wilhelmsen - 16 Jun 2005 21:07 GMT
> dlframe.addWindowListener( new WindowAdapter(){
> public void windowClosing(WindowEvent e)
> {
> System.exit(0);
> }
> });
...
> tabbedPane.setBackground(Color.blue);
> tabbedPane.setForeground(Color.white);
You need to put these into a constructor or instance initializer
block. Unlike the declaration and assignment, other code needs to be
inside a method.
Remi Arntzen - 16 Jun 2005 21:21 GMT
---CODE---
dlframe = new JFrame("Dive Log - by Shengzhi Li");
dlframe.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tabbedpane = new JTabbedPane(SwingConstants.LEFT);
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
---END CODE---
That entire section is incorrect.
Try placing this code into a method such as
private static JFrame dlframe;
private static JTabbbedPane tabbedpane;
public static void main(String[] args) {
[code]
}
You can only put code like this " dlframe.addWindowListener( new
WindowAdapter(){ " inside a method or static initialization block
private static JFrame dlframe;
private static JTabbbedPane tabbedpane;
static {
[CODE]
}
I recommend looking at
http://java.sun.com/docs/books/tutorial/getStarted/index.html as a
learning tool.
This is not your fault the original author of the code is to blame.
(from divelog tutorial on Sun's "New to Java")
Could you give me the url for this "tutorial".
Ian Shef - 16 Jun 2005 21:40 GMT
"sphoenixee" <shengzhi1@gmail.com> wrote in news:1118951161.748221.157970
@o13g2000cwo.googlegroups.com:
> Hello,
>
[quoted text clipped - 54 lines]
>
> ~sphoenixee~
If you look at
http://java.sun.com/developer/onlineTraining/new2java/divelog/part1/page5.j
sp
You will see:
=========================================================================
1. Open the DiveLog.java class file.
2. Type or cut and paste the following lines of code:
public DiveLog()
{
// Create a frame object to add the
// application GUI components to.
dlframe = new JFrame(
"A Java Technology Dive Log");
// Closes from title bar
//and from menu
dlframe.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}// Ends constructor
3. Save the file.
=========================================================================
The identifier that is expected is the name of a constructor (DiveLog, same
as the name of the class) or of a type (such as JFrame). The identifier
may be preceeded by various access types; "public" is one of these.
For simplicity, I have intentionally omitted mentioning some of the other
things that could be placed here. The point is that you intended to define
a constructor but omitted properly starting its definition.

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *