Results 1 to 6 of 6

Thread: java program

  1. #1
    nehasahu is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default java program

    Java 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 12:48 PM. Reason: Code placed inside [code] tag!

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

  3. #3
    nehasahu is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default

    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

    Java 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 08:20 PM. Reason: Code is placed inside code tag.

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

  5. #5
    nehasahu is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default

    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..

  6. #6
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  2. New to Java Program
    By jvasilj1 in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 07:22 AM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  4. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM
  5. help with java program
    By mattvgt in forum New To Java
    Replies: 3
    Last Post: 07-14-2007, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •