Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2007, 03:19 AM
Member
 
Join Date: Jul 2007
Posts: 5
nehasahu is on a distinguished road
java program
Code:
import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class Anu extends JApplet { JLabel j1, j2,j3, j4; JTextField t1,t2,t3,t4; JButton b1,b2; JFrame f1; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); j1=new JLabel("enter your name"); c.add(j1); t1=new JTextField(10); c.add(t1); j2=new JLabel("enter your age"); c.add(j2); t2=new JTextField(10); c.add(t2); j3=new JLabel("enter your sex"); c.add(j3); t3=new JTextField(10); c.add(t3); j4=new JLabel("enter your email id"); c.add(j4); t4=new JTextField(10); c.add(t4); b1=new JButton("Submit"); c.add(b1); b2=new JButton("Cancel"); c.add(b2); } b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { t1.setText(" "); t2.setText(" "); t3.setText(" "); t4.setText(" "); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String rin; rin=t4.getText(); if (rin=="rinki") JOptionPane.showMessageDialog(f1,"great"); else JOptionPane.showMessageDialog(f1,"bad"); } }); };
Hi friends ,

When I execute the above program its giving me following error
b1.addActionListener(new ActionListener() {
^
<identifier> expected
b2.addActionListener(new ActionListener(){
^
2 errors

Please help me out..................dont know why i am getting this error...

Thaks

Last edited by JavaBean : 07-10-2007 at 01:48 PM. Reason: Code placed inside [code] tag!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-10-2007, 01:50 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Because those lines are not inside any method of the class! You can't place such statements inside the class directly. Your code should be inside methods.

Move those lines inside init method and try again.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-10-2007, 08:16 PM
Member
 
Join Date: Jul 2007
Posts: 5
nehasahu is on a distinguished road
Thanks for the suggestion You are correct that was a mistake....
but when i put it inside the program i am getting following error....
==================================================
anu.java:6: anu is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class anu extends JApplet implements ActionListener
================================================== ====

just pasting the whole program again

Code:
import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class anu extends JApplet implements ActionListener { JLabel j1, j2,j3, j4; JTextField t1,t2,t3,t4; JButton b1,b2; JFrame f1; JPanel p1; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); /*p1.add(f1);*/ f1.add(c); f1.setVisible(true); j1=new JLabel("enter your name"); f1.add(j1); t1=new JTextField(10); f1.add(t1); j2=new JLabel("enter your age"); f1.add(j2); t2=new JTextField(10); f1.add(t2); j3=new JLabel("enter your sex"); f1.add(j3); t3=new JTextField(10); f1.add(t3); j4=new JLabel("enter your email id"); f1.add(j4); t4=new JTextField(10); f1.add(t4); b1=new JButton("Submit"); f1.add(b1); b2=new JButton("Cancel"); f1.add(b2); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { t1.setText(" "); t2.setText(" "); t3.setText(" "); t4.setText(" "); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String rin; rin=t4.getText(); if (rin=="rinki") JOptionPane.showMessageDialog(f1,"great"); else JOptionPane.showMessageDialog(f1,"bad"); } }); } };

Last edited by JavaBean : 07-10-2007 at 09:20 PM. Reason: Code is placed inside code tag.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-10-2007, 09:24 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Nehasahu, first of all please use [code] tag around your codes. That will make your codes more readable.

And in your code, your anu class is declared to implement ActionListener but it does not override public void actionPerformed(ActionEvent e). That is the meaning of the error you see.

You have two choices:

1. Combine your existing actionPerformed methods inside anu class as a separate method and handle all your actions inside that method.

2. Remove "implements ActionListener" text from your anu class and continue with your already written action listeners.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-12-2007, 09:19 AM
Member
 
Join Date: Jul 2007
Posts: 5
nehasahu is on a distinguished road
i have removed "implements ActionListener" text from your anu class but still my program is not working its giving me a new error Applet not initialized...i am getting a blank applet..
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-12-2007, 12:05 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
The applet should return an exception. You need to learn the details about the exception. What is it and which line the exception is being thrown? These details should be shown on Java Console. Check following resources:

Java Plug-in Console
Enable and View Java Console Window - 1.4.2_xx

After knowing the details about the exception, we can advise you a solution.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
New to Java Program jvasilj1 New To Java 1 02-05-2008 08:22 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
help with simple java program leonard New To Java 3 07-30-2007 10:40 AM
help with java program mattvgt New To Java 3 07-14-2007 05:57 PM


All times are GMT +3. The time now is 10:34 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org